summaryrefslogtreecommitdiff
path: root/tests/UnitTests/Compile/AttributeCompilerTest.php
blob: 9a8fbf98a80e3320b09a5d5acd4c30bff5a71399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php

use Smarty\Compile\AttributeCompiler;
use Smarty\Compiler\Template;

class AttributeCompilerTest extends PHPUnit\Framework\TestCase
{
	/**
	 * The template compiler.
	 */
	private $template_compiler;

	/**
	 * The attributes
	 */
	private $attributes = [];

	/**
	 * @inheritDoc
	 * Set up attribute compiler class
	 */
	protected function setUp(): void
	{
		$this->template_compiler = $this->createMock(Template::class);

		// reset attributes to empty arrays
		$this->attributes = [
			'required_attributes' => [],
			'optional_attributes' => [],
			'shorttag_order' => [],
			'option_flags' => [],
		];
	}

	/**
	 * Create the attribute compiler for testing.
	 */
	private function createAttributeCompiler() {
		return new AttributeCompiler(
			$this->attributes['required_attributes'],
			$this->attributes['optional_attributes'],
			$this->attributes['shorttag_order'],
			$this->attributes['option_flags']
		);
	}

	/**
	 * Tests shorthand attribute compiling.
	 */
	public function testAttributeCompiler(): void
	{
		$this->attributes['shorttag_order'] = ['shorttag'];
		$this->attributes['required_attributes'] = ['required'];
		$this->attributes['option_flags'] = ['option', 'option_two'];

		$payload = [
			0 => 'shorttag value',
			1 => [
				'required' => 'required_value'
			],
			2 => 'option',
		];

		$this->assertEquals(
			$this->createAttributeCompiler()
				->getAttributes($this->template_compiler, $payload),
			[
				'shorttag' => 'shorttag value',
				'required' => 'required_value',
				'option' => true,
				'option_two' => false,
			]
		);
	}

	/**
	 * Tests normal optional attribute compiling.
	 */
	public function testAttributeCompilerOptionalArguments(): void
	{
		$this->attributes['optional_attributes'] = ['optional'];

		$payload = [
			0 => [
				'optional' => 'optional value'
			],
		];

		$this->assertEquals(
			$this->createAttributeCompiler()
				->getAttributes($this->template_compiler, $payload),
			[
				'optional' => 'optional value',
			]
		);

		$this->assertEquals(
			$this->createAttributeCompiler()
				->getAttributes($this->template_compiler, []),
			[]
		);
	}

	/** 
	 * Tests any attribute compiling.
	 */
	public function testAttributeCompilerAnyOptionalArguments(): void
	{
		$this->attributes['optional_attributes'] = ['_any'];

		$payload = [
			0 => [
				'optional' => 'optional value'
			],
			1 => [
				'optional_two' => 'optional value two'
			],
		];

		$this->assertEquals(
			$this->createAttributeCompiler()
				->getAttributes($this->template_compiler, $payload),
			[
				'optional' => 'optional value',
				'optional_two' => 'optional value two',
			]
		);
	}

	/**
	 * Test if the attribute compiler tries to throw a too many shorthand attributes error.
	 */
	public function testAttributeCompilerTooManyShorthands(): void
	{
		$payload = [
			0 => 'option one',
		];

		$this->template_compiler
			->expects(self::once())
			->method('trigger_template_error')
			->with('too many shorthand attributes', null, true);

		$this->createAttributeCompiler()
			->getAttributes($this->template_compiler, $payload);
	}

	/**
	 * Test if the attribute compiler tries to throw a missing required attribute error.
	 */
	public function testAttributeCompilerWithMissingRequiredAttributes(): void
	{
		$this->attributes['required_attributes'] = ['required'];

		$this->template_compiler
			->expects(self::once())
			->method('trigger_template_error')
			->with('missing \'required\' attribute', null, true);

		$this->createAttributeCompiler()
			->getAttributes($this->template_compiler, []);
	}

	/**
	 * Test if the attribute compiler tries to throw a illegal value template error.
	 */
	public function testAttributeCompilerWithInvalidOptionAttribute(): void
	{
		$this->attributes['option_flags'] = ['option'];

		$this->template_compiler
			->expects(self::once())
			->method('trigger_template_error')
			->with('illegal value \'\'foo\'\' for options flag \'option\'', null, true);

		$this->createAttributeCompiler()
			->getAttributes($this->template_compiler, [0 => ['option' => 'foo']]);
	}

	/**
	 * Test if the attribute compiler tries to throw an unexpected attribute error.
	 */
	public function testAttributeCompilerWithInvalidUnexpectedAttribute(): void
	{
		$this->template_compiler
			->expects(self::once())
			->method('trigger_template_error')
			->with('unexpected \'unexpected\' attribute', null, true);

		$this->createAttributeCompiler()
			->getAttributes($this->template_compiler, [0 => ['unexpected' => 'bar']]);
	}
}