| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace EcodevTests\Felix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ArrayIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Ecodev\Felix\Model\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Ecodev\Felix\Utility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use EcodevTests\Felix\Blog\Model\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use EcodevTests\Felix\Traits\TestWithEntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use GraphQL\Doctrine\Definition\EntityID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | final class UtilityTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     use TestWithEntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function testGetShortClassName(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         self::assertSame('User', Utility::getShortClassName(new User())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         self::assertSame('User', Utility::getShortClassName(User::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function testEntityIdToModel(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $fakeEntity = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $input = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             3 => new stdClass(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             4 => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'model' => new User(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'entity' => new class($fakeEntity) extends EntityID { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     private readonly object $fakeEntity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 ) {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 public function getEntity(): object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     return $this->fakeEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $actual = Utility::entityIdToModel($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $expected = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $expected['entity'] = $fakeEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         self::assertSame($expected, $actual, 'keys and non model values should be preserved'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self::assertNull(Utility::entityIdToModel(null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         self::assertSame([], Utility::entityIdToModel([])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     public function testModelToId(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $input = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             3 => new stdClass(), | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             'model' => new class() implements Model { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 public function getId(): ?int | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     return 123456; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             }, | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             4 => 1, | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             'entityId' => new EntityID(_em(), User::class, '456'), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $actual = Utility::modelToId($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $expected = $input; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $expected['model'] = 123456; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $expected['entityId'] = '456'; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         self::assertSame($expected, $actual, 'models must be replaced by their ids, other values should be preserved'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     private function createArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $object1 = new class() {}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $object2 = new class() {}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $object1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $object2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $object1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             '2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function testUnique(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $array = $this->createArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $actual = Utility::unique($array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $expected = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $array[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $array[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $array[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $array[5], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $array[6], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         self::assertSame($expected, $actual); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @dataProvider providerNativeUniqueWillThrowWithOurTestObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function testNativeUniqueWillThrowWithOurTestObject(int $flag): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         set_error_handler( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             function (int $errno, string $message): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 restore_error_handler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 throw new Exception($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $foo = array_unique($this->createArray(), $flag); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } catch (Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             self::assertStringStartsWith('Object of class class@anonymous could not be converted to ', $e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public static function providerNativeUniqueWillThrowWithOurTestObject(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         yield [SORT_REGULAR]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         yield [SORT_NUMERIC]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         yield [SORT_STRING]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         yield [SORT_LOCALE_STRING]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @dataProvider providerQuoteArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function testQuoteArray(array $input, string $expected): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         self::assertSame($expected, Utility::quoteArray($input)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     public static function providerQuoteArray(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         yield [[1, 2], "'1', '2'"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         yield [['foo bar', 'baz'], "'foo bar', 'baz'"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         yield [[], '']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         yield [[''], "''"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         yield [[false], "''"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         yield [[null], "''"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         yield [[0], "'0'"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         yield [[true], "'1'"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         yield [[1.23], "'1.23'"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @dataProvider providerGetCookieDomain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public function testGetCookieDomain(string $input, ?string $expected): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $actual = Utility::getCookieDomain($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         self::assertSame($expected, $actual); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     public static function providerGetCookieDomain(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         yield ['', null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         yield ['localhost', null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         yield ['example.com', '.example.com']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         yield ['www.example.com', '.example.com']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         yield ['example.com:123', '.example.com']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         yield ['www.example.com:123', '.example.com']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     public function testConcat(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $iterable = new ArrayIterator([1 => 'one', 'a' => 'overridden', 'c' => 'c']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $actual = Utility::concat(['a' => 'a', 2 => 'two'], $iterable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         self::assertSame([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             'a' => 'overridden', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             2 => 'two', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             1 => 'one', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             'c' => 'c', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         ], iterator_to_array($actual)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     public function testFilterByKeys(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         $things = new ArrayIterator(['a' => 'a', 'b' => 'b', 'c' => 'c']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         self::assertSame(['a' => 'a'], iterator_to_array(Utility::filterByKeys($things, 'a'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         self::assertSame(['a' => 'a', 'c' => 'c'], iterator_to_array(Utility::filterByKeys($things, 'c', 'a'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         self::assertSame([], iterator_to_array(Utility::filterByKeys($things, 'unknown-key'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         self::assertSame([], iterator_to_array(Utility::filterByKeys($things))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  |