-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API] Refactoring of lazy customer loader
- Loading branch information
1 parent
f9567d6
commit 7e8d1fe
Showing
5 changed files
with
131 additions
and
84 deletions.
There are no files selected for viewing
73 changes: 0 additions & 73 deletions
73
src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/Loader/LazyCustomerLoader.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,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\AdminApiBundle\Form\ChoiceList\Loader; | ||
|
||
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; | ||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
|
||
final class LazyCustomerLoader implements ChoiceLoaderInterface | ||
{ | ||
/** @var CustomerRepositoryInterface */ | ||
private $customerRepository; | ||
|
||
public function __construct(CustomerRepositoryInterface $customerRepository) | ||
{ | ||
$this->customerRepository = $customerRepository; | ||
} | ||
|
||
public function loadChoiceList($value = null): ChoiceListInterface | ||
{ | ||
return new ArrayChoiceList([], $value); | ||
} | ||
|
||
public function loadChoicesForValues(array $values, $value = null): array | ||
{ | ||
return $this->customerRepository->findBy(['email' => $values]); | ||
} | ||
|
||
public function loadValuesForChoices(array $choices, $value = null): array | ||
{ | ||
/** Intentionally left blank, as in the only usage of this loader is in the context of api, where we don't need to load choices */ | ||
return []; | ||
} | ||
} |
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
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
63 changes: 63 additions & 0 deletions
63
src/Sylius/Bundle/AdminApiBundle/spec/Form/ChoiceList/Loader/LazyCustomerLoaderSpec.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,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\AdminApiBundle\Form\ChoiceList\Loader; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Component\Core\Model\CustomerInterface; | ||
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; | ||
use Sylius\Component\User\Model\UserAwareInterface; | ||
use Sylius\Component\User\Model\UserInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
use Symfony\Component\Form\Form; | ||
use Symfony\Component\Form\FormEvent; | ||
|
||
final class LazyCustomerLoaderSpec extends ObjectBehavior | ||
{ | ||
function let(CustomerRepositoryInterface $customerRepository): void | ||
{ | ||
$this->beConstructedWith($customerRepository); | ||
} | ||
|
||
function it_is_choice_loader(): void | ||
{ | ||
$this->shouldImplement(ChoiceLoaderInterface::class); | ||
} | ||
|
||
function it_loads_customers_by_email( | ||
CustomerRepositoryInterface $customerRepository, | ||
CustomerInterface $firstCustomer, | ||
CustomerInterface $secondCustomer | ||
): void { | ||
$customerRepository | ||
->findBy(['email' => ['first@example.com', 'second@example.com']]) | ||
->willReturn([$firstCustomer, $secondCustomer]) | ||
; | ||
|
||
$this | ||
->loadChoicesForValues(['first@example.com', 'second@example.com']) | ||
->shouldReturn([$firstCustomer, $secondCustomer]) | ||
; | ||
} | ||
|
||
function it_does_not_load_any_choices_available(): void { | ||
$this->loadValuesForChoices([])->shouldReturn([]); | ||
} | ||
|
||
function it_provides_empty_array_choice_list(): void { | ||
$this->loadChoiceList()->shouldBeAnInstanceOf(ArrayChoiceList::class); | ||
} | ||
} |