| 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_json_serialize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		$format = "error: {arg}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 		$arg1 = uniqid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		$arg2 = uniqid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		$arg3 = uniqid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		$arg4 = uniqid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		$attribute = uniqid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		$errors = $this->errors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		$errors->add($attribute, $format, [ 'arg' => $arg3 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		$errors->add_generic($format, [ 'arg' => $arg1 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		$errors->add($attribute, $format, [ 'arg' => $arg4 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		$errors->add_generic($format, [ 'arg' => $arg2 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 		$this->assertSame(json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 			ErrorCollection::GENERIC => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 				"error: {$arg1}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 				"error: {$arg2}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 			], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 			$attribute => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 				"error: {$arg3}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 				"error: {$arg4}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 			] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 		]), json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 205 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  |