TwigOverrideNodeVisitorTest::setup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TwigOverride\Test;
4
5
use PHPUnit\Framework\TestCase;
6
use Prophecy\Prophet;
7
use TwigOverride\TwigOverrideExtension;
8
use TwigOverride\TwigOverrideNodeVisitor;
9
10
/**
11
 * Unit test for the TwigOverrideNodeVisitor class.
12
 *
13
 * @covers \TwigOverride\TwigOverrideNodeVisitor
14
 */
15
class TwigOverrideNodeVisitorTest extends TestCase {
16
17
  /**
18
   * A mock twig environment to pass to the visitor methods.
19
   *
20
   * We don't need a real twig environment since the class doesn't use it.
21
   *
22
   * @var \Twig_Environment
23
   */
24
  private $twigEnv;
25
26
  /**
27
   * The visitor object to test.
28
   *
29
   * Since this object is stateless, we only need to create it once.
30
   *
31
   * @var \TwigOverride\TwigOverrideNodeVisitor
32
   */
33
  private $visitor;
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function setup() {
39
    $prophet = new Prophet();
40
    $this->twigEnv = $prophet->prophesize('\Twig_Environment')->reveal();
41
    $this->visitor = new TwigOverrideNodeVisitor();
42
  }
43
44
  /**
45
   * Tests the leaveNode method.
46
   *
47
   * @param string $node_type
48
   *   A 'module', 'include', or 'embed' to specify which type of node to test.
49
   * @param string $template_name
50
   *   The name of the template to test.
51
   * @param array|null $with
52
   *   Optional 'with' variables to pass to include or embed.
53
   * @param bool $only
54
   *   Optional 'only' flag to pass to include or embed.
55
   *
56
   * @dataProvider nodeProvider
57
   */
58
  public function testLeaveNode($node_type, $template_name, array $with = NULL, $only = NULL) {
59
    if ($node_type == 'module') {
60
      $node = $this->createModule($template_name);
61
      $check_template_node = 'parent';
62
      $check_variables_node = NULL;
63
    }
64
    elseif ($node_type == 'include') {
65
      $node = $this->createInclude($template_name, $with, $only);
66
      $check_template_node = 'expr';
67
      $check_variables_node = 'variables';
68
    }
69
    elseif ($node_type == 'embed') {
70
      $node = $this->createEmbed($template_name, $with, $only);
71
      $check_template_node = NULL;
72
      $check_variables_node = 'variables';
73
    }
74
    else {
75
      throw new \InvalidArgumentException('Invalid Node Type');
76
    }
77
    $result_node = $this->visitor->leaveNode($node, $this->twigEnv);
78
79
    $expected_args = new \Twig_Node([
80
      new \Twig_Node_Expression_Constant($template_name, 1),
81
      new \Twig_Node_Expression_Constant(!!$only, 1),
82
      $with ? new \Twig_Node_Expression_Constant($with, 1) : new \Twig_Node_Expression_Constant(NULL, 1),
83
      new \Twig_Node_Expression_Name('_context', 1),
84
    ]);
85
86
    if (isset($check_template_node)) {
87
      $this->assertCall(TwigOverrideExtension::TEMPLATE_OVERRIDE_FUNCTION, $expected_args, $result_node->getNode($check_template_node));
88
    }
89
90
    if (isset($check_variables_node)) {
91
      $this->assertCall(TwigOverrideExtension::PARAMETER_OVERRIDE_FUNCTION, $expected_args, $result_node->getNode($check_variables_node));
92
    }
93
  }
94
95
  /**
96
   * A test case provider for testLeaveNode.
97
   *
98
   * @return array
99
   *   See PHPUnit docs for info about data providers.
100
   */
101
  public function nodeProvider() {
102
    return [
103
      ['module', '@test/test.twig'],
104
      ['include', '@test/test2.twig', ['withArgs' => TRUE], TRUE],
105
      ['include', '@test/test3.twig', ['withArgs' => TRUE], FALSE],
106
      ['include', '@test/test3.twig', [], FALSE],
107
      ['embed', '@test/test4.twig', ['withArgs' => TRUE], TRUE],
108
      ['embed', '@test/test5.twig', ['withArgs' => TRUE], FALSE],
109
      ['embed', '@test/test6.twig', [], FALSE],
110
    ];
111
  }
112
113
  /**
114
   * A helper function for asserting that a twig call was injected correctly.
115
   *
116
   * @param string $expected_name
117
   *   The name of the function expected to be called.
118
   * @param \Twig_Node $expected_args
119
   *   The arguments that were expected to be passed as a twig node subtree.
120
   * @param \Twig_Node $node
121
   *   The node to test against.
122
   */
123
  protected function assertCall($expected_name, \Twig_Node $expected_args, \Twig_Node $node) {
124
    $this->assertInstanceOf(\Twig_Node_Expression_Function::CLASS, $node);
0 ignored issues
show
Bug introduced by
The constant Twig_Node_Expression_Function::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
125
    $this->assertEquals($expected_name, $node->getAttribute('name'));
126
    $this->assertTrue($node->hasNode('arguments'));
127
    $this->assertEquals((string) $expected_args, (string) $node->getNode('arguments'));
128
  }
129
130
  /**
131
   * Creates a Twig_Node_Module for a template.
132
   *
133
   * This simulates a twig module loaded as a result of a {% embed ... %} or
134
   * {% include ... %}.
135
   *
136
   * @return \Twig_Node_Module
137
   *   The simulated module node.
138
   */
139
  protected function createModule($template_name) {
140
    return new \Twig_Node_Module(
141
      new \Twig_Node(),
142
      new \Twig_Node_Expression_Constant($template_name, 1),
143
      new \Twig_Node(),
144
      new \Twig_Node(),
145
      new \Twig_Node(),
146
      NULL,
147
      new \Twig_Source('', $template_name)
148
    );
149
  }
150
151
  /**
152
   * Creates a Twig_Node_Include for a template.
153
   *
154
   * This simulates a {% include ... %}.
155
   *
156
   * @return \Twig_Node_Include
157
   *   The simulated include node.
158
   */
159
  protected function createInclude($template_name, array $with = NULL, $only) {
160
    return new \Twig_Node_Include(
161
      new \Twig_Node_Expression_Constant($template_name, 1),
162
      $with ? new \Twig_Node_Expression_Constant($with, 1) : NULL,
163
      $only,
164
      FALSE,
165
      0
166
    );
167
  }
168
169
  /**
170
   * Creates a Twig_Node_Embed for a template.
171
   *
172
   * This simulates a {% embed ... %}.
173
   *
174
   * @return \Twig_Node_Embed
175
   *   The simulated embed node.
176
   */
177
  protected function createEmbed($template_name, array $with = NULL, $only) {
178
    return new \Twig_Node_Embed(
179
      $template_name,
180
      0,
181
      $with ? new \Twig_Node_Expression_Constant($with, 1) : NULL,
182
      $only,
183
      FALSE,
184
      0
185
    );
186
  }
187
188
}
189