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