bytic /
data-objects
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace ByTIC\DataObjects\Tests\Casts; |
||||||
| 4 | |||||||
| 5 | use ByTIC\DataObjects\Casts\Metadata\Metadata; |
||||||
| 6 | use ByTIC\DataObjects\Tests\AbstractTest; |
||||||
| 7 | use ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book; |
||||||
| 8 | use ByTIC\DataObjects\Tests\Fixtures\Models\Books\BookOptions; |
||||||
| 9 | |||||||
| 10 | /** |
||||||
| 11 | * Class SerializedCollectionTest |
||||||
| 12 | * @package ByTIC\DataObjects\Tests\Casts |
||||||
| 13 | */ |
||||||
| 14 | class AsMetadataObjectTest extends AbstractTest |
||||||
| 15 | { |
||||||
| 16 | /** |
||||||
| 17 | * @dataProvider data_cast_values |
||||||
| 18 | */ |
||||||
| 19 | public function test_cast_values($string, $return) |
||||||
| 20 | { |
||||||
| 21 | $book = new Book(); |
||||||
| 22 | $book->fill(['metadata' => $string]); |
||||||
| 23 | |||||||
| 24 | /** @var Metadata $propertiesValue */ |
||||||
| 25 | $propertiesValue = $book->get('metadata'); |
||||||
| 26 | self::assertInstanceOf(\ByTIC\DataObjects\Casts\Metadata\Metadata::class, $propertiesValue); |
||||||
| 27 | |||||||
| 28 | self::assertSame($return, $propertiesValue->getArrayCopy()); |
||||||
| 29 | } |
||||||
| 30 | |||||||
| 31 | /** |
||||||
| 32 | * @return array[] |
||||||
| 33 | */ |
||||||
| 34 | public function data_cast_values() |
||||||
| 35 | { |
||||||
| 36 | return [ |
||||||
| 37 | [null, []], |
||||||
| 38 | ['', []], |
||||||
| 39 | ['{}', []], |
||||||
| 40 | [json_encode(['currency' => 'RON']), ['currency' => 'RON']], |
||||||
| 41 | [json_encode(['test' => 1]), ['test' => 1]], |
||||||
| 42 | ]; |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | public function test_cast_null() |
||||||
| 46 | { |
||||||
| 47 | $book = new Book(); |
||||||
| 48 | $book->fill( |
||||||
| 49 | [ |
||||||
| 50 | 'metadata' => null |
||||||
| 51 | ] |
||||||
| 52 | ); |
||||||
| 53 | |||||||
| 54 | /** @var Metadata $propertiesValue */ |
||||||
| 55 | $propertiesValue = $book->get('metadata'); |
||||||
| 56 | self::assertInstanceOf(Metadata::class, $propertiesValue); |
||||||
| 57 | self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
||||||
| 58 | self::assertSame(null, $book->getAttribute('metadata')); |
||||||
| 59 | |||||||
| 60 | $propertiesValue['options3'] = 'value3'; |
||||||
| 61 | $book->set('metadata', $propertiesValue); |
||||||
| 62 | |||||||
| 63 | self::assertSame( |
||||||
| 64 | '{"options3":"value3"}', |
||||||
| 65 | $book->getAttribute('metadata') |
||||||
| 66 | ); |
||||||
| 67 | } |
||||||
| 68 | |||||||
| 69 | public function test_cast_string() |
||||||
| 70 | { |
||||||
| 71 | $book = new Book(); |
||||||
| 72 | $book->fill( |
||||||
| 73 | [ |
||||||
| 74 | 'metadata' => '{}' |
||||||
| 75 | ] |
||||||
| 76 | ); |
||||||
| 77 | |||||||
| 78 | /** @var Metadata $propertiesValue */ |
||||||
| 79 | $propertiesValue = $book->get('metadata'); |
||||||
| 80 | self::assertInstanceOf(Metadata::class, $propertiesValue); |
||||||
| 81 | self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
||||||
| 82 | self::assertSame('{}', $book->getAttribute('metadata')); |
||||||
| 83 | |||||||
| 84 | $propertiesValue['options3'] = 'value3'; |
||||||
| 85 | $book->set('metadata', $propertiesValue); |
||||||
| 86 | |||||||
| 87 | self::assertSame( |
||||||
| 88 | '{"options3":"value3"}', |
||||||
| 89 | $book->getAttribute('metadata') |
||||||
| 90 | ); |
||||||
| 91 | } |
||||||
| 92 | |||||||
| 93 | public function test_cast_invalid() |
||||||
| 94 | { |
||||||
| 95 | $book = new Book(); |
||||||
| 96 | $book->fill( |
||||||
| 97 | [ |
||||||
| 98 | 'metadata' => '{789}' |
||||||
| 99 | ] |
||||||
| 100 | ); |
||||||
| 101 | $this->expectException(\InvalidArgumentException::class); |
||||||
| 102 | $book->get('metadata'); |
||||||
| 103 | } |
||||||
| 104 | |||||||
| 105 | public function test_cast_with_custom_class() |
||||||
| 106 | { |
||||||
| 107 | $book = new Book(); |
||||||
| 108 | ; |
||||||
| 109 | $options = $book->get('options'); |
||||||
| 110 | self::assertInstanceOf(BookOptions::class, $options); |
||||||
| 111 | } |
||||||
| 112 | |||||||
| 113 | public function test_cast() |
||||||
| 114 | { |
||||||
| 115 | $properties = ['option1' => 1, 'option2' => '2', 'option3' => ['opt33' => '33']]; |
||||||
| 116 | $propertiesSerialized = json_encode($properties); |
||||||
| 117 | $book = new Book(); |
||||||
| 118 | $book->fill( |
||||||
| 119 | [ |
||||||
| 120 | 'metadata' => $propertiesSerialized |
||||||
| 121 | ] |
||||||
| 122 | ); |
||||||
| 123 | |||||||
| 124 | /** @var Metadata $propertiesValue */ |
||||||
| 125 | $propertiesValue = $book->get('metadata'); |
||||||
| 126 | self::assertInstanceOf(Metadata::class, $propertiesValue); |
||||||
| 127 | self::assertSame($propertiesValue->get('option1'), 1); |
||||||
| 128 | self::assertSame($propertiesValue->get('option3.opt33'), '33'); |
||||||
| 129 | self::assertSame($propertiesSerialized, $book->getAttribute('metadata')); |
||||||
| 130 | |||||||
| 131 | $book->metadata->set('option2', '22'); |
||||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
The method
set() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 132 | $book->metadata->set('option3.opt33', '333'); |
||||||
| 133 | |||||||
| 134 | self::assertSame( |
||||||
| 135 | '{"option1":1,"option2":"22","option3":{"opt33":"333"}}', |
||||||
| 136 | $book->getAttribute('metadata') |
||||||
| 137 | ); |
||||||
| 138 | } |
||||||
| 139 | } |
||||||
| 140 |