Skip to content
This repository was archived by the owner on Sep 23, 2022. It is now read-only.

Commit 41aaed6

Browse files
committed
Add unit tests on the TranscludeCompiler.
1 parent 520f434 commit 41aaed6

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
namespace CHStudio\LaravelTransclude\Tests\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use CHStudio\LaravelTransclude\TranscludeCompiler as SUT;
7+
8+
class TranscludeCompilerTest extends TestCase
9+
{
10+
public function testNameDefault()
11+
{
12+
$sut = new SUT;
13+
$this->assertEquals('__transcluder', $sut->getName());
14+
}
15+
16+
public function testNameSet()
17+
{
18+
$sut = new SUT('aSuperName');
19+
$this->assertEquals('aSuperName', $sut->getName());
20+
}
21+
22+
public function testTranscludeDirectiveOutput()
23+
{
24+
$sut = new SUT;
25+
$this->assertEquals(
26+
"<?php \$__transcluder->startTranscluding(); ?>",
27+
$sut->compileTransclude('view')
28+
);
29+
}
30+
31+
public function giveExpressions()
32+
{
33+
return [
34+
['view'],
35+
["'view'"],
36+
["'elements', ['heading' => 'title', 'value' => 'other']"]
37+
];
38+
}
39+
40+
/**
41+
* @dataProvider giveExpressions
42+
*/
43+
public function testTranscludeDirectiveAddViewToTheStack($expression)
44+
{
45+
$sut = new SUT;
46+
$sut->compileTransclude($expression);
47+
$reflection = (new \ReflectionClass($sut))->getProperty('transcludeStack');
48+
$reflection->setAccessible(true);
49+
50+
$this->assertEquals([$expression], $reflection->getValue($sut));
51+
}
52+
53+
public function testTranscludeDirectiveWithParenthesisAddViewToTheStack()
54+
{
55+
$sut = new SUT;
56+
$sut->compileTransclude('(view)');
57+
$reflection = (new \ReflectionClass($sut))->getProperty('transcludeStack');
58+
$reflection->setAccessible(true);
59+
60+
$this->assertEquals(['view'], $reflection->getValue($sut));
61+
}
62+
63+
public function testTranscludedDirectiveOutput()
64+
{
65+
$sut = new SUT;
66+
$this->assertEquals(
67+
"<?php echo \$__transcluder->echoLatestTranscluded(); ?>",
68+
$sut->compileTranscluded()
69+
);
70+
}
71+
72+
/**
73+
* @expectedException \CHStudio\LaravelTransclude\Exceptions\TranscludeNotStarted
74+
*/
75+
public function testEndTranscludeDirectiveCantBeCalledBeforeStarting()
76+
{
77+
$sut = new SUT;
78+
$sut->compileEndTransclude();
79+
}
80+
81+
/**
82+
* @dataProvider giveExpressions
83+
*/
84+
public function testEndTranscludeDirectiveOutput($expression)
85+
{
86+
$sut = new SUT;
87+
$sut->compileTransclude($expression);
88+
$output = $sut->compileEndTransclude();
89+
90+
$this->assertContains(
91+
"\$__transcluder->endTranscluding();",
92+
$output
93+
);
94+
$this->assertContains(
95+
"->make($expression, array_except(get_defined_vars(), array('__data', '__path')))",
96+
$output
97+
);
98+
$this->assertContains(
99+
"->render()",
100+
$output
101+
);
102+
}
103+
104+
public function testStartTranscluding()
105+
{
106+
$level = ob_get_level();
107+
$sut = new SUT();
108+
$sut->startTranscluding();
109+
$this->assertEquals($level + 1, ob_get_level());
110+
$sut->endTranscluding();
111+
$this->assertEquals($level, ob_get_level());
112+
}
113+
114+
public function testEndTranscluding()
115+
{
116+
$sut = new SUT();
117+
$sut->startTranscluding();
118+
echo "in buffer";
119+
$output = $sut->endTranscluding();
120+
121+
$reflection = (new \ReflectionClass($sut))->getProperty('transcludedContent');
122+
$reflection->setAccessible(true);
123+
124+
$this->assertEquals(['in buffer'], $reflection->getValue($sut));
125+
}
126+
127+
/**
128+
* @expectedException \CHStudio\LaravelTransclude\Exceptions\MissingTranscludeDirective
129+
*/
130+
public function testEchoLatestTranscludedWithoutTransclusion()
131+
{
132+
$sut = new SUT();
133+
134+
$sut->echoLatestTranscluded();
135+
}
136+
137+
public function testEchoLatestTranscluded()
138+
{
139+
$sut = new SUT();
140+
$sut->startTranscluding();
141+
echo "in buffer";
142+
$sut->endTranscluding();
143+
$sut->startTranscluding();
144+
echo "in buffer 2";
145+
$sut->endTranscluding();
146+
147+
$this->assertEquals('in buffer 2', $sut->echoLatestTranscluded());
148+
$this->assertEquals('in buffer', $sut->echoLatestTranscluded());
149+
}
150+
}

0 commit comments

Comments
 (0)