GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProcedureLoaderTest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
lcom 1
cbo 10
dl 0
loc 308
rs 10
c 1
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvalidArgumentExceptionIsThrownIfProcedureFileIsNotLocal() 0 13 1
A testNotExistsExceptionIsThrownIfProcedureFileDoesNotExist() 0 13 1
A testProcedureCanBeLoaded() 0 15 1
A testProcedureTemplateIsSetInProcedureInstance() 0 15 1
A testProcedureTemplateCanBeLoaded() 0 15 1
A getProcedureLoader() 0 6 1
A getProcedureFactory() 0 11 1
A getEngine() 0 6 1
A getParser() 0 6 1
A getCache() 0 6 2
A getRenderer() 0 10 1
A getFileLocator() 0 6 1
A setUp() 0 9 2
A tearDown() 0 8 2
A getFilename() 0 4 1
A writeProcedure() 0 8 1
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\Unit\Procedure;
10
11
use Twig_Environment;
12
use Twig_Loader_String;
13
use Symfony\Component\Config\FileLocatorInterface;
14
use JonnyW\PhantomJs\Engine;
15
use JonnyW\PhantomJs\Cache\FileCache;
16
use JonnyW\PhantomJs\Cache\CacheInterface;
17
use JonnyW\PhantomJs\Parser\JsonParser;
18
use JonnyW\PhantomJs\Parser\ParserInterface;
19
use JonnyW\PhantomJs\Template\TemplateRenderer;
20
use JonnyW\PhantomJs\Template\TemplateRendererInterface;
21
use JonnyW\PhantomJs\Procedure\ProcedureFactory;
22
use JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface;
23
use JonnyW\PhantomJs\Procedure\ProcedureLoader;
24
25
/**
26
 * PHP PhantomJs
27
 *
28
 * @author Jon Wenmoth <[email protected]>
29
 */
30
class ProcedureLoaderTest extends \PHPUnit_Framework_TestCase
31
{
32
    /**
33
     * Test filename
34
     *
35
     * @var string
36
     * @access protected
37
     */
38
    protected $filename;
39
40
    /**
41
     * Test directory
42
     *
43
     * @var string
44
     * @access protected
45
     */
46
    protected $directory;
47
48
/** +++++++++++++++++++++++++++++++++++ **/
49
/** ++++++++++++++ TESTS ++++++++++++++ **/
50
/** +++++++++++++++++++++++++++++++++++ **/
51
52
    /**
53
     * Test invalid argument exception is thrown if procedure
54
     * file is not local.
55
     *
56
     * @access public
57
     * @return void
58
     */
59
    public function testInvalidArgumentExceptionIsThrownIfProcedureFileIsNotLocal()
60
    {
61
        $this->setExpectedException('\InvalidArgumentException');
62
63
        $procedureFactory = $this->getProcedureFactory();
64
        $fileLocator      = $this->getFileLocator();
65
66
        $fileLocator->method('locate')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...g\FileLocatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            ->will($this->returnValue('http://example.com/index.html'));
68
69
        $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
70
        $procedureLoader->load('test');
71
    }
72
73
    /**
74
     * Test load throws not exists exception if
75
     * if procedure file does not exist.
76
     *
77
     * @access public
78
     * @return void
79
     */
80
    public function testNotExistsExceptionIsThrownIfProcedureFileDoesNotExist()
81
    {
82
        $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotExistsException');
83
84
        $procedureFactory = $this->getProcedureFactory();
85
        $fileLocator      = $this->getFileLocator();
86
87
        $fileLocator->method('locate')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...g\FileLocatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            ->will($this->returnValue('/invalid/file.proc'));
89
90
        $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
91
        $procedureLoader->load('test');
92
    }
93
94
    /**
95
     * Test procedure can be laoded.
96
     *
97
     * @access public
98
     * @return void
99
     */
100
    public function testProcedureCanBeLoaded()
101
    {
102
        $body = 'TEST_PROCEDURE';
103
        $file = $this->writeProcedure($body);
104
105
        $procedureFactory = $this->getProcedureFactory();
106
        $fileLocator      = $this->getFileLocator();
107
108
        $fileLocator->method('locate')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...g\FileLocatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
            ->will($this->returnValue($file));
110
111
        $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
112
113
        $this->assertInstanceOf('\JonnyW\PhantomJs\Procedure\ProcedureInterface', $procedureLoader->load('test'));
114
    }
115
116
    /**
117
     * Test procedure template is set in procedure
118
     * instance.
119
     *
120
     * @access public
121
     * @return void
122
     */
123
    public function testProcedureTemplateIsSetInProcedureInstance()
124
    {
125
        $body = 'TEST_PROCEDURE';
126
        $file = $this->writeProcedure($body);
127
128
        $procedureFactory = $this->getProcedureFactory();
129
        $fileLocator      = $this->getFileLocator();
130
131
        $fileLocator->method('locate')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...g\FileLocatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
            ->will($this->returnValue($file));
133
134
        $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
135
136
        $this->assertSame($body, $procedureLoader->load('test')->getTemplate());
137
    }
138
139
    /**
140
     * Test procedure template can be loaded.
141
     *
142
     * @access public
143
     * @return void
144
     */
145
    public function testProcedureTemplateCanBeLoaded()
146
    {
147
        $body = 'TEST_PROCEDURE';
148
        $file = $this->writeProcedure($body);
149
150
        $procedureFactory = $this->getProcedureFactory();
151
        $fileLocator      = $this->getFileLocator();
152
153
        $fileLocator->method('locate')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...g\FileLocatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
            ->will($this->returnValue($file));
155
156
        $procedureLoader = $this->getProcedureLoader($procedureFactory, $fileLocator);
157
158
        $this->assertNotNull($procedureLoader->loadTemplate('test'));
159
    }
160
161
/** +++++++++++++++++++++++++++++++++++ **/
162
/** ++++++++++ TEST ENTITIES ++++++++++ **/
163
/** +++++++++++++++++++++++++++++++++++ **/
164
165
    /**
166
     * Get procedure loader instance.
167
     *
168
     * @access public
169
     * @param  \JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface $procedureFactory
170
     * @param  \Symfony\Component\Config\FileLocatorInterface        $locator
171
     * @return \JonnyW\PhantomJs\Procedure\ProcedureLoader
172
     */
173
    protected function getProcedureLoader(ProcedureFactoryInterface $procedureFactory, FileLocatorInterface $locator)
174
    {
175
        $procedureLoader = new ProcedureLoader($procedureFactory, $locator);
176
177
        return $procedureLoader;
178
    }
179
180
    /**
181
     * Get procedure factory instance.
182
     *
183
     * @access protected
184
     * @param  \JonnyW\PhantomJs\Parser\ParserInterface             $parser
0 ignored issues
show
Bug introduced by
There is no parameter named $parser. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
185
     * @param  \JonnyW\PhantomJs\Cache\CacheInterface               $cacheHandler
0 ignored issues
show
Bug introduced by
There is no parameter named $cacheHandler. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
186
     * @param  \JonnyW\PhantomJs\Template\TemplateRendererInterface $renderer
0 ignored issues
show
Bug introduced by
There is no parameter named $renderer. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
187
     * @return \JonnyW\PhantomJs\Procedure\ProcedureFactory
188
     */
189
    protected function getProcedureFactory()
190
    {
191
        $engine   = $this->getEngine();
192
        $parser   = $this->getParser();
193
        $cache    = $this->getCache();
194
        $renderer = $this->getRenderer();
195
196
        $procedureFactory = new ProcedureFactory($engine, $parser, $cache, $renderer);
197
198
        return $procedureFactory;
199
    }
200
201
    /**
202
     * Get engine.
203
     *
204
     * @access protected
205
     * @return \JonnyW\PhantomJs\Engine
206
     */
207
    protected function getEngine()
208
    {
209
        $engine = new Engine();
210
211
        return $engine;
212
    }
213
214
    /**
215
     * Get parser.
216
     *
217
     * @access protected
218
     * @return \JonnyW\PhantomJs\Parser\JsonParser
219
     */
220
    protected function getParser()
221
    {
222
        $parser = new JsonParser();
223
224
        return $parser;
225
    }
226
227
    /**
228
     * Get cache.
229
     *
230
     * @access protected
231
     * @param  string                            $cacheDir  (default: '')
232
     * @param  string                            $extension (default: 'proc')
233
     * @return \JonnyW\PhantomJs\Cache\FileCache
234
     */
235
    protected function getCache($cacheDir = '', $extension = 'proc')
0 ignored issues
show
Unused Code introduced by
The parameter $extension is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
236
    {
237
        $cache = new FileCache(($cacheDir ? $cacheDir : sys_get_temp_dir()), 'proc');
238
239
        return $cache;
240
    }
241
242
    /**
243
     * Get template renderer.
244
     *
245
     * @access protected
246
     * @return \JonnyW\PhantomJs\Template\TemplateRenderer
247
     */
248
    protected function getRenderer()
249
    {
250
        $twig = new Twig_Environment(
251
            new Twig_Loader_String()
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_String has been deprecated with message: since 1.18.1 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
252
        );
253
254
        $renderer = new TemplateRenderer($twig);
255
256
        return $renderer;
257
    }
258
259
/** +++++++++++++++++++++++++++++++++++ **/
260
/** ++++++++++ MOCKS / STUBS ++++++++++ **/
261
/** +++++++++++++++++++++++++++++++++++ **/
262
263
    /**
264
     * Get file locator.
265
     *
266
     * @access protected
267
     * @return \Symfony\Component\Config\FileLocatorInterface
268
     */
269
    protected function getFileLocator()
270
    {
271
        $fileLocator = $this->getMock('\Symfony\Component\Config\FileLocatorInterface');
272
273
        return $fileLocator;
274
    }
275
276
/** +++++++++++++++++++++++++++++++++++ **/
277
/** ++++++++++++ UTILITIES ++++++++++++ **/
278
/** +++++++++++++++++++++++++++++++++++ **/
279
280
    /**
281
     * Set up test environment.
282
     *
283
     * @access public
284
     * @return void
285
     */
286
    public function setUp()
287
    {
288
        $this->filename  = 'test.proc';
289
        $this->directory = sys_get_temp_dir();
290
291
        if (!is_writable($this->directory)) {
292
            throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
293
        }
294
    }
295
296
    /**
297
     * Tear down test environment.
298
     *
299
     * @access public
300
     * @return void
301
     */
302
    public function tearDown()
303
    {
304
        $filename = $this->getFilename();
305
306
        if (file_exists($filename)) {
307
            unlink($filename);
308
        }
309
    }
310
311
    /**
312
     * Get test filename.
313
     *
314
     * @access public
315
     * @return string
316
     */
317
    public function getFilename()
318
    {
319
        return sprintf('%1$s/%2$s', $this->directory, $this->filename);
320
    }
321
322
    /**
323
     * Write procedure body to file.
324
     *
325
     * @access public
326
     * @param  string $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
327
     * @return string
328
     */
329
    public function writeProcedure($procedure)
330
    {
331
        $filename = $this->getFilename();
332
333
        file_put_contents($filename, $procedure);
334
335
        return $filename;
336
    }
337
}
338