Completed
Push — 2.0 ( 9a9bd1...98f64c )
by Olivier
02:55
created

ErrorCollectionIteratorTest::test_renderer()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 2
eloc 22
nc 2
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 render
16
 */
17
class ErrorCollectionIteratorTest extends \PHPUnit_Framework_TestCase
18
{
19
	public function test_renderer()
20
	{
21
		$format = "error: {arg}";
22
		$arg1 = uniqid();
23
		$arg2 = uniqid();
24
		$arg3 = uniqid();
25
		$arg4 = uniqid();
26
		$attribute = uniqid();
27
28
		$errors = (new ErrorCollection)
29
			->add($attribute, $format, [ 'arg' => $arg3 ])
30
			->add_generic($format, [ 'arg' => $arg1 ])
31
			->add($attribute, $format, [ 'arg' => $arg4 ])
32
			->add_generic($format, [ 'arg' => $arg2 ])
33
		;
34
35
		$renderer = new ErrorCollectionIterator($errors);
36
		$rendered = [];
37
38
		foreach ($renderer as $a => $r)
39
		{
40
			$rendered[] = [ $a, $r ];
41
		}
42
43
		$this->assertSame([
44
45
			[ ErrorCollection::GENERIC, "error: {$arg1}" ],
46
			[ ErrorCollection::GENERIC, "error: {$arg2}" ],
47
			[ $attribute, "error: {$arg3}" ],
48
			[ $attribute, "error: {$arg4}" ],
49
50
		], $rendered);
51
	}
52
53
	public function test_render_with_customer_error_renderer()
54
	{
55
		$format = "error: {arg}";
56
		$arg1 = uniqid();
57
		$arg2 = uniqid();
58
		$arg3 = uniqid();
59
		$arg4 = uniqid();
60
		$attribute = uniqid();
61
62
		$errors = (new ErrorCollection)
63
			->add($attribute, $format, [ 'arg' => $arg3 ])
64
			->add_generic($format, [ 'arg' => $arg1 ])
65
			->add($attribute, $format, [ 'arg' => $arg4 ])
66
			->add_generic($format, [ 'arg' => $arg2 ])
67
		;
68
69
		$renderer = new ErrorCollectionIterator($errors, function (Error $error, $attribute, ErrorCollection $collection) use ($errors) {
70
71
			$this->assertSame($errors, $collection);
72
73
			return strrev($error);
74
75
		});
76
77
		$rendered = [];
78
79
		foreach ($renderer as $a => $r)
80
		{
81
			$rendered[] = [ $a, $r ];
82
		}
83
84
		$this->assertSame([
85
86
			[ ErrorCollection::GENERIC, strrev("error: {$arg1}") ],
87
			[ ErrorCollection::GENERIC, strrev("error: {$arg2}") ],
88
			[ $attribute, strrev("error: {$arg3}") ],
89
			[ $attribute, strrev("error: {$arg4}") ],
90
91
		], $rendered);
92
	}
93
}
94