-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Contact- and AccountDataProviderResolver (#122)
- Loading branch information
Showing
5 changed files
with
470 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
Content/DataProviderResolver/AccountDataProviderResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver; | ||
|
||
use JMS\Serializer\SerializationContext; | ||
use Sulu\Bundle\ContactBundle\Api\Account; | ||
use Sulu\Bundle\ContactBundle\Entity\Account as AccountEntity; | ||
use Sulu\Bundle\HeadlessBundle\Content\Serializer\AccountSerializerInterface; | ||
use Sulu\Component\Content\Compat\PropertyParameter; | ||
use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface; | ||
use Sulu\Component\SmartContent\DataProviderInterface; | ||
|
||
class AccountDataProviderResolver implements DataProviderResolverInterface | ||
{ | ||
public static function getDataProvider(): string | ||
{ | ||
return 'accounts'; | ||
} | ||
|
||
/** | ||
* @var DataProviderInterface | ||
*/ | ||
private $accountDataProvider; | ||
|
||
/** | ||
* @var AccountSerializerInterface | ||
*/ | ||
private $accountSerializer; | ||
|
||
public function __construct( | ||
DataProviderInterface $accountDataProvider, | ||
AccountSerializerInterface $accountSerializer | ||
) { | ||
$this->accountDataProvider = $accountDataProvider; | ||
$this->accountSerializer = $accountSerializer; | ||
} | ||
|
||
public function getProviderConfiguration(): ProviderConfigurationInterface | ||
{ | ||
return $this->accountDataProvider->getConfiguration(); | ||
} | ||
|
||
/** | ||
* @return PropertyParameter[] | ||
*/ | ||
public function getProviderDefaultParams(): array | ||
{ | ||
return $this->accountDataProvider->getDefaultPropertyParameter(); | ||
} | ||
|
||
public function resolve( | ||
array $filters, | ||
array $propertyParameters, | ||
array $options = [], | ||
?int $limit = null, | ||
int $page = 1, | ||
?int $pageSize = null | ||
): DataProviderResult { | ||
$providerResult = $this->accountDataProvider->resolveResourceItems( | ||
$filters, | ||
$propertyParameters, | ||
$options, | ||
$limit, | ||
$page, | ||
$pageSize | ||
); | ||
|
||
/** @var string $locale */ | ||
$locale = $options['locale']; | ||
|
||
$items = []; | ||
foreach ($providerResult->getItems() as $providerItem) { | ||
/** @var Account $account */ | ||
$account = $providerItem->getResource(); | ||
/** @var AccountEntity $accountEntity */ | ||
$accountEntity = $account->getEntity(); | ||
|
||
$items[] = $this->accountSerializer->serialize( | ||
$accountEntity, | ||
$locale, | ||
SerializationContext::create()->setGroups(['partialAccount']) | ||
); | ||
} | ||
|
||
return new DataProviderResult($items, $providerResult->getHasNextPage()); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
Content/DataProviderResolver/ContactDataProviderResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver; | ||
|
||
use JMS\Serializer\SerializationContext; | ||
use Sulu\Bundle\ContactBundle\Api\Contact; | ||
use Sulu\Bundle\ContactBundle\Entity\Contact as ContactEntity; | ||
use Sulu\Bundle\HeadlessBundle\Content\Serializer\ContactSerializerInterface; | ||
use Sulu\Component\Content\Compat\PropertyParameter; | ||
use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface; | ||
use Sulu\Component\SmartContent\DataProviderInterface; | ||
|
||
class ContactDataProviderResolver implements DataProviderResolverInterface | ||
{ | ||
public static function getDataProvider(): string | ||
{ | ||
return 'contacts'; | ||
} | ||
|
||
/** | ||
* @var DataProviderInterface | ||
*/ | ||
private $contactDataProvider; | ||
|
||
/** | ||
* @var ContactSerializerInterface | ||
*/ | ||
private $contactSerializer; | ||
|
||
public function __construct( | ||
DataProviderInterface $contactDataProvider, | ||
ContactSerializerInterface $contactSerializer | ||
) { | ||
$this->contactDataProvider = $contactDataProvider; | ||
$this->contactSerializer = $contactSerializer; | ||
} | ||
|
||
public function getProviderConfiguration(): ProviderConfigurationInterface | ||
{ | ||
return $this->contactDataProvider->getConfiguration(); | ||
} | ||
|
||
/** | ||
* @return PropertyParameter[] | ||
*/ | ||
public function getProviderDefaultParams(): array | ||
{ | ||
return $this->contactDataProvider->getDefaultPropertyParameter(); | ||
} | ||
|
||
public function resolve( | ||
array $filters, | ||
array $propertyParameters, | ||
array $options = [], | ||
?int $limit = null, | ||
int $page = 1, | ||
?int $pageSize = null | ||
): DataProviderResult { | ||
$providerResult = $this->contactDataProvider->resolveResourceItems( | ||
$filters, | ||
$propertyParameters, | ||
$options, | ||
$limit, | ||
$page, | ||
$pageSize | ||
); | ||
|
||
/** @var string $locale */ | ||
$locale = $options['locale']; | ||
|
||
$items = []; | ||
foreach ($providerResult->getItems() as $providerItem) { | ||
/** @var Contact $contact */ | ||
$contact = $providerItem->getResource(); | ||
/** @var ContactEntity $contactEntity */ | ||
$contactEntity = $contact->getEntity(); | ||
|
||
$items[] = $this->contactSerializer->serialize( | ||
$contactEntity, | ||
$locale, | ||
SerializationContext::create()->setGroups(['partialContact']) | ||
); | ||
} | ||
|
||
return new DataProviderResult($items, $providerResult->getHasNextPage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
Tests/Unit/Content/DataProviderResolver/AccountDataProviderResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\HeadlessBundle\Tests\Unit\Content\DataProviderResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Sulu\Bundle\ContactBundle\Api\Account; | ||
use Sulu\Bundle\ContactBundle\Entity\AccountInterface; | ||
use Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver\AccountDataProviderResolver; | ||
use Sulu\Bundle\HeadlessBundle\Content\Serializer\AccountSerializerInterface; | ||
use Sulu\Component\Contact\SmartContent\AccountDataProvider; | ||
use Sulu\Component\Content\Compat\PropertyParameter; | ||
use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface; | ||
use Sulu\Component\SmartContent\DataProviderResult; | ||
use Sulu\Component\SmartContent\ResourceItemInterface; | ||
|
||
class AccountDataProviderResolverTest extends TestCase | ||
{ | ||
/** | ||
* @var AccountDataProvider|ObjectProphecy | ||
*/ | ||
private $accountDataProvider; | ||
|
||
/** | ||
* @var AccountSerializerInterface|ObjectProphecy | ||
*/ | ||
private $accountSerializer; | ||
|
||
/** | ||
* @var AccountDataProviderResolver | ||
*/ | ||
private $accountResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->accountDataProvider = $this->prophesize(AccountDataProvider::class); | ||
$this->accountSerializer = $this->prophesize(AccountSerializerInterface::class); | ||
|
||
$this->accountResolver = new AccountDataProviderResolver( | ||
$this->accountDataProvider->reveal(), | ||
$this->accountSerializer->reveal() | ||
); | ||
} | ||
|
||
public function testGetDataProvider(): void | ||
{ | ||
self::assertSame('accounts', $this->accountResolver::getDataProvider()); | ||
} | ||
|
||
public function testGetProviderConfiguration(): void | ||
{ | ||
$configuration = $this->prophesize(ProviderConfigurationInterface::class); | ||
$this->accountDataProvider->getConfiguration()->willReturn($configuration->reveal()); | ||
|
||
$this->assertSame($configuration->reveal(), $this->accountResolver->getProviderConfiguration()); | ||
} | ||
|
||
public function testGetProviderDefaultParams(): void | ||
{ | ||
$propertyParameter = $this->prophesize(PropertyParameter::class); | ||
$this->accountDataProvider->getDefaultPropertyParameter()->willReturn(['test' => $propertyParameter->reveal()]); | ||
|
||
$this->assertSame(['test' => $propertyParameter->reveal()], $this->accountResolver->getProviderDefaultParams()); | ||
} | ||
|
||
public function testResolve(): void | ||
{ | ||
$account1 = $this->prophesize(AccountInterface::class); | ||
$apiAccount1 = $this->prophesize(Account::class); | ||
$apiAccount1->getEntity()->willReturn($account1->reveal()); | ||
|
||
$account2 = $this->prophesize(AccountInterface::class); | ||
$apiAccount2 = $this->prophesize(Account::class); | ||
$apiAccount2->getEntity()->willReturn($account2->reveal()); | ||
|
||
$resourceItem1 = $this->prophesize(ResourceItemInterface::class); | ||
$resourceItem1->getResource()->willReturn($apiAccount1->reveal()); | ||
|
||
$resourceItem2 = $this->prophesize(ResourceItemInterface::class); | ||
$resourceItem2->getResource()->willReturn($apiAccount2->reveal()); | ||
|
||
$providerResult = $this->prophesize(DataProviderResult::class); | ||
$providerResult->getHasNextPage()->willReturn(true); | ||
$providerResult->getItems()->willReturn([$resourceItem1, $resourceItem2]); | ||
|
||
$this->accountDataProvider->resolveResourceItems([], [], ['locale' => 'en'], 10, 1, 5)->willReturn($providerResult->reveal()); | ||
|
||
$this->accountSerializer->serialize($account1, 'en', Argument::cetera())->willReturn([ | ||
'id' => 1, | ||
'name' => 'Account 1', | ||
]); | ||
|
||
$this->accountSerializer->serialize($account2, 'en', Argument::cetera())->willReturn([ | ||
'id' => 2, | ||
'name' => 'Account 2', | ||
]); | ||
|
||
$result = $this->accountResolver->resolve([], [], ['locale' => 'en'], 10, 1, 5); | ||
|
||
$this->assertTrue($result->getHasNextPage()); | ||
$this->assertSame( | ||
[ | ||
[ | ||
'id' => 1, | ||
'name' => 'Account 1', | ||
], | ||
[ | ||
'id' => 2, | ||
'name' => 'Account 2', | ||
], | ||
], | ||
$result->getItems() | ||
); | ||
} | ||
} |
Oops, something went wrong.