PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/View/ViewBladeCompilerTest.php

https://github.com/luukholleman/framework
PHP | 460 lines | 369 code | 91 blank | 0 comment | 0 complexity | 9c81151ddb4edff22fa7537d9f531a2a MD5 | raw file
  1. <?php
  2. use Mockery as m;
  3. use Illuminate\View\Compilers\BladeCompiler;
  4. class ViewBladeCompilerTest extends PHPUnit_Framework_TestCase {
  5. public function tearDown()
  6. {
  7. m::close();
  8. }
  9. public function testIsExpiredReturnsTrueIfCompiledFileDoesntExist()
  10. {
  11. $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
  12. $files->shouldReceive('exists')->once()->with(__DIR__.'/'.md5('foo'))->andReturn(false);
  13. $this->assertTrue($compiler->isExpired('foo'));
  14. }
  15. public function testIsExpiredReturnsTrueIfCachePathIsNull()
  16. {
  17. $compiler = new BladeCompiler($files = $this->getFiles(), null);
  18. $files->shouldReceive('exists')->never();
  19. $this->assertTrue($compiler->isExpired('foo'));
  20. }
  21. public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
  22. {
  23. $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
  24. $files->shouldReceive('exists')->once()->with(__DIR__.'/'.md5('foo'))->andReturn(true);
  25. $files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100);
  26. $files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.md5('foo'))->andReturn(0);
  27. $this->assertTrue($compiler->isExpired('foo'));
  28. }
  29. public function testCompilePathIsProperlyCreated()
  30. {
  31. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  32. $this->assertEquals(__DIR__.'/'.md5('foo'), $compiler->getCompiledPath('foo'));
  33. }
  34. public function testCompileCompilesFileAndReturnsContents()
  35. {
  36. $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
  37. $files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
  38. $files->shouldReceive('put')->once()->with(__DIR__.'/'.md5('foo'), 'Hello World');
  39. $compiler->compile('foo');
  40. }
  41. public function testCompileCompilesAndGetThePath()
  42. {
  43. $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
  44. $files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
  45. $files->shouldReceive('put')->once()->with(__DIR__.'/'.md5('foo'), 'Hello World');
  46. $compiler->compile('foo');
  47. $this->assertEquals('foo', $compiler->getPath());
  48. }
  49. public function testCompileSetAndGetThePath()
  50. {
  51. $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
  52. $compiler->setPath('foo');
  53. $this->assertEquals('foo', $compiler->getPath());
  54. }
  55. public function testCompileDoesntStoreFilesWhenCachePathIsNull()
  56. {
  57. $compiler = new BladeCompiler($files = $this->getFiles(), null);
  58. $files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
  59. $files->shouldReceive('put')->never();
  60. $compiler->compile('foo');
  61. }
  62. public function testEchosAreCompiled()
  63. {
  64. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  65. $this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{{$name}}}'));
  66. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{$name}}'));
  67. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{ $name }}'));
  68. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{
  69. $name
  70. }}'));
  71. $this->assertEquals("<?php echo \$name; ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
  72. $this->assertEquals("<?php echo \$name; ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));
  73. $this->assertEquals("<?php echo \$name; ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
  74. $this->assertEquals("<?php echo \$name; ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));
  75. $this->assertEquals('<?php echo isset($name) ? $name : "foo"; ?>', $compiler->compileString('{{ $name or "foo" }}'));
  76. $this->assertEquals('<?php echo isset($user->name) ? $user->name : "foo"; ?>', $compiler->compileString('{{ $user->name or "foo" }}'));
  77. $this->assertEquals('<?php echo isset($name) ? $name : "foo"; ?>', $compiler->compileString('{{$name or "foo"}}'));
  78. $this->assertEquals('<?php echo isset($name) ? $name : "foo"; ?>', $compiler->compileString('{{
  79. $name or "foo"
  80. }}'));
  81. $this->assertEquals('<?php echo isset($name) ? $name : \'foo\'; ?>', $compiler->compileString('{{ $name or \'foo\' }}'));
  82. $this->assertEquals('<?php echo isset($name) ? $name : \'foo\'; ?>', $compiler->compileString('{{$name or \'foo\'}}'));
  83. $this->assertEquals('<?php echo isset($name) ? $name : \'foo\'; ?>', $compiler->compileString('{{
  84. $name or \'foo\'
  85. }}'));
  86. $this->assertEquals('<?php echo isset($age) ? $age : 90; ?>', $compiler->compileString('{{ $age or 90 }}'));
  87. $this->assertEquals('<?php echo isset($age) ? $age : 90; ?>', $compiler->compileString('{{$age or 90}}'));
  88. $this->assertEquals('<?php echo isset($age) ? $age : 90; ?>', $compiler->compileString('{{
  89. $age or 90
  90. }}'));
  91. $this->assertEquals('<?php echo "Hello world or foo"; ?>', $compiler->compileString('{{ "Hello world or foo" }}'));
  92. $this->assertEquals('<?php echo "Hello world or foo"; ?>', $compiler->compileString('{{"Hello world or foo"}}'));
  93. $this->assertEquals('<?php echo $foo + $or + $baz; ?>', $compiler->compileString('{{$foo + $or + $baz}}'));
  94. $this->assertEquals('<?php echo "Hello world or foo"; ?>', $compiler->compileString('{{
  95. "Hello world or foo"
  96. }}'));
  97. $this->assertEquals('<?php echo \'Hello world or foo\'; ?>', $compiler->compileString('{{ \'Hello world or foo\' }}'));
  98. $this->assertEquals('<?php echo \'Hello world or foo\'; ?>', $compiler->compileString('{{\'Hello world or foo\'}}'));
  99. $this->assertEquals('<?php echo \'Hello world or foo\'; ?>', $compiler->compileString('{{
  100. \'Hello world or foo\'
  101. }}'));
  102. $this->assertEquals('<?php echo myfunc(\'foo or bar\'); ?>', $compiler->compileString('{{ myfunc(\'foo or bar\') }}'));
  103. $this->assertEquals('<?php echo myfunc("foo or bar"); ?>', $compiler->compileString('{{ myfunc("foo or bar") }}'));
  104. $this->assertEquals('<?php echo myfunc("$name or \'foo\'"); ?>', $compiler->compileString('{{ myfunc("$name or \'foo\'") }}'));
  105. }
  106. public function testEscapedWithAtEchosAreCompiled()
  107. {
  108. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  109. $this->assertEquals('{{$name}}', $compiler->compileString('@{{$name}}'));
  110. $this->assertEquals('{{ $name }}', $compiler->compileString('@{{ $name }}'));
  111. $this->assertEquals('{{
  112. $name
  113. }}',
  114. $compiler->compileString('@{{
  115. $name
  116. }}'));
  117. $this->assertEquals('{{ $name }}
  118. ',
  119. $compiler->compileString('@{{ $name }}
  120. '));
  121. }
  122. public function testReversedEchosAreCompiled()
  123. {
  124. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  125. $compiler->setEscapedContentTags('{{', '}}');
  126. $compiler->setContentTags('{{{', '}}}');
  127. $this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{$name}}'));
  128. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{{$name}}}'));
  129. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{{ $name }}}'));
  130. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{{
  131. $name
  132. }}}'));
  133. }
  134. public function testExtendsAreCompiled()
  135. {
  136. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  137. $string = '@extends(\'foo\')
  138. test';
  139. $expected = "test".PHP_EOL.'<?php echo $__env->make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
  140. $this->assertEquals($expected, $compiler->compileString($string));
  141. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  142. $string = '@extends(name(foo))'.PHP_EOL.'test';
  143. $expected = "test".PHP_EOL.'<?php echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
  144. $this->assertEquals($expected, $compiler->compileString($string));
  145. }
  146. public function testPushIsCompiled()
  147. {
  148. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  149. $string = '@push(\'foo\')
  150. test
  151. @endpush';
  152. $expected = '<?php $__env->startSection(\'foo\'); ?>
  153. test
  154. <?php $__env->appendSection(); ?>';
  155. $this->assertEquals($expected, $compiler->compileString($string));
  156. }
  157. public function testStackIsCompiled()
  158. {
  159. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  160. $string = '@stack(\'foo\')';
  161. $expected = '<?php echo $__env->yieldContent(\'foo\'); ?>';
  162. $this->assertEquals($expected, $compiler->compileString($string));
  163. }
  164. public function testCommentsAreCompiled()
  165. {
  166. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  167. $string = '{{--this is a comment--}}';
  168. $expected = '<?php /*this is a comment*/ ?>';
  169. $this->assertEquals($expected, $compiler->compileString($string));
  170. $string = '{{--
  171. this is a comment
  172. --}}';
  173. $expected = '<?php /*
  174. this is a comment
  175. */ ?>';
  176. $this->assertEquals($expected, $compiler->compileString($string));
  177. }
  178. public function testIfStatementsAreCompiled()
  179. {
  180. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  181. $string = '@if (name(foo(bar)))
  182. breeze
  183. @endif';
  184. $expected = '<?php if(name(foo(bar))): ?>
  185. breeze
  186. <?php endif; ?>';
  187. $this->assertEquals($expected, $compiler->compileString($string));
  188. }
  189. public function testElseStatementsAreCompiled()
  190. {
  191. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  192. $string = '@if (name(foo(bar)))
  193. breeze
  194. @else
  195. boom
  196. @endif';
  197. $expected = '<?php if(name(foo(bar))): ?>
  198. breeze
  199. <?php else: ?>
  200. boom
  201. <?php endif; ?>';
  202. $this->assertEquals($expected, $compiler->compileString($string));
  203. }
  204. public function testElseIfStatementsAreCompiled()
  205. {
  206. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  207. $string = '@if(name(foo(bar)))
  208. breeze
  209. @elseif(boom(breeze))
  210. boom
  211. @endif';
  212. $expected = '<?php if(name(foo(bar))): ?>
  213. breeze
  214. <?php elseif(boom(breeze)): ?>
  215. boom
  216. <?php endif; ?>';
  217. $this->assertEquals($expected, $compiler->compileString($string));
  218. }
  219. public function testUnlessStatementsAreCompiled()
  220. {
  221. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  222. $string = '@unless (name(foo(bar)))
  223. breeze
  224. @endunless';
  225. $expected = '<?php if ( ! (name(foo(bar)))): ?>
  226. breeze
  227. <?php endif; ?>';
  228. $this->assertEquals($expected, $compiler->compileString($string));
  229. }
  230. public function testForelseStatementsAreCompiled()
  231. {
  232. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  233. $string = '@forelse ($this->getUsers() as $user)
  234. breeze
  235. @empty
  236. empty
  237. @endforelse';
  238. $expected = '<?php $__empty_1 = true; foreach($this->getUsers() as $user): $__empty_1 = false; ?>
  239. breeze
  240. <?php endforeach; if ($__empty_1): ?>
  241. empty
  242. <?php endif; ?>';
  243. $this->assertEquals($expected, $compiler->compileString($string));
  244. }
  245. public function testNestedForelseStatementsAreCompiled()
  246. {
  247. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  248. $string = '@forelse ($this->getUsers() as $user)
  249. @forelse ($user->tags as $tag)
  250. breeze
  251. @empty
  252. tag empty
  253. @endforelse
  254. @empty
  255. empty
  256. @endforelse';
  257. $expected = '<?php $__empty_1 = true; foreach($this->getUsers() as $user): $__empty_1 = false; ?>
  258. <?php $__empty_2 = true; foreach($user->tags as $tag): $__empty_2 = false; ?>
  259. breeze
  260. <?php endforeach; if ($__empty_2): ?>
  261. tag empty
  262. <?php endif; ?>
  263. <?php endforeach; if ($__empty_1): ?>
  264. empty
  265. <?php endif; ?>';
  266. $this->assertEquals($expected, $compiler->compileString($string));
  267. }
  268. public function testStatementThatContainsNonConsecutiveParanthesisAreCompiled()
  269. {
  270. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  271. $string = "Foo @lang(function_call('foo(blah)')) bar";
  272. $expected = "Foo <?php echo \Illuminate\Support\Facades\Lang::get(function_call('foo(blah)')); ?> bar";
  273. $this->assertEquals($expected, $compiler->compileString($string));
  274. }
  275. public function testIncludesAreCompiled()
  276. {
  277. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  278. $this->assertEquals('<?php echo $__env->make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(\'foo\')'));
  279. $this->assertEquals('<?php echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(name(foo))'));
  280. }
  281. public function testShowEachAreCompiled()
  282. {
  283. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  284. $this->assertEquals('<?php echo $__env->renderEach(\'foo\', \'bar\'); ?>', $compiler->compileString('@each(\'foo\', \'bar\')'));
  285. $this->assertEquals('<?php echo $__env->renderEach(name(foo)); ?>', $compiler->compileString('@each(name(foo))'));
  286. }
  287. public function testYieldsAreCompiled()
  288. {
  289. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  290. $this->assertEquals('<?php echo $__env->yieldContent(\'foo\'); ?>', $compiler->compileString('@yield(\'foo\')'));
  291. $this->assertEquals('<?php echo $__env->yieldContent(\'foo\', \'bar\'); ?>', $compiler->compileString('@yield(\'foo\', \'bar\')'));
  292. $this->assertEquals('<?php echo $__env->yieldContent(name(foo)); ?>', $compiler->compileString('@yield(name(foo))'));
  293. }
  294. public function testShowsAreCompiled()
  295. {
  296. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  297. $this->assertEquals('<?php echo $__env->yieldSection(); ?>', $compiler->compileString('@show'));
  298. }
  299. public function testLanguageAndChoicesAreCompiled()
  300. {
  301. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  302. $this->assertEquals('<?php echo \Illuminate\Support\Facades\Lang::get(\'foo\'); ?>', $compiler->compileString("@lang('foo')"));
  303. $this->assertEquals('<?php echo \Illuminate\Support\Facades\Lang::choice(\'foo\', 1); ?>', $compiler->compileString("@choice('foo', 1)"));
  304. }
  305. public function testSectionStartsAreCompiled()
  306. {
  307. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  308. $this->assertEquals('<?php $__env->startSection(\'foo\'); ?>', $compiler->compileString('@section(\'foo\')'));
  309. $this->assertEquals('<?php $__env->startSection(name(foo)); ?>', $compiler->compileString('@section(name(foo))'));
  310. }
  311. public function testStopSectionsAreCompiled()
  312. {
  313. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  314. $this->assertEquals('<?php $__env->stopSection(); ?>', $compiler->compileString('@stop'));
  315. }
  316. public function testEndSectionsAreCompiled()
  317. {
  318. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  319. $this->assertEquals('<?php $__env->stopSection(); ?>', $compiler->compileString('@endsection'));
  320. }
  321. public function testAppendSectionsAreCompiled()
  322. {
  323. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  324. $this->assertEquals('<?php $__env->appendSection(); ?>', $compiler->compileString('@append'));
  325. }
  326. public function testCustomPhpCodeIsCorrectlyHandled()
  327. {
  328. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  329. $this->assertEquals('<?php if($test): ?> <?php @show(\'test\'); ?> <?php endif; ?>', $compiler->compileString("@if(\$test) <?php @show('test'); ?> @endif"));
  330. }
  331. public function testMixingYieldAndEcho()
  332. {
  333. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  334. $this->assertEquals('<?php echo $__env->yieldContent(\'title\'); ?> - <?php echo Config::get(\'site.title\'); ?>', $compiler->compileString("@yield('title') - {{Config::get('site.title')}}"));
  335. }
  336. public function testCustomExtensionsAreCompiled()
  337. {
  338. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  339. $compiler->extend(function($value) { return str_replace('foo', 'bar', $value); });
  340. $this->assertEquals('bar', $compiler->compileString('foo'));
  341. }
  342. public function testConfiguringContentTags()
  343. {
  344. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  345. $compiler->setContentTags('[[', ']]');
  346. $compiler->setEscapedContentTags('[[[', ']]]');
  347. $this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('[[[ $name ]]]'));
  348. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('[[ $name ]]'));
  349. $this->assertEquals('<?php echo $name; ?>', $compiler->compileString('[[
  350. $name
  351. ]]'));
  352. }
  353. public function testExpressionsOnTheSameLine()
  354. {
  355. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  356. $this->assertEquals('<?php echo \Illuminate\Support\Facades\Lang::get(foo(bar(baz(qux(breeze()))))); ?> space () <?php echo \Illuminate\Support\Facades\Lang::get(foo(bar)); ?>', $compiler->compileString('@lang(foo(bar(baz(qux(breeze()))))) space () @lang(foo(bar))'));
  357. }
  358. public function testExpressionWithinHTML()
  359. {
  360. $compiler = new BladeCompiler($this->getFiles(), __DIR__);
  361. $this->assertEquals('<html <?php echo $foo; ?>>', $compiler->compileString('<html {{ $foo }}>'));
  362. $this->assertEquals('<html<?php echo $foo; ?>>', $compiler->compileString('<html{{ $foo }}>'));
  363. $this->assertEquals('<html <?php echo $foo; ?> <?php echo \Illuminate\Support\Facades\Lang::get(\'foo\'); ?>>', $compiler->compileString('<html {{ $foo }} @lang(\'foo\')>'));
  364. }
  365. protected function getFiles()
  366. {
  367. return m::mock('Illuminate\Filesystem\Filesystem');
  368. }
  369. }