componentWithScalarArgumentsRendersStartTag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Jhoff\BladeVue\Testing\Unit;
4
5
use Exception;
6
use TypeError;
7
use Jhoff\BladeVue\Components\Basic;
8
use Jhoff\BladeVue\Testing\TestCase;
9
use Jhoff\BladeVue\Components\Inline;
10
11
class ComponentTest extends TestCase
12
{
13
    /**
14
     * @test
15
     */
16
    public function basicComponentRendersStartTag()
17
    {
18
        $tag = Basic::start('foobar');
19
20
        $this->assertRegExp('/\<component.*\>/', $tag);
21
        $this->assertRegExp('/[\s\>]v-cloak[\s\>]/', $tag);
22
        $this->assertRegExp('/[\s\>]is="foobar"[\s\>]/', $tag);
23
    }
24
25
    /**
26
     * @test
27
     */
28
    public function inlineComponentRendersStartTag()
29
    {
30
        $tag = Inline::start('foobar');
31
32
        $this->assertRegExp('/\<component.*\>/', $tag);
33
        $this->assertRegExp('/[\s\>]inline-template[\s\>]/', $tag);
34
        $this->assertRegExp('/[\s\>]v-cloak[\s\>]/', $tag);
35
        $this->assertRegExp('/[\s\>]is="foobar"[\s\>]/', $tag);
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function basicComponentRendersEndTag()
42
    {
43
        $tag = Basic::end();
44
45
        $this->assertEquals('</component>', $tag);
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function tooManyArgumentsThrowsException()
52
    {
53
        try {
54
            Basic::start('foobar', [], 'this is invalid');
55
        } catch (Exception $exception) {
56
            $this->assertEquals('Too many arguments passed to vue directive', $exception->getMessage());
57
            return;
58
        }
59
60
        $this->fail('Failed to throw exception with too many arguments');
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function nonArraySecondArgumentThrowsTypeError()
67
    {
68
        try {
69
            Basic::start('foobar', 'this is invalid');
70
        } catch (TypeError $typeError) {
71
            $this->assertStringContainsString('::start() must be of the type array', $typeError->getMessage());
72
            return;
73
        }
74
75
        $this->fail('Failed to throw type error with non array second argument');
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function nonAssociativeArraySecondArgumentThrowsException()
82
    {
83
        try {
84
            Basic::start('foobar', [1, 2, 3]);
85
        } catch (Exception $exception) {
86
            $this->assertEquals(
87
                'Second argument for vue directive must be an associtive array',
88
                $exception->getMessage()
89
            );
90
            return;
91
        }
92
93
        $this->fail('Failed to throw exception with non associtive array second argument');
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function componentWithBooleanArgumentsRendersStartTag()
100
    {
101
        $tag = Basic::start('foobar', ['foo' => true, 'baz' => false]);
102
103
        $this->assertRegExp('/[\s\>]:foo="true"[\s\>]/', $tag);
104
        $this->assertRegExp('/[\s\>]:baz="false"[\s\>]/', $tag);
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function componentWithScalarArgumentsRendersStartTag()
111
    {
112
        $tag = Basic::start('foobar', ['foo' => 'bar', 'baz' => 123]);
113
114
        $this->assertRegExp('/[\s\>]foo="bar"[\s\>]/', $tag);
115
        $this->assertRegExp('/[\s\>]:baz="123"[\s\>]/', $tag);
116
    }
117
118
    /**
119
     * @test
120
     */
121
    public function componentWithArrayOrObjectArgumentsRendersStartTag()
122
    {
123
        $tag = Basic::start(
124
            'foobar',
125
            [
126
                'foo' => [1, 2, 3],
127
                'bar' => (object) ['foo' => 'bar'],
128
                'baz' => ['baz' => 'qux']
129
            ]
130
        );
131
132
        $this->assertRegExp('/[\s\>]:foo="\[1,2,3\]"[\s\>]/', $tag);
133
        $this->assertRegExp('/[\s\>]:bar="{&quot;foo&quot;:&quot;bar&quot;}"[\s\>]/', $tag);
134
        $this->assertRegExp('/[\s\>]:baz="{&quot;baz&quot;:&quot;qux&quot;}"[\s\>]/', $tag);
135
    }
136
}
137