|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of event-engine/php-data. |
| 4 | + * (c) 2018-2020 prooph software GmbH <contact@prooph.de> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace EventEngineTest\Data; |
| 12 | + |
| 13 | +use EventEngine\<
8000
span class=pl-v>Data\ImmutableRecordDataConverter; |
| 14 | +use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use stdClass; |
| 17 | + |
| 18 | +final class ImmutableRecordDataConverterTest extends TestCase |
| 19 | +{ |
| 20 | + private $data = []; |
| 21 | + |
| 22 | + protected function setUp() |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + $this->data = [ |
| 27 | + 'name' => 'test', |
| 28 | + 'version' => 1, |
| 29 | + 'itemList' => [['name' => 'one']], |
| 30 | + 'access' => true, |
| 31 | + ]; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @test |
| 36 | + */ |
| 37 | + public function it_converts_immutable_record_to_array() |
| 38 | + { |
| 39 | + $valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data); |
| 40 | + |
| 41 | + $dataConverter = new ImmutableRecordDataConverter(); |
| 42 | + |
| 43 | + $this->data['type'] = null; |
| 44 | + $this->data['percentage'] = 0.5; |
| 45 | + |
| 46 | + |
| 47 | + $this->assertEquals( |
| 48 | + $this->data, |
| 49 | + $dataConverter->convertDataToArray( |
| 50 | + TypeHintedImmutableRecordWithValueObjects::class, |
| 51 | + $valueObjects |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @test |
| 58 | + */ |
| 59 | + public function it_converts_stdClass_to_array() |
| 60 | + { |
| 61 | + $obj = new stdClass(); |
| 62 | + |
| 63 | + $obj->test = "This is a test"; |
| 64 | + $obj->msg = "With a message"; |
| 65 | + |
| 66 | + $this->assertEquals([ |
| 67 | + 'test' => "This is a test", |
| 68 | + 'msg' => "With a message", |
| 69 | + ], |
| 70 | + (new ImmutableRecordDataConverter())->convertDataToArray(stdClass::class, $obj) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @test |
| 76 | + */ |
| 77 | + public function it_converts_array_to_immutable_record() |
| 78 | + { |
| 79 | + $dataConverter = new ImmutableRecordDataConverter(); |
| 80 | + |
| 81 | + $this->assertTrue($dataConverter->canConvertTypeToData(TypeHintedImmutableRecordWithValueObjects::class)); |
| 82 | + |
| 83 | + $valueObjects = $dataConverter->convertArrayToData( |
| 84 | + TypeHintedImmutableRecordWithValueObjects::class, |
| 85 | + $this->data |
| 86 | + ); |
| 87 | + |
| 88 | + $this->data['type'] = null; |
| 89 | + $this->data['percentage'] = 0.5; |
| 90 | + |
| 91 | + $this->assertEquals( |
| 92 | + $this->data, |
| 93 | + $valueObjects->toArray() |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments