Skip to content

Commit dc537e4

Browse files
author
Tortue Torche
committed
Add some tests for the Blade directives.
1 parent a53c638 commit dc537e4

File tree

3 files changed

+215
-31
lines changed

3 files changed

+215
-31
lines changed

src/Efficiently/JqueryLaravel/BladeExtensions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function registerContentTags()
2626
];
2727

2828
$contentTagsPattern = implode("|", $contentTags);
29-
// Convert @<tag>_tag to @content_tag('<tag>') extension
29+
// Convert @tag_tag to @content_tag('tag') extension
3030
// E.g. "@div_tag blabla @end_div_tag" becomes "@content_tag('div') blabla @end_contag_tag('div')"
3131
$contentTagsPattern = '/@('.$contentTagsPattern.')_(tag)(\(([^\r\n]*?)\)|[\r\n\s\t]*)([\r\n\s\t]+)/';
3232
if (preg_match($contentTagsPattern, $view, $result)) {
@@ -37,16 +37,16 @@ public function registerContentTags()
3737
}
3838

3939
$contentTagsPattern = implode("|", $contentTags);
40-
// Convert @<tag>_for to @content_tag_for(object, '<tag>') extension
41-
// E.g. "@div_for(object) blabla" becomes "@content_tag_for(object, 'div') blabla"
40+
// Convert @tag_for to @content_tag_for('tag', object) extension
41+
// E.g. "@div_for(object) blabla" becomes "@content_tag_for('div', object) blabla"
4242
$contentTagsPattern = '/@('.$contentTagsPattern.')_(tag_for|for)\((.+)\)([\r\n\s\t]+)/';
4343
if (preg_match($contentTagsPattern, $view)) {
4444
$replacement = '@content_tag_for("$1", $3)$4';
4545
$view = preg_replace($contentTagsPattern, $replacement, $view);
4646
}
4747

4848
$contentTagsPattern = implode("|", $contentTags);
49-
// Convert @end_<tag>_tag and @end_<tag>_for to @endcontent_tag('<tag>') and @endcontent_tag_for('<tag>') extensions
49+
// Convert @end_tag_tag and @end_tag_for to @endcontent_tag('tag') and @endcontent_tag_for('tag') extensions
5050
// E.g. "blabla @enddiv_for" becomes "blabla @endcontag_tag_for('div')"
5151
$contentTagsPattern = '/@end_?('.$contentTagsPattern.')_(tag|tag_for|for)([\r\n\s\t]+)/';
5252
if (preg_match($contentTagsPattern, $view, $matches)) {

tests/JqlBladeExtensionsTest.php

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
3+
use Mockery as m;
4+
5+
class JqlBladeExtensionsTest extends JqlTestCase
6+
{
7+
public function testContentTag()
8+
{
9+
$contentTag = <<<EOT
10+
@content_tag('div')
11+
text
12+
@endcontent_tag('div')
13+
14+
EOT;
15+
$expect = <<<EOT
16+
<?php echo '<'.'div'.app('html')->attributes([]).'>';
17+
?>text
18+
<?php echo '</'.'div'.'>';
19+
?>
20+
EOT;
21+
$view = Blade::compileString($contentTag);
22+
$this->assertEquals($expect, $view);
23+
24+
$expect2 = <<<EOT
25+
<?php echo '<'.'div'.app('html')->attributes([]).'>'; ?>text2 <?php echo '</'.'div'.'>';
26+
?>
27+
EOT;
28+
$this->assertNotEquals($expect2, $view);
29+
30+
$contentTag2 = <<<EOT
31+
@content_tag('div') text2 @end_content_tag('div')
32+
33+
EOT;
34+
$view2 = Blade::compileString($contentTag2);
35+
$this->assertEquals($expect2, $view2);
36+
}
37+
38+
public function testContentTagWithAttributes()
39+
{
40+
$contentTag = <<<EOT
41+
@content_tag('div', ['class' => 'div-tag-class'])
42+
text
43+
@endcontent_tag('div')
44+
45+
EOT;
46+
$expect = <<<EOT
47+
<?php echo '<'.'div'.app('html')->attributes(['class' => 'div-tag-class']).'>';
48+
?>text
49+
<?php echo '</'.'div'.'>';
50+
?>
51+
EOT;
52+
$view = Blade::compileString($contentTag);
53+
$this->assertEquals($expect, $view);
54+
55+
$expect2 = <<<EOT
56+
<?php echo '<'.'div'.app('html')->attributes(['id' => 'div_tag_id']).'>'; ?>text2 <?php echo '</'.'div'.'>';
57+
?>
58+
EOT;
59+
$this->assertNotEquals($expect2, $view);
60+
61+
$contentTag2 = <<<EOT
62+
@content_tag('div', ['id' => 'div_tag_id']) text2 @end_content_tag('div')
63+
64+
EOT;
65+
$view2 = Blade::compileString($contentTag2);
66+
$this->assertEquals($expect2, $view2);
67+
}
68+
69+
public function testContentTagFor()
70+
{
71+
// Existing Project
72+
$project = new Jql\Project;
73+
$project->id = 3;
74+
$project->exists = true;
75+
76+
$contentTagFor = <<<EOT
77+
@content_tag_for('div', \$project)
78+
text
79+
@endcontent_tag_for('div')
80+
81+
EOT;
82+
$expect = <<<EOT
83+
<?php
84+
\$projectRecord = \$project;
85+
if (! is_a(\$projectRecord, "\Illuminate\Support\Collection")) {
86+
\$projectRecord = new \Illuminate\Support\Collection([\$projectRecord]);
87+
}
88+
\$projectIndex = -1;// -1 because we increment index at the beginnning of the loop
89+
\$projectRecord->each(function(\$project) use(\$__env, &\$projectIndex){
90+
\$projectIndex++;
91+
\$options = (array) [];
92+
\$options['class'] = implode(" ",
93+
array_filter([dom_class(\$project, null), array_get(\$options, 'class')])
94+
);
95+
\$options['id'] = dom_id(\$project, null);
96+
echo '<'.'div'.app('html')->attributes(\$options).'>';
97+
?>text
98+
<?php
99+
echo '</'.'div'.'>';
100+
101+
});
102+
?>
103+
EOT;
104+
$view = Blade::compileString($contentTagFor);
105+
$this->assertEquals($expect, $view);
106+
}
107+
108+
public function testContentTagForWithOptions()
109+
{
110+
// Existing Project
111+
$project = new Jql\Project;
112+
$project->id = 3;
113+
$project->exists = true;
114+
115+
$contentTagFor = <<<EOT
116+
@content_tag_for('div', \$project, 'edit_prefix', ['remote' => 'true'])
117+
text
118+
@endcontent_tag_for('div')
119+
120+
EOT;
121+
$expect = <<<EOT
122+
<?php
123+
\$projectRecord = \$project;
124+
if (! is_a(\$projectRecord, "\Illuminate\Support\Collection")) {
125+
\$projectRecord = new \Illuminate\Support\Collection([\$projectRecord]);
126+
}
127+
\$projectIndex = -1;// -1 because we increment index at the beginnning of the loop
128+
\$projectRecord->each(function(\$project) use(\$__env, &\$projectIndex){
129+
\$projectIndex++;
130+
\$options = (array) ['remote' => 'true'];
131+
\$options['class'] = implode(" ",
132+
array_filter([dom_class(\$project, 'edit_prefix'), array_get(\$options, 'class')])
133+
);
134+
\$options['id'] = dom_id(\$project, 'edit_prefix');
135+
echo '<'.'div'.app('html')->attributes(\$options).'>';
136+
?>text
137+
<?php
138+
echo '</'.'div'.'>';
139+
140+
});
141+
?>
142+
EOT;
143+
$view = Blade::compileString($contentTagFor);
144+
$this->assertEquals($expect, $view);
145+
}
146+
147+
148+
public function testDivTag()
149+
{
150+
$divTag = <<<EOT
151+
@div_tag
152+
text
153+
@enddiv_tag
154+
155+
EOT;
156+
$expect = <<<EOT
157+
<?php echo '<'."div".app('html')->attributes([]).'>';
158+
?>text
159+
<?php echo '</'."div".'>';
160+
?>
161+
EOT;
162+
$view = Blade::compileString($divTag);
163+
$this->assertEquals($expect, $view);
164+
165+
$expect2 = <<<EOT
166+
<?php echo '<'."div".app('html')->attributes([]).'>'; ?>text2 <?php echo '</'."div".'>';
167+
?>
168+
EOT;
169+
$this->assertNotEquals($expect2, $view);
170+
171+
$divTag2 = <<<EOT
172+
@div_tag text2 @end_div_tag
173+
174+
EOT;
175+
$view2 = Blade::compileString($divTag2);
176+
$this->assertEquals($expect2, $view2);
177+
}
178+
179+
public function testDivTagWithAttributes()
180+
{
181+
$divTag = <<<EOT
182+
@div_tag(['class' => 'div-tag-class'])
183+
text
184+
@enddiv_tag
185+
186+
EOT;
187+
$expect = <<<EOT
188+
<?php echo '<'."div".app('html')->attributes(['class' => 'div-tag-class']).'>';
189+
?>text
190+
<?php echo '</'."div".'>';
191+
?>
192+
EOT;
193+
$view = Blade::compileString($divTag);
194+
$this->assertEquals($expect, $view);
195+
196+
$expect2 = <<<EOT
197+
<?php echo '<'."div".app('html')->attributes(['id' => 'div_tag_id']).'>'; ?>text2 <?php echo '</'."div".'>';
198+
?>
199+
EOT;
200+
$this->assertNotEquals($expect2, $view);
201+
202+
$divTag2 = <<<EOT
203+
@div_tag(['id' => 'div_tag_id']) text2 @end_div_tag
204+
205+
EOT;
206+
$view2 = Blade::compileString($divTag2);
207+
$this->assertEquals($expect2, $view2);
208+
}
209+
}

tests/JqlTestCase.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,11 @@ public function tearDown()
1313
m::close();
1414
}
1515

16-
protected function mock($className)
17-
{
18-
$mock = m::mock($className);
19-
App::bind($className, function ($app, $parameters = []) use ($mock) {
20-
if (is_array($parameters) && is_array($attributes = array_get($parameters, 0, [])) && respond_to($mock, "fill")) {
21-
$mock = $this->fillMock($mock, $attributes);
22-
}
23-
24-
return $mock;
25-
});
26-
27-
return $mock;
28-
}
29-
30-
protected function fillMock($mock, $attributes = [])
31-
{
32-
$instance = $mock->makePartial();
33-
foreach ($attributes as $key => $value) {
34-
$instance->$key = $value;
35-
}
36-
37-
return $instance;
38-
}
39-
4016
protected function createSession()
4117
{
42-
// Fake session
18+
// Mock session
4319
app('request')->setSession(
44-
new \Illuminate\Session\Store(
20+
new Illuminate\Session\Store(
4521
'name',
4622
m::mock('SessionHandlerInterface'),
4723
'aaaa'
@@ -64,7 +40,6 @@ protected function getPackageAliases($app)
6440
'Form' => 'Collective\Html\FormFacade',
6541
'HTML' => 'Collective\Html\HtmlFacade',
6642
'Former' => 'Former\Facades\Former',
67-
// 'HTMLEloquentHelper' => 'Efficiently\JqueryLaravel\Facades\HTMLEloquentHelper',
6843
];
6944
}
7045
}

0 commit comments

Comments
 (0)