|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Jstack\Newyse\Tests\Mapper; |
| 4 | + |
| 5 | +use Jstack\Newyse\Mapper\ResponseMapper; |
| 6 | +use Jstack\Newyse\Model\AccommodationType; |
| 7 | +use Jstack\Newyse\Model\Availability; |
| 8 | +use Jstack\Newyse\Model\Property; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class ResponseMapperTest extends TestCase |
| 12 | +{ |
| 13 | + public function testMapSimpleProperties() |
| 14 | + { |
| 15 | + $accommodationTypeDefinition = [ |
| 16 | + 'ResourceId' => 4, |
| 17 | + 'Code' => 'SomeCode' |
| 18 | + ]; |
| 19 | + |
| 20 | + $responseMapper = new ResponseMapper(); |
| 21 | + $accommodationType = $responseMapper->map($accommodationTypeDefinition, new AccommodationType()); |
| 22 | + |
| 23 | + $this->assertEquals(4, $accommodationType->getResourceId()); |
| 24 | + $this->assertEquals('SomeCode', $accommodationType->getCode()); |
| 25 | + } |
| 26 | + |
| 27 | + public function testMapOneToManyRelations() |
| 28 | + { |
| 29 | + $availabilityDefinition = [ |
| 30 | + 'Prices' => (object) [ |
| 31 | + 'PriceItem' => [ |
| 32 | + ['Price' => 1000], |
| 33 | + ['Price' => 2000] |
| 34 | + ] |
| 35 | + ] |
| 36 | + ]; |
| 37 | + |
| 38 | + $responseMapper = new ResponseMapper(); |
| 39 | + $availability = $responseMapper->map($availabilityDefinition, new Availability()); |
| 40 | + |
| 41 | + $this->assertEquals(2000, $availability->getPrices()[1]->getPrice()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testMapOneToManyRelationsWithOnlyOneChild() |
| 45 | + { |
| 46 | + $availabilityDefinition = [ |
| 47 | + 'Prices' => (object) [ |
| 48 | + 'PriceItem' => (object) ['Price' => 1000] |
| 49 | + ] |
| 50 | + ]; |
| 51 | + |
| 52 | + $responseMapper = new ResponseMapper(); |
| 53 | + $availability = $responseMapper->map($availabilityDefinition, new Availability()); |
| 54 | + |
| 55 | + $this->assertEquals(1000, $availability->getPrices()[0]->getPrice()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testMapOneToOneRelations() |
| 59 | + { |
| 60 | + $accommodationTypeDefinition = [ |
| 61 | + 'ComplexDefinition' => (object) [ |
| 62 | + 'Code' => 'Some Code', |
| 63 | + 'ComplexDefinitionId' => 123 |
| 64 | + ] |
| 65 | + ]; |
| 66 | + |
| 67 | + $responseMapper = new ResponseMapper(); |
| 68 | + $accommodationType = $responseMapper->map($accommodationTypeDefinition, new AccommodationType()); |
| 69 | + |
| 70 | + $this->assertEquals('Some Code', $accommodationType->getComplexDefinition()->getCode()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testMapDateTime() |
| 74 | + { |
| 75 | + $propertyDefinition = [ |
| 76 | + 'StartDate' => '2019-01-01 12:00:00' |
| 77 | + ]; |
| 78 | + |
| 79 | + $responseMapper = new ResponseMapper(); |
| 80 | + $property = $responseMapper->map($propertyDefinition, new Property()); |
| 81 | + |
| 82 | + $this->assertSame('2019-01-01 12:00:00', $property->getStartDate()->format('Y-m-d H:i:s')); |
| 83 | + } |
| 84 | + |
| 85 | + public function testMapArray() |
| 86 | + { |
| 87 | + $array = [ |
| 88 | + ['ResourceId' => 4, 'Code' => 'Code1'], |
| 89 | + ['ResourceId' => 6, 'Code' => 'Code2'] |
| 90 | + ]; |
| 91 | + |
| 92 | + $responseMapper = new ResponseMapper(); |
| 93 | + $accommodationTypes = $responseMapper->mapArray($array, new AccommodationType()); |
| 94 | + |
| 95 | + $this->assertSame('Code2', $accommodationTypes[1]->getCode()); |
| 96 | + } |
| 97 | +} |
0 commit comments