ProcedureValidatorTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Tests\Integration\Procedure;
  9. use Symfony\Component\Config\FileLocator;
  10. use JonnyW\PhantomJs\Client;
  11. use JonnyW\PhantomJs\Procedure\Procedure;
  12. use JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface;
  13. use JonnyW\PhantomJs\Procedure\ProcedureValidator;
  14. use JonnyW\PhantomJs\Validator\Esprima;
  15. use JonnyW\PhantomJs\Validator\EngineInterface;
  16. /**
  17. * PHP PhantomJs
  18. *
  19. * @author Jon Wenmoth <contact@jonnyw.me>
  20. */
  21. class ProcedureValidatorTest extends \PHPUnit_Framework_TestCase
  22. {
  23. /** +++++++++++++++++++++++++++++++++++ **/
  24. /** ++++++++++++++ TESTS ++++++++++++++ **/
  25. /** +++++++++++++++++++++++++++++++++++ **/
  26. /**
  27. * Test syntax exception is
  28. * thrown if procedure contains
  29. * syntax error.
  30. *
  31. * @access public
  32. * @return void
  33. */
  34. public function testProcedureSyntaxExceptionIsThrownIfProcedureContainsSyntaxError()
  35. {
  36. $this->setExpectedException('\JonnyW\PhantomJs\Exception\SyntaxException');
  37. $procedureLoader = $this->getProcedureLoader();
  38. $esprima = $this->getEsprima();
  39. $validator = $this->getValidator($procedureLoader, $esprima);
  40. $validator->validate('return false; var');
  41. }
  42. /**
  43. * Test syntax exception contains errors.
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. public function testSyntaxExceptionContainsErrors()
  49. {
  50. $procedureLoader = $this->getProcedureLoader();
  51. $esprima = $this->getEsprima();
  52. try {
  53. $validator = $this->getValidator($procedureLoader, $esprima);
  54. $validator->validate('return false; var');
  55. } catch (\JonnyW\PhantomJs\Exception\SyntaxException $e) {
  56. $this->assertNotEmpty($e->getErrors());
  57. }
  58. }
  59. /**
  60. * Test requirement exception is thrown
  61. * if procedure does not contain phantom
  62. * exit statement.
  63. *
  64. * @access public
  65. * @return void
  66. */
  67. public function testRequirementExceptionIsThrownIfProcedureDoesNotContainPhanomtExitStatement()
  68. {
  69. $this->setExpectedException('\JonnyW\PhantomJs\Exception\RequirementException');
  70. $procedureLoader = $this->getProcedureLoader();
  71. $esprima = $this->getEsprima();
  72. $validator = $this->getValidator($procedureLoader, $esprima);
  73. $validator->validate('var test = function () { console.log("ok"); }');
  74. }
  75. /**
  76. * Test true is returned if procedure is valid
  77. *
  78. * @access public
  79. * @return void
  80. */
  81. public function testTrueIsReturnedIfProcedureIsValid()
  82. {
  83. $procedureLoader = $this->getProcedureLoader();
  84. $esprima = $this->getEsprima();
  85. $validator = $this->getValidator($procedureLoader, $esprima);
  86. $this->assertTrue($validator->validate('var test = function () { console.log("ok"); }; phantom.exit(1);'));
  87. }
  88. /**
  89. * Test procedure is valid if procedure
  90. * has comments.
  91. *
  92. * @access public
  93. * @return void
  94. */
  95. public function testProcedureIsValidIfProcedureHasComments()
  96. {
  97. $procedureLoader = $this->getProcedureLoader();
  98. $esprima = $this->getEsprima();
  99. $validator = $this->getValidator($procedureLoader, $esprima);
  100. $this->assertTrue($validator->validate('/** * Test comment **/ var test = function () { console.log("ok"); }; phantom.exit(1);'));
  101. }
  102. /** +++++++++++++++++++++++++++++++++++ **/
  103. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  104. /** +++++++++++++++++++++++++++++++++++ **/
  105. /**
  106. * Get procedure validator.
  107. *
  108. * @access protected
  109. * @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface $procedureLoader
  110. * @param \JonnyW\PhantomJs\Validator\EngineInterface $engine
  111. * @return \JonnyW\PhantomJs\Procedure\ProcedureValidator
  112. */
  113. protected function getValidator(ProcedureLoaderInterface $procedureLoader, EngineInterface $engine)
  114. {
  115. $validator = new ProcedureValidator($procedureLoader, $engine);
  116. return $validator;
  117. }
  118. /**
  119. * Get procedure loader.
  120. *
  121. * @access protected
  122. * @return \JonnyW\PhantomJs\Procedure\ProcedureLoader
  123. */
  124. protected function getProcedureLoader()
  125. {
  126. return Client::getInstance()->getProcedureLoader();
  127. }
  128. /**
  129. * Get esprima.
  130. *
  131. * @access protected
  132. * @return \JonnyW\PhantomJs\Validator\Esprima
  133. */
  134. protected function getEsprima()
  135. {
  136. $fileLocator = new FileLocator(
  137. sprintf('%s/../../../Resources/validators/', __DIR__)
  138. );
  139. $esprima = new Esprima($fileLocator, 'esprima-2.0.0.js');
  140. return $esprima;
  141. }
  142. }