Skip to content

Commit

Permalink
test: add installer test
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 2, 2023
1 parent 7e08891 commit 12a2e9e
Show file tree
Hide file tree
Showing 12 changed files with 4,270 additions and 1,455 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"php": ">=7.2.0"
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.3",
"doctrine/orm": "^2.7",
"guzzlehttp/guzzle": "^7.4",
"pestphp/pest": "^1.22",
"spatie/pest-plugin-snapshots": "^1.1.0"
Expand Down
5,534 changes: 4,083 additions & 1,451 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/Mock/BaseMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Sdk\src\Support\Str;

abstract class BaseMock implements Arrayable
class BaseMock implements Arrayable
{
/**
* @var array
Expand Down
1 change: 1 addition & 0 deletions tests/Mock/MockPsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private function setupContainer(): void
$builder->addDefinitions([
'doctrine.orm.entity_manager' => get(MockPsEntityManager::class),
'prestashop.adapter.legacy.configuration' => get(Configuration::class),
'prestashop.core.admin.tab.repository' => get(MockPsTabRepository::class),
'ps.entityManager' => get(MockPsEntityManager::class),
]);

Expand Down
9 changes: 7 additions & 2 deletions tests/Mock/MockPsEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use MyParcelNL\PrestaShop\Tests\Bootstrap\Contract\StaticMockInterface;
use ObjectModel;

final class MockPsEntityManager implements StaticMockInterface
final class MockPsEntityManager extends BaseMock implements StaticMockInterface
{
private static $repositories = [];

Expand All @@ -16,7 +16,12 @@ public static function reset(): void
self::$repositories = [];
}

public function flush(): void {}
public function flush(): void { }

public function getConfiguration()
{
return new BaseMock();
}

/**
* @param class-string<ObjectModel> $entityName
Expand Down
2 changes: 1 addition & 1 deletion tests/Mock/MockPsEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @template T of EntityInterface
*/
final class MockPsEntityRepository
class MockPsEntityRepository
{
/**
* @var class-string<T>
Expand Down
68 changes: 68 additions & 0 deletions tests/Mock/MockPsTabRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Tests\Mock;

use Tab;

/**
* @see \PrestaShopBundle\Entity\Repository\TabRepository
*/
final class MockPsTabRepository extends MockPsEntityRepository
{
public function __construct()
{
parent::__construct(Tab::class);
}

/**
* @param string $moduleName
*
* @return Tab[]
*/
public function findByModule(string $moduleName): array
{
return $this
->findBy(['module' => $moduleName])
->all();
}

/**
* @param int $idParent
*
* @return Tab[]
*/
public function findByParentId(int $idParent): array
{
return $this
->findBy(['idParent' => $idParent])
->all();
}

/**
* @param string $className
*
* @return Tab|null
*/
public function findOneByClassName(string $className): ?Tab
{
return $this->findOneBy(['className' => $className]);
}

/**
* @param string $className
*
* @return int|null
*/
public function findOneIdByClassName(string $className)
{
$tab = $this->findOneByClassName($className);

if ($tab) {
return $tab->getId();
}

return null;
}
}
48 changes: 48 additions & 0 deletions tests/Unit/Pdk/Installer/Service/PsInstallerServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */

/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Pdk\Installer\Service;

use MyParcelNL\Pdk\Facade\Installer;
use MyParcelNL\Pdk\Facade\Pdk;
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;

usesShared(new UsesMockPlugin());

it('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();
Installer::install($module);

$this->assertMatchesJsonSnapshot(
json_encode((new Collection($tabRepository->findByModule($module->name)))->toArray())
);
});

it('doesn\'t add same tab twice', function () {
/** @var \MyParcelNL $module */
$module = Pdk::get('moduleInstance');
/** @var \PrestaShopBundle\Entity\Repository\TabRepository $tabRepository */
$tabRepository = Pdk::get('ps.tabRepository');

psFactory(Tab::class)
->withModule($module->name)
->withClassName(Pdk::get('legacyControllerSettings'))
->store();

expect($tabRepository->findByModule($module->name))->toHaveCount(1);
Installer::install($module);
expect($tabRepository->findByModule($module->name))->toHaveCount(1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"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
}
}
23 changes: 23 additions & 0 deletions tests/factories/TabFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithLang;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithShop;

/**
* @method $this withIdParent(int $idParent)
* @method $this withActive(bool $active)
* @method $this withClassName(string $className)
* @method $this withModule(string $module)
* @method $this withName(array $name)
* @method $this withRouteName(string $routeName)
*/
final class TabFactory extends AbstractPsObjectModelFactory implements WithLang, WithShop
{
protected function getObjectModelClass(): string
{
return Tab::class;
}
}
11 changes: 11 additions & 0 deletions tests/mock_class_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

declare(strict_types=1);

use MyParcelNL\PrestaShop\Tests\Mock\BaseMock;
use MyParcelNL\PrestaShop\Tests\Mock\MockPrestaShopLogger;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsCarrier;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsConfiguration;
Expand Down Expand Up @@ -52,6 +53,11 @@ abstract class DbQueryCore extends MockPsDbQuery { }

final class DbQuery extends DbQueryCore { }

/** @see \ToolsCore */
abstract class ToolsCore extends BaseMock { }

final class Tools extends ToolsCore { }

###
# Modules
###
Expand Down Expand Up @@ -224,6 +230,11 @@ abstract class SupplierCore extends ObjectModel { }

final class Supplier extends SupplierCore { }

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

final class Tab extends TabCore { }

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

Expand Down
10 changes: 10 additions & 0 deletions tests/mock_namespaced_class_map_after.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ class FrameworkBundleAdminController extends AbstractController { }
use MyParcelNL\PrestaShop\Tests\Mock\BaseMock;

class AbstractColumn extends BaseMock { }

namespace Doctrine\Common\Annotations;

use MyParcelNL\PrestaShop\Tests\Mock\BaseMock;

class DocParser extends BaseMock { }

class AnnotationReader extends BaseMock { }

class PsrCachedReader extends BaseMock { }

0 comments on commit 12a2e9e

Please sign in to comment.