Skip to content

Commit

Permalink
test: test registering hooks on install
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 2, 2023
1 parent 05bd9b2 commit cb03dc5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 13 deletions.
9 changes: 8 additions & 1 deletion tests/Mock/BaseMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Sdk\src\Support\Str;

class BaseMock implements Arrayable
Expand Down Expand Up @@ -120,7 +121,13 @@ public function setAttribute(string $key, $value): self
*/
public function toArray(?int $flags = null): array
{
return $this->attributes;
$attributes = $flags & Arrayable::SKIP_NULL ? Utils::filterNull($this->attributes) : $this->attributes;

return array_map(static function ($value) use ($flags) {
return $value instanceof Arrayable
? $value->toArray($flags)
: $value;
}, $attributes);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Mock/MockPsObjectModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MyParcelNL\PrestaShop\Tests\Mock;

use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Sdk\src\Support\Str;
use PrestaShop\PrestaShop\Core\Foundation\Database\EntityInterface;
Expand Down Expand Up @@ -35,7 +36,7 @@ public function __construct(?int $id = null, ?int $id_lang = null, ?int $id_shop
return;
}

$this->hydrate($existing->toArray());
$this->hydrate($existing->toArray(Arrayable::SKIP_NULL));
}
}

Expand Down
30 changes: 22 additions & 8 deletions tests/Unit/Pdk/Installer/Service/PsInstallerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

namespace MyParcelNL\PrestaShop\Pdk\Installer\Service;

use Hook;
use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Facade\Installer;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsDb;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsObjectModels;
use MyParcelNL\PrestaShop\Tests\Uses\UsesMockPlugin;
use MyParcelNL\Sdk\src\Support\Collection;
use Tab;
use function MyParcelNL\Pdk\Tests\usesShared;
use function MyParcelNL\PrestaShop\psFactory;
Expand All @@ -26,7 +29,7 @@

usesShared(new UsesMockPlugin());

it('executes database migrations', function () {
it('installs: executes database migrations', function () {
/** @var \MyParcelNL $module */
$module = Pdk::get('moduleInstance');

Expand All @@ -37,21 +40,32 @@
expect(MockPsDb::getDatabase())->toHaveKeys(MYPARCEL_TABLES);
});

it('installs tabs', function () {
it('installs: registers hooks', function () {
/** @var \MyParcelNL $module */
$module = Pdk::get('moduleInstance');

expect(MockPsObjectModels::getByClass(Hook::class))->toBeEmpty();
Installer::install($module);

$createdHooks = MockPsObjectModels::getByClass(Hook::class);

expect($createdHooks)->not->toBeEmpty();
$this->assertMatchesJsonSnapshot(json_encode($createdHooks->toArray(Arrayable::SKIP_NULL)));
});

it('installs: installs tabs', function () {
/** @var \MyParcelNL $module */
$module = Pdk::get('moduleInstance');
/** @var \PrestaShopBundle\Entity\Repository\TabRepository $tabRepository */
$tabRepository = Pdk::get('ps.tabRepository');

expect($tabRepository->findAll())->toBeEmpty();
expect(MockPsObjectModels::getByClass(Tab::class))->toBeEmpty();
Installer::install($module);

$this->assertMatchesJsonSnapshot(
json_encode((new Collection($tabRepository->findByModule($module->name)))->toArray())
json_encode((new Collection(MockPsObjectModels::getByClass(Tab::class)))->toArray(Arrayable::SKIP_NULL))
);
});

it('doesn\'t add same tab twice', function () {
it('installs: doesn\'t add same tab twice', function () {
/** @var \MyParcelNL $module */
$module = Pdk::get('moduleInstance');
/** @var \PrestaShopBundle\Entity\Repository\TabRepository $tabRepository */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"1": {
"id_lang": null,
"id_shop": null,
"active": 1,
"class_name": "MyParcelNLAdminSettings",
"route_name": "myparcelnl_settings",
"name": {
"1": "MyParcelNL"
},
"id_parent": null,
"module": "myparcelnl",
"id": 1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"1": {
"name": "displayBeforeCarrier",
"id": 1
},
"2": {
"name": "displayCarrierExtraContent",
"id": 2
},
"3": {
"name": "actionCarrierProcess",
"id": 3
},
"4": {
"name": "actionOrderGridDefinitionModifier",
"id": 4
},
"5": {
"name": "actionOrderGridPresenterModifier",
"id": 5
},
"6": {
"name": "actionObjectCustomerMessageAddAfter",
"id": 6
},
"7": {
"name": "displayAdminOrderMain",
"id": 7
},
"8": {
"name": "actionProductUpdate",
"id": 8
},
"9": {
"name": "displayAdminProductsExtra",
"id": 9
},
"10": {
"name": "displayAdminAfterHeader",
"id": 10
},
"11": {
"name": "displayAdminEndContent",
"id": 11
},
"12": {
"name": "displayBackOfficeHeader",
"id": 12
},
"13": {
"name": "displayHeader",
"id": 13
},
"14": {
"name": "actionCarrierUpdate",
"id": 14
}
}
5 changes: 5 additions & 0 deletions tests/mock_class_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ abstract class GroupCore extends ObjectModel { }

final class Group extends GroupCore { }

/** @see \HookCore */
abstract class HookCore extends ObjectModel { }

final class Hook extends HookCore { }

/** @see \LangCore */
abstract class LangCore extends ObjectModel { }

Expand Down

0 comments on commit cb03dc5

Please sign in to comment.