1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TwigOverride\Test; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Prophecy\Prophet; |
7
|
|
|
use TwigOverride\TwigOverrideExtension; |
8
|
|
|
use TwigOverride\Providers\ProviderInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Unit test for the TwigOverrideExtension class. |
12
|
|
|
* |
13
|
|
|
* @covers \TwigOverride\TwigOverrideExtension |
14
|
|
|
*/ |
15
|
|
|
class TwigOverrideExtensionTest extends TestCase { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A prophecy prophet for creating mocks. |
19
|
|
|
* |
20
|
|
|
* @var \Prophecy\Prophet |
21
|
|
|
*/ |
22
|
|
|
private $prophet; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function setup() { |
28
|
|
|
$this->prophet = new Prophet(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function tearDown() { |
35
|
|
|
$this->prophet->checkPredictions(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Tests trying to create a twig extension with invalid providers. |
40
|
|
|
* |
41
|
|
|
* @expectedException \InvalidArgumentException |
42
|
|
|
*/ |
43
|
|
|
public function testInvalidConstruction() { |
44
|
|
|
new TwigOverrideExtension(['a_string']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Tests that the override methods call the correct provider method. |
49
|
|
|
* |
50
|
|
|
* @param string $override_method |
51
|
|
|
* The name of the method to call on the extension. |
52
|
|
|
* @param string $expected_call |
53
|
|
|
* The name of the expected provider method to be called. |
54
|
|
|
* |
55
|
|
|
* @dataProvider paramProvider |
56
|
|
|
*/ |
57
|
|
|
public function testOverrideMethods($override_method, $expected_call) { |
58
|
|
|
$template_name = 'test.twig'; |
59
|
|
|
$only = TRUE; |
60
|
|
|
$expected_return = 'return_value'; |
61
|
|
|
$provider = $this->prophet->prophesize(ProviderInterface::CLASS); |
|
|
|
|
62
|
|
|
$provider->{$expected_call}($template_name, [], [], $only) |
63
|
|
|
->willReturn($expected_return) |
64
|
|
|
->shouldBeCalledTimes(1); |
65
|
|
|
|
66
|
|
|
$twig_extension = new TwigOverrideExtension([$provider->reveal()]); |
67
|
|
|
$actual_return = $twig_extension->{$override_method}($template_name, $only); |
68
|
|
|
$this->assertEquals($expected_return, $actual_return); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Provides test cases for testOverrideMethods. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
* See PHPUnit docs for info about data providers. |
76
|
|
|
*/ |
77
|
|
|
public function paramProvider() { |
78
|
|
|
return [ |
79
|
|
|
['twigOverride', 'rewriteTemplateName'], |
80
|
|
|
['twigOverrideParameters', 'preprocessTemplateArgs'], |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|