|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\AdminUi\Form\DataMapper; |
| 10 | + |
| 11 | +use Ibexa\AdminUi\Exception\InvalidArgumentException; |
| 12 | +use Ibexa\AdminUi\Form\DataMapper\ContentMainLocationUpdateMapper; |
| 13 | +use Ibexa\Contracts\Core\Repository\LocationService; |
| 14 | +use Ibexa\Contracts\Core\Repository\Values\Content\ContentMetadataUpdateStruct; |
| 15 | +use Ibexa\Contracts\Core\Repository\Values\Content\Location; |
| 16 | +use Ibexa\Contracts\Core\Repository\Values\ValueObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * @covers \Ibexa\AdminUi\Form\DataMapper\ContentMainLocationUpdateMapper |
| 21 | + */ |
| 22 | +final class ContentMainLocationUpdateMapperTest extends TestCase |
| 23 | +{ |
| 24 | + /** @var \Ibexa\Contracts\Core\Repository\LocationService&\PHPUnit\Framework\MockObject\MockObject */ |
| 25 | + private LocationService $locationService; |
| 26 | + |
| 27 | + private ContentMainLocationUpdateMapper $mapper; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + $this->locationService = $this->createMock(LocationService::class); |
| 32 | + $this->mapper = new ContentMainLocationUpdateMapper($this->locationService); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
| 37 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException |
| 38 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException |
| 39 | + */ |
| 40 | + public function testMapWithMainLocationId(): void |
| 41 | + { |
| 42 | + $mainLocationId = 42; |
| 43 | + $location = $this->createMock(Location::class); |
| 44 | + |
| 45 | + $struct = new ContentMetadataUpdateStruct(['mainLocationId' => $mainLocationId]); |
| 46 | + |
| 47 | + $this->locationService |
| 48 | + ->expects(self::once()) |
| 49 | + ->method('loadLocation') |
| 50 | + ->with($mainLocationId) |
| 51 | + ->willReturn($location); |
| 52 | + |
| 53 | + $data = $this->mapper->map($struct); |
| 54 | + |
| 55 | + self::assertSame($location, $data->getLocation()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException |
| 60 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
| 61 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException |
| 62 | + */ |
| 63 | + public function testMapWithNullMainLocationId(): void |
| 64 | + { |
| 65 | + $struct = new ContentMetadataUpdateStruct(['mainLocationId' => null]); |
| 66 | + |
| 67 | + $this->locationService |
| 68 | + ->expects(self::never()) |
| 69 | + ->method('loadLocation'); |
| 70 | + |
| 71 | + $data = $this->mapper->map($struct); |
| 72 | + |
| 73 | + self::assertNull($data->getLocation()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
| 78 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException |
| 79 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException |
| 80 | + */ |
| 81 | + public function testMapThrowsOnInvalidValueObject(): void |
| 82 | + { |
| 83 | + $this->expectException(InvalidArgumentException::class); |
| 84 | + $this->mapper->map($this->createMock(ValueObject::class)); |
| 85 | + } |
| 86 | +} |
0 commit comments