Skip to content

Commit

Permalink
test: allow changing shipping methods in mock repository
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 15, 2024
1 parent 78bdece commit 6e05b9c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 20 deletions.
51 changes: 31 additions & 20 deletions tests/Bootstrap/MockPdkShippingMethodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,40 @@
use MyParcelNL\Pdk\App\ShippingMethod\Collection\PdkShippingMethodCollection;
use MyParcelNL\Pdk\App\ShippingMethod\Contract\PdkShippingMethodRepositoryInterface;
use MyParcelNL\Pdk\App\ShippingMethod\Model\PdkShippingMethod;
use function MyParcelNL\Pdk\Tests\factory;
use Symfony\Contracts\Service\ResetInterface;

class MockPdkShippingMethodRepository implements PdkShippingMethodRepositoryInterface
class MockPdkShippingMethodRepository implements PdkShippingMethodRepositoryInterface, ResetInterface
{
/**
* @var array<\MyParcelNL\Pdk\App\ShippingMethod\Model\PdkShippingMethod>
*/
private $shippingMethods = [];

/**
* @param \MyParcelNL\Pdk\App\ShippingMethod\Model\PdkShippingMethod ...$shippingMethods
*
* @return void
*/
public function add(PdkShippingMethod ...$shippingMethods): void
{
foreach ($shippingMethods as $method) {
$this->shippingMethods[] = $method;
}
}

/**
* @return \MyParcelNL\Pdk\App\ShippingMethod\Collection\PdkShippingMethodCollection
*/
public function all(): PdkShippingMethodCollection
{
return new PdkShippingMethodCollection([
factory(PdkShippingMethod::class)
->withId('shipping:1')
->withName('Shipping 1')
->withIsEnabled(true)
->make(),
factory(PdkShippingMethod::class)
->withId('shipping:2')
->withName('Shipping 2')
->withIsEnabled(false)
->make(),
factory(PdkShippingMethod::class)
->withId('shipping:3')
->withName('Shipping 3')
->withDescription('My description')
->withIsEnabled(true)
->make(),
]);
return new PdkShippingMethodCollection($this->shippingMethods);
}

/**
* @return void
*/
public function reset(): void
{
$this->shippingMethods = [];
}
}
23 changes: 23 additions & 0 deletions tests/Bootstrap/TestBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MyParcelNL\Pdk\Account\Collection\ShopCollection;
use MyParcelNL\Pdk\Account\Model\Account;
use MyParcelNL\Pdk\Account\Platform;
use MyParcelNL\Pdk\App\ShippingMethod\Model\PdkShippingMethod;
use MyParcelNL\Pdk\Settings\Model\AccountSettings;
use MyParcelNL\Pdk\Tests\Factory\Contract\CollectionFactoryInterface;
use MyParcelNL\Pdk\Tests\Factory\Contract\ModelFactoryInterface;
Expand Down Expand Up @@ -39,4 +40,26 @@ public static function hasApiKey(string $apiKey = self::API_KEY_VALID): void
->withApiKey($apiKey)
->store();
}

public static function hasShippingMethods(): void
{
factory(PdkShippingMethod::class)
->withId('shipping:1')
->withName('Shipping 1')
->withIsEnabled(true)
->store();

factory(PdkShippingMethod::class)
->withId('shipping:2')
->withName('Shipping 2')
->withIsEnabled(false)
->store();

factory(PdkShippingMethod::class)
->withId('shipping:3')
->withName('Shipping 3')
->withDescription('My description')
->withIsEnabled(true)
->store();
}
}
1 change: 1 addition & 0 deletions tests/Bootstrap/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function getResetServices(): array
MockMemoryCacheStorage::class,
MockOrderStatusService::class,
MockPdkActionsService::class,
MockPdkShippingMethodRepository::class,
MockPdkWebhookManager::class,
MockPdkWebhooksRepository::class,
SharedFactoryState::class,
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Frontend/Service/FrontendRenderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

beforeEach(function () {
TestBootstrapper::hasAccount();
TestBootstrapper::hasShippingMethods();

factory(Settings::class)->store();
});
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Frontend/View/SettingsViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

it('gets settings view', function (string $class) {
TestBootstrapper::hasAccount();
TestBootstrapper::hasShippingMethods();

/** @var \MyParcelNL\Pdk\Frontend\View\AbstractSettingsView $view */
$view = Pdk::get($class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

namespace MyParcelNL\Pdk\App\ShippingMethod\Model;

use MyParcelNL\Pdk\App\ShippingMethod\Contract\PdkShippingMethodRepositoryInterface;
use MyParcelNL\Pdk\Base\Model\Address;
use MyParcelNL\Pdk\Base\Model\AddressFactory;
use MyParcelNL\Pdk\Base\Model\Model;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Shipment\Collection\PackageTypeCollection;
use MyParcelNL\Pdk\Shipment\Model\PackageTypeFactory;
use MyParcelNL\Pdk\Tests\Factory\Model\AbstractModelFactory;
Expand All @@ -29,4 +32,16 @@ public function getModel(): string
{
return PdkShippingMethod::class;
}

/**
* @param T $model
*
* @return void
*/
protected function save(Model $model): void
{
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockPdkShippingMethodRepository $shippingMethodRepository */
$shippingMethodRepository = Pdk::get(PdkShippingMethodRepositoryInterface::class);
$shippingMethodRepository->add($model);
}
}

0 comments on commit 6e05b9c

Please sign in to comment.