MustacheSpecTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2017 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A PHPUnit test case wrapping the Mustache Spec.
  12. *
  13. * @group mustache-spec
  14. * @group functional
  15. */
  16. class Mustache_Test_FiveThree_Functional_MustacheSpecTest extends Mustache_Test_SpecTestCase
  17. {
  18. /**
  19. * For some reason data providers can't mark tests skipped, so this test exists
  20. * simply to provide a 'skipped' test if the `spec` submodule isn't initialized.
  21. */
  22. public function testSpecInitialized()
  23. {
  24. if (!file_exists(dirname(__FILE__) . '/../../../../../vendor/spec/specs/')) {
  25. $this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
  26. }
  27. }
  28. /**
  29. * @group lambdas
  30. * @dataProvider loadLambdasSpec
  31. */
  32. public function testLambdasSpec($desc, $source, $partials, $data, $expected)
  33. {
  34. $template = self::loadTemplate($source, $partials);
  35. $this->assertEquals($expected, $template($this->prepareLambdasSpec($data)), $desc);
  36. }
  37. public function loadLambdasSpec()
  38. {
  39. return $this->loadSpec('~lambdas');
  40. }
  41. /**
  42. * Extract and lambdafy any 'lambda' values found in the $data array.
  43. */
  44. private function prepareLambdasSpec($data)
  45. {
  46. foreach ($data as $key => $val) {
  47. if ($key === 'lambda') {
  48. if (!isset($val['php'])) {
  49. $this->markTestSkipped(sprintf('PHP lambda test not implemented for this test.'));
  50. }
  51. $func = $val['php'];
  52. $data[$key] = function ($text = null) use ($func) {
  53. return eval($func);
  54. };
  55. } elseif (is_array($val)) {
  56. $data[$key] = $this->prepareLambdasSpec($val);
  57. }
  58. }
  59. return $data;
  60. }
  61. }