Completed
Push — 2.0 ( 4df6fa...9a9bd1 )
by Olivier
02:03
created

ErrorCollectionTest::test_merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie;
13
14
/**
15
 * @group error
16
 */
17
class ErrorCollectionTest extends \PHPUnit_Framework_TestCase
18
{
19
	/**
20
	 * @var ErrorCollection
21
	 */
22
	private $errors;
23
24
	public function setUp()
25
	{
26
		$this->errors = new ErrorCollection;
27
	}
28
29
	public function test_add_with_string()
30
	{
31
		$attribute = uniqid();
32
		$format = uniqid();
33
		$args = [ uniqid() => uniqid() ];
34
		$this->errors->add($attribute, $format, $args);
35
		$this->assertEquals(1, $this->errors->count());
36
37
		$errors = $this->errors[$attribute];
38
		$this->assertInternalType('array', $errors);
39
		$error = reset($errors);
40
		$this->assertInstanceOf(Error::class, $error);
41
		$this->assertSame($format, $error->format);
42
		$this->assertSame($args, $error->args);
43
	}
44
45
	public function test_add_with_true()
46
	{
47
		$attribute = uniqid();
48
		$args = [ uniqid() => uniqid() ];
49
		$this->errors->add($attribute, true, $args);
50
		$this->assertEquals(1, $this->errors->count());
51
52
		$errors = $this->errors[$attribute];
53
		$this->assertInternalType('array', $errors);
54
		$error = reset($errors);
55
		$this->assertInstanceOf(Error::class, $error);
56
		$this->assertSame("", $error->format);
57
		$this->assertSame($args, $error->args);
58
	}
59
60
	public function test_add_with_error()
61
	{
62
		$error = new Error(uniqid(), [ uniqid() => uniqid() ]);
63
		$attribute = uniqid();
64
		$this->errors->add($attribute, $error, [ uniqid() => uniqid() ]);
65
		$this->assertEquals(1, $this->errors->count());
66
67
		$errors = $this->errors[$attribute];
68
		$this->assertInternalType('array', $errors);
69
		$this->assertSame($error, reset($errors));
70
	}
71
72
	public function test_add_generic()
73
	{
74
		$error = new Error(uniqid(), [ uniqid() => uniqid() ]);
75
		$this->errors->add_generic($error);
76
		$errors = $this->errors[null];
77
		$this->assertSame($error, reset($errors));
78
	}
79
80
	public function test_array_access_interface()
81
	{
82
		$errors = $this->errors;
83
		$err1 = new Error(uniqid(), [ uniqid() => uniqid() ]);
84
		$err2 = new Error(uniqid(), [ uniqid() => uniqid() ]);
85
		$err3 = new Error(uniqid(), [ uniqid() => uniqid() ]);
86
		$err4 = new Error(uniqid(), [ uniqid() => uniqid() ]);
87
		$attribute = uniqid();
88
		$errors[] = $err1;
89
		$errors[] = $err2;
90
		$errors[$attribute] = $err3;
91
		$errors[$attribute] = $err4;
92
93
		$this->assertTrue(isset($errors[null]));
94
		$this->assertTrue(isset($errors[$attribute]));
95
		$this->assertSame([ $err1, $err2 ], $errors[null]);
96
		$this->assertSame([ $err3, $err4 ], $errors[$attribute]);
97
98
		unset($errors[null]);
99
		$this->assertSame([], $errors[null]);
100
		unset($errors[$attribute]);
101
		$this->assertSame([], $errors[$attribute]);
102
	}
103
104
	public function test_iterator()
105
	{
106
		$errors = $this->errors;
107
		$err1 = new Error('err1-' . uniqid());
108
		$err2 = new Error('err2-' . uniqid());
109
		$err3 = new Error('err3-' . uniqid());
110
		$err4 = new Error('err4-' . uniqid());
111
		$attribute = uniqid();
112
		$errors[$attribute] = $err3;
113
		$errors[] = $err1;
114
		$errors[$attribute] = $err4;
115
		$errors[] = $err2;
116
117
		$iterator_errors = [];
118
119
		foreach ($errors as $a => $e)
120
		{
121
			$iterator_errors[] = [ $a => $e ];
122
		}
123
124
		$this->assertSame([
125
126
			[ ErrorCollection::GENERIC => $err1 ],
127
			[ ErrorCollection::GENERIC => $err2 ],
128
			[ $attribute => $err3 ],
129
			[ $attribute => $err4 ],
130
131
		], $iterator_errors);
132
	}
133
134
	public function test_each()
135
	{
136
		$errors = $this->errors;
137
		$err1 = new Error('err1-' . uniqid());
138
		$err2 = new Error('err2-' . uniqid());
139
		$err3 = new Error('err3-' . uniqid());
140
		$err4 = new Error('err4-' . uniqid());
141
		$attribute = uniqid();
142
		$errors[$attribute] = $err3;
143
		$errors[] = $err1;
144
		$errors[$attribute] = $err4;
145
		$errors[] = $err2;
146
147
		$iterator_errors = [];
148
149
		$errors->each(function ($error, $attribute, $collection) use (&$iterator_errors, $errors) {
150
151
			$this->assertSame($errors, $collection);
152
			$iterator_errors[] = [ $attribute => $error ];
153
154
		});
155
156
		$this->assertSame([
157
158
			[ ErrorCollection::GENERIC => $err1 ],
159
			[ ErrorCollection::GENERIC => $err2 ],
160
			[ $attribute => $err3 ],
161
			[ $attribute => $err4 ],
162
163
		], $iterator_errors);
164
	}
165
166
	public function test_clear()
167
	{
168
		$errors = $this->errors->add(uniqid())->add(uniqid())->add(uniqid());
169
		$this->assertEquals(3, $errors->count());
170
		$this->assertEquals(0, $errors->clear()->count());
171
	}
172
173
	public function test_merge()
174
	{
175
		$er1 = new Error(uniqid());
176
		$er2 = new Error(uniqid());
177
178
		$col1 = (new ErrorCollection)
179
			->add_generic($er1);
180
		$col2 = (new ErrorCollection)
181
			->add_generic($er2);
182
183
		$col1->merge($col2);
184
185
		$this->assertSame([
186
187
			ErrorCollection::GENERIC => [
188
189
				$er1, $er2
190
191
			]
192
193
		], $col1->to_array());
194
	}
195
196
	public function test_json_serialize()
197
	{
198
		$format = "error: {arg}";
199
		$arg1 = uniqid();
200
		$arg2 = uniqid();
201
		$arg3 = uniqid();
202
		$arg4 = uniqid();
203
		$attribute = uniqid();
204
		$errors = $this->errors;
205
		$errors->add($attribute, $format, [ 'arg' => $arg3 ]);
206
		$errors->add_generic($format, [ 'arg' => $arg1 ]);
207
		$errors->add($attribute, $format, [ 'arg' => $arg4 ]);
208
		$errors->add_generic($format, [ 'arg' => $arg2 ]);
209
210
		$this->assertSame(json_encode([
211
212
			ErrorCollection::GENERIC => [
213
214
				"error: {$arg1}",
215
				"error: {$arg2}",
216
217
			],
218
219
			$attribute => [
220
221
				"error: {$arg3}",
222
				"error: {$arg4}",
223
224
			]
225
226
		]), json_encode($errors));
227
	}
228
}
229