Skip to content

Commit 938a278

Browse files
authored
Fixed setting location data in ContentMainLocationUpdateMapper (#1615)
* Fixed setting location data in ContentMainLocationUpdateMapper * [Tests] Added minimal test coverage for ContentMainLocationUpdateMapper
1 parent 8a4ec0f commit 938a278

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

src/lib/Form/DataMapper/ContentMainLocationUpdateMapper.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Ibexa\AdminUi\Exception\InvalidArgumentException;
1111
use Ibexa\AdminUi\Form\Data\Content\Location\ContentMainLocationUpdateData;
1212
use Ibexa\Contracts\AdminUi\Form\DataMapper\DataMapperInterface;
13+
use Ibexa\Contracts\Core\Repository\LocationService;
1314
use Ibexa\Contracts\Core\Repository\Values\Content\ContentMetadataUpdateStruct;
1415
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
1516

@@ -18,14 +19,21 @@
1819
*/
1920
class ContentMainLocationUpdateMapper implements DataMapperInterface
2021
{
22+
private LocationService $locationService;
23+
24+
public function __construct(LocationService $locationService)
25+
{
26+
$this->locationService = $locationService;
27+
}
28+
2129
/**
2230
* Maps given ContentMetadataUpdateStruct object to a ContentMainLocationUpdateData object.
2331
*
2432
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentMetadataUpdateStruct|\Ibexa\Contracts\Core\Repository\Values\ValueObject $value
2533
*
26-
* @return \Ibexa\AdminUi\Form\Data\Content\Location\ContentMainLocationUpdateData
27-
*
28-
* @throws \Ibexa\AdminUi\Exception\InvalidArgumentException
34+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
35+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
36+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
2937
*/
3038
public function map(ValueObject $value): ContentMainLocationUpdateData
3139
{
@@ -35,7 +43,9 @@ public function map(ValueObject $value): ContentMainLocationUpdateData
3543

3644
$data = new ContentMainLocationUpdateData();
3745

38-
$data->setLocation($value->mainLocationId);
46+
if (null !== $value->mainLocationId) {
47+
$data->setLocation($this->locationService->loadLocation($value->mainLocationId));
48+
}
3949

4050
return $data;
4151
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)