diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 99e58e12..e00ff28b 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -13,4 +13,4 @@ jobs: name: "PHPUnit" uses: "doctrine/.github/.github/workflows/continuous-integration.yml@2.1.0" with: - php-versions: '["7.4", "8.0", "8.1", "8.2"]' + php-versions: '["8.0", "8.1", "8.2"]' diff --git a/composer.json b/composer.json index eacc7c0c..3e2dcd50 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ ], "homepage": "http://www.doctrine-project.org/", "require": { - "php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0", + "php": "~8.0.0 || ~8.1.0 || ~8.2.0", "composer-runtime-api": "^2.0", "doctrine/annotations": "^1.13.3 || ^2", "doctrine/cache": "^1.13.0 || ^2.1.0", diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 2e6fd3f5..06af6b2f 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -6,8 +6,8 @@ - - + + diff --git a/phpstan.neon b/phpstan.neon index b0c63772..12ac6ff9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -16,7 +16,6 @@ parameters: - message: '#mock an undefined method save\(\) on class Doctrine\\Common\\Cache\\ArrayCache#' path: tests/Cache/DoctrineCacheStorageTest.php - includes: - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon diff --git a/psalm.xml b/psalm.xml index 72b4005c..3923b42b 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,8 @@ setOptions($options); $this->inflector = $inflector ?? InflectorFactory::create()->build(); @@ -47,7 +46,7 @@ public function __construct($options = [], ?Inflector $inflector = null) /** * @param mixed[]|AuthenticationOptions $options */ - public function setOptions($options): self + public function setOptions(array|AuthenticationOptions $options): self { if (! $options instanceof AuthenticationOptions) { $options = new AuthenticationOptions($options); @@ -102,8 +101,8 @@ protected function validateIdentity(object $identity): AuthenticationResult sprintf( 'Property (%s) in (%s) is not accessible. You should implement %s::%s()', $credentialProperty, - get_class($identity), - get_class($identity), + $identity::class, + $identity::class, $getter ) ); diff --git a/src/Authentication/Storage/ObjectRepository.php b/src/Authentication/Storage/ObjectRepository.php index abad3709..9d477bff 100644 --- a/src/Authentication/Storage/ObjectRepository.php +++ b/src/Authentication/Storage/ObjectRepository.php @@ -17,7 +17,7 @@ class ObjectRepository implements StorageInterface /** * @param mixed[]|AuthenticationOptions $options */ - public function setOptions($options): ObjectRepository + public function setOptions(array|AuthenticationOptions $options): ObjectRepository { if (! $options instanceof AuthenticationOptions) { $options = new AuthenticationOptions($options); @@ -33,7 +33,7 @@ public function setOptions($options): ObjectRepository * * @param mixed[]|AuthenticationOptions $options */ - public function __construct($options = []) + public function __construct(array|AuthenticationOptions $options = []) { $this->setOptions($options); } @@ -60,18 +60,13 @@ public function read(): ?object /** * Will return the key of the identity. If only the key is needed, this avoids an * unnecessary db call - * - * @return mixed */ - public function readKeyOnly() + public function readKeyOnly(): mixed { return $identity = $this->options->getStorage()->read(); } - /** - * @param mixed $contents - */ - public function write($contents): void + public function write(mixed $contents): void { $metadataInfo = $this->options->getClassMetadata(); $identifierValues = $metadataInfo->getIdentifierValues($contents); diff --git a/src/Cache/DoctrineCacheStorage.php b/src/Cache/DoctrineCacheStorage.php index f1bde553..c1304b68 100644 --- a/src/Cache/DoctrineCacheStorage.php +++ b/src/Cache/DoctrineCacheStorage.php @@ -15,19 +15,15 @@ */ class DoctrineCacheStorage extends AbstractAdapter { - protected Cache $cache; - /** * {@inheritDoc} * * @param iterable|AdapterOptions $options * @param Cache $cache */ - public function __construct($options, Cache $cache) + public function __construct($options, protected Cache $cache) { parent::__construct($options); - - $this->cache = $cache; } /** diff --git a/src/Cache/LaminasStorageCache.php b/src/Cache/LaminasStorageCache.php index 600359e0..b56c210c 100644 --- a/src/Cache/LaminasStorageCache.php +++ b/src/Cache/LaminasStorageCache.php @@ -16,11 +16,8 @@ */ class LaminasStorageCache extends CacheProvider { - protected StorageInterface $storage; - - public function __construct(StorageInterface $storage) + public function __construct(protected StorageInterface $storage) { - $this->storage = $storage; } /** diff --git a/src/Form/Element/ObjectMultiCheckbox.php b/src/Form/Element/ObjectMultiCheckbox.php index 5459a969..44ab1773 100644 --- a/src/Form/Element/ObjectMultiCheckbox.php +++ b/src/Form/Element/ObjectMultiCheckbox.php @@ -28,12 +28,9 @@ public function setOptions(iterable $options): self } /** - * @param mixed $value - * @param mixed $key - * * @return $this */ - public function setOption($key, $value): self + public function setOption(mixed $key, mixed $value): self { $this->getProxy()->setOptions([$key => $value]); diff --git a/src/Form/Element/ObjectRadio.php b/src/Form/Element/ObjectRadio.php index 3cf9a9d3..0cd921c8 100644 --- a/src/Form/Element/ObjectRadio.php +++ b/src/Form/Element/ObjectRadio.php @@ -23,12 +23,9 @@ public function setOptions(iterable $options): self } /** - * @param mixed $value - * @param mixed $key - * * @return $this */ - public function setOption($key, $value): self + public function setOption(mixed $key, mixed $value): self { $this->getProxy()->setOptions([$key => $value]); diff --git a/src/Form/Element/ObjectSelect.php b/src/Form/Element/ObjectSelect.php index fb37b4ad..e16de534 100644 --- a/src/Form/Element/ObjectSelect.php +++ b/src/Form/Element/ObjectSelect.php @@ -28,12 +28,9 @@ public function setOptions(iterable $options): self } /** - * @param mixed $value - * @param mixed $key - * * @return $this */ - public function setOption($key, $value): self + public function setOption(mixed $key, mixed $value): self { $this->getProxy()->setOptions([$key => $value]); diff --git a/src/Form/Element/Proxy.php b/src/Form/Element/Proxy.php index d4389c07..ce7703fa 100644 --- a/src/Form/Element/Proxy.php +++ b/src/Form/Element/Proxy.php @@ -9,6 +9,7 @@ use Doctrine\Inflector\Inflector; use Doctrine\Inflector\InflectorFactory; use Doctrine\Persistence\ObjectManager; +use DoctrineModule\Form\Element\Exception\InvalidRepositoryResultException; use DoctrineModule\Persistence\ObjectManagerAwareInterface; use Laminas\Stdlib\ArrayUtils; use Laminas\Stdlib\Guard\ArrayOrTraversableGuardTrait; @@ -22,7 +23,6 @@ use function call_user_func; use function count; use function current; -use function get_class; use function gettype; use function is_callable; use function is_object; @@ -47,8 +47,7 @@ class Proxy implements ObjectManagerAwareInterface /** @var mixed[] */ protected array $findMethod = []; - /** @var mixed */ - protected $property; + protected mixed $property; /** @var mixed[] */ protected array $optionAttributes = []; @@ -131,10 +130,7 @@ public function setOptions(iterable $options): void $this->setOptgroupDefault($options['optgroup_default']); } - /** - * @return mixed - */ - public function getValueOptions() + public function getValueOptions(): mixed { if (empty($this->valueOptions)) { $this->loadValueOptions(); @@ -143,10 +139,7 @@ public function getValueOptions() return $this->valueOptions; } - /** - * @return mixed - */ - public function getObjects() + public function getObjects(): mixed { $this->loadObjects(); @@ -253,10 +246,7 @@ public function setProperty(string $property): Proxy return $this; } - /** - * @return mixed - */ - public function getProperty() + public function getProperty(): mixed { return $this->property; } @@ -332,10 +322,7 @@ public function getFindMethod(): array return $this->findMethod; } - /** - * @param mixed $targetEntity - */ - protected function generateLabel($targetEntity): ?string + protected function generateLabel(mixed $targetEntity): ?string { if ($this->getLabelGenerator() === null) { return null; @@ -345,13 +332,9 @@ protected function generateLabel($targetEntity): ?string } /** - * @param mixed $value - * - * @return mixed[]|mixed|object - * * @throws RuntimeException */ - public function getValue($value) + public function getValue(mixed $value): mixed { $metadata = $this->getObjectManager()->getClassMetadata($this->getTargetClass()); @@ -366,7 +349,7 @@ public function getValue($value) $value = $data; } else { - $metadata = $this->getObjectManager()->getClassMetadata(get_class($value)); + $metadata = $this->getObjectManager()->getClassMetadata($value::class); $identifier = $metadata->getIdentifierFieldNames(); if ($identifier !== null && count($identifier) > 1) { @@ -416,7 +399,7 @@ protected function loadObjects(): void sprintf( 'Method "%s" could not be found in repository "%s"', $findMethodName, - get_class($repository) + $repository::class ) ); } @@ -436,7 +419,7 @@ protected function loadObjects(): void . ' was not provided', $param->getName(), $findMethodName, - get_class($repository) + $repository::class ) ); } @@ -447,8 +430,8 @@ protected function loadObjects(): void $this->guardForArrayOrTraversable( $objects, - sprintf('%s::%s() return value', get_class($repository), $findMethodName), - 'DoctrineModule\Form\Element\Exception\InvalidRepositoryResultException' + sprintf('%s::%s() return value', $repository::class, $findMethodName), + InvalidRepositoryResultException::class ); $this->objects = $objects; diff --git a/src/Options/Authentication.php b/src/Options/Authentication.php index 79431fc3..0e461984 100644 --- a/src/Options/Authentication.php +++ b/src/Options/Authentication.php @@ -52,10 +52,8 @@ final class Authentication extends AbstractOptions { /** * A valid object implementing ObjectManager interface - * - * @var string | ObjectManager */ - protected $objectManager; + protected string|ObjectManager $objectManager; /** * A valid object implementing ObjectRepository interface (or ObjectManager/identityClass) @@ -81,10 +79,8 @@ final class Authentication extends AbstractOptions /** * Callable function to check if a credential is valid - * - * @var mixed */ - protected $credentialCallable; + protected mixed $credentialCallable; /** * If an objectManager is not supplied, this metadata will be used @@ -99,15 +95,10 @@ final class Authentication extends AbstractOptions * When using this options class to create an AuthenticationService with and * the option storeOnlyKeys == false, this is the storage instance that the whole * object will be stored in. - * - * @var StorageInterface|string */ - protected $storage = 'DoctrineModule\Authentication\Storage\Session'; + protected StorageInterface|string $storage = 'DoctrineModule\Authentication\Storage\Session'; - /** - * @param string | ObjectManager $objectManager - */ - public function setObjectManager($objectManager): Authentication + public function setObjectManager(string|ObjectManager $objectManager): Authentication { $this->objectManager = $objectManager; @@ -118,10 +109,8 @@ public function setObjectManager($objectManager): Authentication * Causes issue with unit test StorageFactoryTest::testCanInstantiateStorageFromServiceLocator * when return type is specified * : ObjectManager - * - * @return mixed */ - public function getObjectManager() + public function getObjectManager(): mixed { return $this->objectManager; } @@ -197,11 +186,9 @@ public function getCredentialProperty(): string } /** - * @param mixed $credentialCallable - * * @throws Exception\InvalidArgumentException */ - public function setCredentialCallable($credentialCallable): Authentication + public function setCredentialCallable(mixed $credentialCallable): Authentication { if (! is_callable($credentialCallable)) { throw new Exception\InvalidArgumentException( @@ -217,10 +204,7 @@ public function setCredentialCallable($credentialCallable): Authentication return $this; } - /** - * @return mixed - */ - public function getCredentialCallable() + public function getCredentialCallable(): mixed { return $this->credentialCallable; } @@ -239,18 +223,12 @@ public function setClassMetadata(ClassMetadata $classMetadata): void $this->classMetadata = $classMetadata; } - /** - * @return StorageInterface|string - */ - public function getStorage() + public function getStorage(): StorageInterface|string { return $this->storage; } - /** - * @param StorageInterface|string $storage - */ - public function setStorage($storage): void + public function setStorage(StorageInterface|string $storage): void { $this->storage = $storage; } diff --git a/src/Paginator/Adapter/Collection.php b/src/Paginator/Adapter/Collection.php index a05664c7..09186070 100644 --- a/src/Paginator/Adapter/Collection.php +++ b/src/Paginator/Adapter/Collection.php @@ -17,15 +17,11 @@ */ class Collection implements AdapterInterface { - /** @var DoctrineCollection */ - protected DoctrineCollection $collection; - /** * @param DoctrineCollection $collection */ - public function __construct(DoctrineCollection $collection) + public function __construct(protected DoctrineCollection $collection) { - $this->collection = $collection; } /** diff --git a/src/Paginator/Adapter/Selectable.php b/src/Paginator/Adapter/Selectable.php index 649b9ad4..d7afde06 100644 --- a/src/Paginator/Adapter/Selectable.php +++ b/src/Paginator/Adapter/Selectable.php @@ -19,9 +19,6 @@ */ class Selectable implements AdapterInterface { - /** @var DoctrineSelectable $selectable */ - protected DoctrineSelectable $selectable; - protected Criteria $criteria; /** @@ -30,10 +27,9 @@ class Selectable implements AdapterInterface * * @param DoctrineSelectable $selectable */ - public function __construct(DoctrineSelectable $selectable, ?Criteria $criteria = null) + public function __construct(protected DoctrineSelectable $selectable, ?Criteria $criteria = null) { - $this->selectable = $selectable; - $this->criteria = $criteria ? clone $criteria : new Criteria(); + $this->criteria = $criteria ? clone $criteria : new Criteria(); } /** diff --git a/src/Service/AbstractFactory.php b/src/Service/AbstractFactory.php index b0169254..24010f53 100644 --- a/src/Service/AbstractFactory.php +++ b/src/Service/AbstractFactory.php @@ -23,13 +23,10 @@ abstract class AbstractFactory implements FactoryInterface */ protected ?string $mappingType = null; - protected string $name; - protected ?AbstractOptions $options = null; - public function __construct(string $name) + public function __construct(protected string $name) { - $this->name = $name; } public function getName(): string diff --git a/src/Service/Authentication/AdapterFactory.php b/src/Service/Authentication/AdapterFactory.php index a10111cd..582ef0f0 100644 --- a/src/Service/Authentication/AdapterFactory.php +++ b/src/Service/Authentication/AdapterFactory.php @@ -10,7 +10,6 @@ use Psr\Container\ContainerInterface; use RuntimeException; -use function get_class; use function is_string; use function sprintf; @@ -30,7 +29,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ throw new RuntimeException(sprintf( 'Invalid options received, expected %s, got %s.', Authentication::class, - get_class($options) + $options::class )); } diff --git a/src/Service/Authentication/StorageFactory.php b/src/Service/Authentication/StorageFactory.php index e2262e48..8baa5a46 100644 --- a/src/Service/Authentication/StorageFactory.php +++ b/src/Service/Authentication/StorageFactory.php @@ -10,7 +10,6 @@ use Psr\Container\ContainerInterface; use RuntimeException; -use function get_class; use function is_string; use function sprintf; @@ -30,7 +29,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ throw new RuntimeException(sprintf( 'Invalid options received, expected %s, got %s.', Authentication::class, - get_class($options) + $options::class )); } diff --git a/src/Service/CacheFactory.php b/src/Service/CacheFactory.php index f80067c8..6d6259bd 100644 --- a/src/Service/CacheFactory.php +++ b/src/Service/CacheFactory.php @@ -11,7 +11,6 @@ use Psr\Container\ContainerInterface; use RuntimeException; -use function get_class; use function is_string; use function sprintf; @@ -35,7 +34,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ throw new RuntimeException(sprintf( 'Invalid options received, expected %s, got %s.', CacheOptions::class, - get_class($options) + $options::class )); } diff --git a/src/Service/DriverFactory.php b/src/Service/DriverFactory.php index e6f6bc4d..4256a6d5 100644 --- a/src/Service/DriverFactory.php +++ b/src/Service/DriverFactory.php @@ -20,7 +20,6 @@ use RuntimeException; use function class_exists; -use function get_class; use function is_a; use function sprintf; @@ -42,7 +41,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ throw new RuntimeException(sprintf( 'Invalid options received, expected %s, got %s.', Driver::class, - get_class($options) + $options::class )); } @@ -108,13 +107,13 @@ protected function createDriver(ContainerInterface $container, Driver $options): if ($options->getExtension() && $driver instanceof FileDriver) { $locator = $driver->getLocator(); - if (get_class($locator) !== DefaultFileLocator::class) { + if ($locator::class !== DefaultFileLocator::class) { throw new InvalidArgumentException( sprintf( 'Discovered file locator for driver of type "%s" is an instance of "%s". This factory ' . 'supports only the DefaultFileLocator when an extension is set for the file locator', - get_class($driver), - get_class($locator) + $driver::class, + $locator::class ) ); } @@ -137,7 +136,7 @@ protected function createDriver(ContainerInterface $container, Driver $options): throw new RuntimeException(sprintf( 'Invalid options received, expected %s, got %s.', Driver::class, - get_class($options) + $options::class )); } diff --git a/src/Service/EventManagerFactory.php b/src/Service/EventManagerFactory.php index d7961b91..73f79bd8 100644 --- a/src/Service/EventManagerFactory.php +++ b/src/Service/EventManagerFactory.php @@ -12,7 +12,6 @@ use RuntimeException; use function class_exists; -use function get_class; use function gettype; use function is_object; use function is_string; @@ -34,7 +33,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ throw new RuntimeException(sprintf( 'Invalid options received, expected %s, got %s.', EventManagerOptions::class, - get_class($options) + $options::class )); } @@ -56,7 +55,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ continue; } - $subscriberType = is_object($subscriberName) ? get_class($subscriberName) : $subscriberName; + $subscriberType = is_object($subscriberName) ? $subscriberName::class : $subscriberName; throw new InvalidArgumentException( sprintf( diff --git a/src/ServiceFactory/AbstractDoctrineServiceFactory.php b/src/ServiceFactory/AbstractDoctrineServiceFactory.php index 421767f4..a2e7086d 100644 --- a/src/ServiceFactory/AbstractDoctrineServiceFactory.php +++ b/src/ServiceFactory/AbstractDoctrineServiceFactory.php @@ -44,7 +44,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ /** * @return mixed[]|bool */ - private function getFactoryMapping(ContainerInterface $serviceLocator, string $name) + private function getFactoryMapping(ContainerInterface $serviceLocator, string $name): array|bool { $matches = []; diff --git a/src/Validator/ObjectExists.php b/src/Validator/ObjectExists.php index f8357df8..4bbd2928 100644 --- a/src/Validator/ObjectExists.php +++ b/src/Validator/ObjectExists.php @@ -14,8 +14,8 @@ use function array_key_exists; use function array_values; use function count; -use function get_class; use function gettype; +use function is_countable; use function is_object; use function is_string; use function sprintf; @@ -40,10 +40,8 @@ class ObjectExists extends AbstractValidator /** * Fields to be checked - * - * @var mixed[]|string */ - protected $fields = null; + protected mixed $fields = null; /** * Constructor @@ -61,7 +59,7 @@ public function __construct(array $options) $provided = 'nothing'; } else { if (is_object($options['object_repository'])) { - $provided = get_class($options['object_repository']); + $provided = $options['object_repository']::class; } else { $provided = gettype($options['object_repository']); } @@ -127,7 +125,7 @@ private function validateFields(): array * * @throws Exception\RuntimeException */ - protected function cleanSearchValue($value): array + protected function cleanSearchValue(string|object|array $value): array { $value = is_object($value) ? [$value] : (array) $value; @@ -150,7 +148,7 @@ protected function cleanSearchValue($value): array } else { try { $matchedFieldsValues = @array_combine($this->fields, $value); - } catch (ValueError $valueError) { + } catch (ValueError) { $matchedFieldsValues = false; } @@ -159,7 +157,7 @@ protected function cleanSearchValue($value): array sprintf( 'Provided values count is %s, while expected number of fields to be matched is %s', count($value), - count($this->fields) + is_countable($this->fields) ? count($this->fields) : 0 ) ); } diff --git a/src/Validator/UniqueObject.php b/src/Validator/UniqueObject.php index 9ada4c87..195ce937 100644 --- a/src/Validator/UniqueObject.php +++ b/src/Validator/UniqueObject.php @@ -10,7 +10,6 @@ use function array_diff_assoc; use function array_key_exists; use function count; -use function get_class; use function gettype; use function is_object; use function sprintf; @@ -52,7 +51,7 @@ public function __construct(array $options) $provided = 'nothing'; } else { if (is_object($options['object_manager'])) { - $provided = get_class($options['object_manager']); + $provided = $options['object_manager']::class; } else { $provided = gettype($options['object_manager']); } @@ -73,11 +72,8 @@ public function __construct(array $options) /** * Returns false if there is another object with the same field values but other identifiers. - * - * @param mixed $value - * @param mixed $context */ - public function isValid($value, $context = null): bool + public function isValid(mixed $value, mixed $context = null): bool { if (! $this->useContext) { $context = (array) $value; @@ -125,7 +121,7 @@ protected function getFoundIdentifiers(object $match): array * * @throws Exception\RuntimeException */ - protected function getExpectedIdentifiers($context = null): array + protected function getExpectedIdentifiers(array|object|null $context = null): array { if ($context === null) { throw new Exception\RuntimeException( diff --git a/tests/Authentication/Adapter/ObjectRepositoryTest.php b/tests/Authentication/Adapter/ObjectRepositoryTest.php index 0bd53c35..16067386 100644 --- a/tests/Authentication/Adapter/ObjectRepositoryTest.php +++ b/tests/Authentication/Adapter/ObjectRepositoryTest.php @@ -4,9 +4,14 @@ namespace DoctrineModuleTest\Authentication\Adapter; +use Doctrine\Persistence\ObjectManager; +use Doctrine\Persistence\ObjectRepository; use DoctrineModule\Authentication\Adapter\ObjectRepository as ObjectRepositoryAdapter; use DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject; use DoctrineModuleTest\Authentication\Adapter\TestAsset\PublicPropertiesIdentityObject; +use Laminas\Authentication\Adapter\Exception\InvalidArgumentException; +use Laminas\Authentication\Adapter\Exception\RuntimeException; +use Laminas\Authentication\Adapter\Exception\UnexpectedValueException; use PHPUnit\Framework\TestCase as BaseTestCase; use stdClass; @@ -20,7 +25,7 @@ class ObjectRepositoryTest extends BaseTestCase public function testWillRejectInvalidIdentityProperty(): void { $this->expectException( - 'Laminas\Authentication\Adapter\Exception\InvalidArgumentException' + InvalidArgumentException::class ); $this->expectExceptionMessage( 'Provided $identityProperty is invalid, string given' @@ -32,7 +37,7 @@ public function testWillRejectInvalidIdentityProperty(): void public function testWillRejectInvalidCredentialProperty(): void { $this->expectException( - 'Laminas\Authentication\Adapter\Exception\InvalidArgumentException' + InvalidArgumentException::class ); $this->expectExceptionMessage( 'Provided $credentialProperty is invalid, string given' @@ -43,7 +48,7 @@ public function testWillRejectInvalidCredentialProperty(): void public function testWillRequireIdentityValue(): void { $this->expectException( - 'Laminas\Authentication\Adapter\Exception\RuntimeException' + RuntimeException::class ); $this->expectExceptionMessage( 'A value for the identity was not provided prior to authentication with ObjectRepository authentication ' @@ -51,8 +56,8 @@ public function testWillRequireIdentityValue(): void ); $adapter = new ObjectRepositoryAdapter(); $adapter->setOptions([ - 'object_manager' => $this->createMock('Doctrine\Persistence\ObjectManager'), - 'identity_class' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + 'object_manager' => $this->createMock(ObjectManager::class), + 'identity_class' => IdentityObject::class, ]); $adapter->setCredential('a credential'); $adapter->authenticate(); @@ -61,15 +66,15 @@ public function testWillRequireIdentityValue(): void public function testWillRequireCredentialValue(): void { $this->expectException( - 'Laminas\Authentication\Adapter\Exception\RuntimeException' + RuntimeException::class ); $this->expectExceptionMessage( 'A credential value was not provided prior to authentication with ObjectRepository authentication adapter' ); $adapter = new ObjectRepositoryAdapter(); $adapter->setOptions([ - 'object_manager' => $this->createMock('Doctrine\Persistence\ObjectManager'), - 'identity_class' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + 'object_manager' => $this->createMock(ObjectManager::class), + 'identity_class' => IdentityObject::class, ]); $adapter->setIdentity('an identity'); @@ -79,15 +84,15 @@ public function testWillRequireCredentialValue(): void public function testWillRejectInvalidCredentialCallable(): void { $this->expectException( - 'Laminas\Authentication\Adapter\Exception\InvalidArgumentException' + InvalidArgumentException::class ); $this->expectExceptionMessage( '"array" is not a callable' ); $adapter = new ObjectRepositoryAdapter(); $adapter->setOptions([ - 'object_manager' => $this->createMock('Doctrine\Persistence\ObjectManager'), - 'identity_class' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + 'object_manager' => $this->createMock(ObjectManager::class), + 'identity_class' => IdentityObject::class, 'credential_callable' => [], ]); @@ -100,25 +105,26 @@ public function testAuthentication(): void $entity->setUsername('a username'); $entity->setPassword('a password'); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $method = $objectRepository ->expects($this->exactly(2)) ->method('findOneBy') ->with($this->equalTo(['username' => 'a username'])) ->will($this->returnValue($entity)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->exactly(2)) ->method('getRepository') - ->with($this->equalTo('DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject')) + ->with($this->equalTo(IdentityObject::class)) ->will($this->returnValue($objectRepository)); $adapter = new ObjectRepositoryAdapter(); $adapter->setOptions([ 'object_manager' => $objectManager, - 'identity_class' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + 'identity_class' => IdentityObject::class, 'credential_property' => 'password', 'identity_property' => 'username', + 'credential_callable' => static fn () => true, ]); $adapter->setIdentity('a username'); @@ -128,7 +134,7 @@ public function testAuthentication(): void $this->assertTrue($result->isValid()); $this->assertInstanceOf( - 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + IdentityObject::class, $result->getIdentity() ); @@ -145,7 +151,7 @@ public function testAuthenticationWithPublicProperties(): void $entity->username = 'a username'; $entity->password = 'a password'; - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $method = $objectRepository ->expects($this->exactly(2)) ->method('findOneBy') @@ -157,6 +163,7 @@ public function testAuthenticationWithPublicProperties(): void 'object_repository' => $objectRepository, 'credential_property' => 'password', 'identity_property' => 'username', + 'credential_callable' => static fn () => true, ]); $adapter->setIdentity('a username'); @@ -175,9 +182,9 @@ public function testAuthenticationWithPublicProperties(): void public function testWillRefuseToAuthenticateWithoutGettersOrPublicMethods(): void { - $this->expectException('Laminas\Authentication\Adapter\Exception\UnexpectedValueException'); + $this->expectException(UnexpectedValueException::class); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository ->expects($this->once()) ->method('findOneBy') @@ -204,7 +211,7 @@ public function testCanValidateWithSpecialCrypt(): void // Crypt password using Blowfish $entity->setPassword(crypt('password', $hash)); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository ->expects($this->exactly(2)) ->method('findOneBy') @@ -217,9 +224,7 @@ public function testCanValidateWithSpecialCrypt(): void 'credential_property' => 'password', 'identity_property' => 'username', // enforced type hinting to verify that closure is invoked correctly - 'credential_callable' => static function (IdentityObject $identity, $credentialValue) use ($hash) { - return $identity->getPassword() === crypt($credentialValue, $hash); - }, + 'credential_callable' => static fn (IdentityObject $identity, $credentialValue) => $identity->getPassword() === crypt($credentialValue, $hash), ]); $adapter->setIdentity('username'); @@ -237,9 +242,9 @@ public function testCanValidateWithSpecialCrypt(): void public function testWillRefuseToAuthenticateWhenInvalidInstanceIsFound(): void { - $this->expectException('Laminas\Authentication\Adapter\Exception\UnexpectedValueException'); + $this->expectException(UnexpectedValueException::class); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository ->expects($this->once()) ->method('findOneBy') @@ -261,7 +266,7 @@ public function testWillRefuseToAuthenticateWhenInvalidInstanceIsFound(): void public function testWillNotCastAuthCredentialValue(): void { - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $adapter = new ObjectRepositoryAdapter(); $entity = new IdentityObject(); @@ -270,6 +275,7 @@ public function testWillNotCastAuthCredentialValue(): void 'object_repository' => $objectRepository, 'credential_property' => 'password', 'identity_property' => 'username', + 'credential_callable' => static fn () => null, ]); $adapter->setIdentity('a username'); $adapter->setCredential('00000'); diff --git a/tests/Authentication/Adapter/TestAsset/IdentityObject.php b/tests/Authentication/Adapter/TestAsset/IdentityObject.php index cf7f62dc..1d10a5c7 100644 --- a/tests/Authentication/Adapter/TestAsset/IdentityObject.php +++ b/tests/Authentication/Adapter/TestAsset/IdentityObject.php @@ -13,10 +13,7 @@ class IdentityObject protected ?string $password = null; - /** - * @param mixed $password - */ - public function setPassword($password): void + public function setPassword(mixed $password): void { $this->password = (string) $password; } diff --git a/tests/Authentication/Storage/ObjectRepositoryTest.php b/tests/Authentication/Storage/ObjectRepositoryTest.php index 80d16243..6047e47d 100644 --- a/tests/Authentication/Storage/ObjectRepositoryTest.php +++ b/tests/Authentication/Storage/ObjectRepositoryTest.php @@ -4,6 +4,8 @@ namespace DoctrineModuleTest\Authentication\Storage; +use Doctrine\Persistence\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectRepository; use DoctrineModule\Authentication\Storage\ObjectRepository as ObjectRepositoryStorage; use DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject; use Laminas\Authentication\Storage\NonPersistent as NonPersistentStorage; @@ -21,13 +23,13 @@ public function testCanRetrieveEntityFromObjectRepositoryStorage(): void $entity->setUsername('a username'); $entity->setPassword('a password'); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository->expects($this->exactly(1)) ->method('find') ->with($this->equalTo('a username')) ->will($this->returnValue($entity)); - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $metadata = $this->createMock(ClassMetadata::class); $metadata->expects($this->exactly(1)) ->method('getIdentifierValues') ->with($this->equalTo($entity)) diff --git a/tests/Cache/DoctrineCacheStorageTest.php b/tests/Cache/DoctrineCacheStorageTest.php index 79f85582..8d0e4536 100644 --- a/tests/Cache/DoctrineCacheStorageTest.php +++ b/tests/Cache/DoctrineCacheStorageTest.php @@ -9,6 +9,7 @@ use Doctrine\Common\Cache\ArrayCache as DoctrineArrayCache; use DoctrineModule\Cache\DoctrineCacheStorage; use Laminas\Cache\Storage\Adapter\AdapterOptions; +use Laminas\Cache\Storage\Capabilities; use Laminas\Cache\Storage\StorageInterface; use Laminas\Stdlib\ErrorHandler; use PHPUnit\Framework\TestCase; @@ -21,7 +22,8 @@ use function is_string; use function ksort; use function method_exists; -use function rand; +use function mt_getrandmax; +use function random_int; use function sort; use function sprintf; use function str_replace; @@ -58,12 +60,12 @@ protected function setUp(): void $this->storage = new DoctrineCacheStorage($this->options, new DoctrineArrayCache()); $this->assertInstanceOf( - 'Laminas\Cache\Storage\StorageInterface', + StorageInterface::class, $this->storage, 'Storage adapter instance is needed for test' ); $this->assertInstanceOf( - 'Laminas\Cache\Storage\Adapter\AdapterOptions', + AdapterOptions::class, $this->options, 'Options instance is needed for test' ); @@ -158,7 +160,7 @@ public function testOptionsFluentInterface(): void public function testGetCapabilities(): void { $capabilities = $this->storage->getCapabilities(); - $this->assertInstanceOf('Laminas\Cache\Storage\Capabilities', $capabilities); + $this->assertInstanceOf(Capabilities::class, $capabilities); } public function testDatatypesCapability(): void @@ -731,7 +733,7 @@ public function testTouchItemsReturnsGivenKeysIfNonWritable(): void public function testSetItemAndSetItemsCallSaveWithTtl(): void { - $ttl = rand(); + $ttl = random_int(0, mt_getrandmax()); $provider = $this->createMock(DoctrineArrayCache::class); $provider->expects($this->exactly(4)) diff --git a/tests/Form/Element/ObjectMultiCheckboxTest.php b/tests/Form/Element/ObjectMultiCheckboxTest.php index 5ade6981..9831e32a 100644 --- a/tests/Form/Element/ObjectMultiCheckboxTest.php +++ b/tests/Form/Element/ObjectMultiCheckboxTest.php @@ -5,10 +5,9 @@ namespace DoctrineModuleTest\Form\Element; use DoctrineModule\Form\Element\ObjectMultiCheckbox; +use DoctrineModule\Form\Element\Proxy; use Laminas\Form\Element; -use function get_class; - /** * Tests for the ObjectMultiCheckbox element * @@ -56,11 +55,11 @@ public function testSetValueWithArray(): void public function testGetValueOptionsDoesntCauseInfiniteLoopIfProxyReturnsEmptyArrayAndValidatorIsInitialized(): void { - $element = $this->createPartialMock(get_class($this->element), ['setValueOptions']); + $element = $this->createPartialMock($this->element::class, ['setValueOptions']); $options = []; - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->exactly(2)) ->method('getValueOptions') ->will($this->returnValue($options)); @@ -77,7 +76,7 @@ public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty(): void { $options = ['foo' => 'bar']; - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->once()) ->method('getValueOptions') ->will($this->returnValue($options)); @@ -90,7 +89,7 @@ public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty(): void public function testOptionsCanBeSetSingle(): void { - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]); $this->setProxyViaReflection($proxy); diff --git a/tests/Form/Element/ObjectRadioTest.php b/tests/Form/Element/ObjectRadioTest.php index ac6b0d28..3f3f2ed8 100644 --- a/tests/Form/Element/ObjectRadioTest.php +++ b/tests/Form/Element/ObjectRadioTest.php @@ -5,10 +5,9 @@ namespace DoctrineModuleTest\Form\Element; use DoctrineModule\Form\Element\ObjectRadio; +use DoctrineModule\Form\Element\Proxy; use Laminas\Form\Element; -use function get_class; - /** * Tests for the ObjectRadio element * @@ -30,11 +29,11 @@ protected function setUp(): void public function testGetValueOptionsDoesntCauseInfiniteLoopIfProxyReturnsEmptyArrayAndValidatorIsInitialized(): void { - $element = $this->createPartialMock(get_class($this->element), ['setValueOptions']); + $element = $this->createPartialMock($this->element::class, ['setValueOptions']); $options = []; - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->exactly(2)) ->method('getValueOptions') ->will($this->returnValue($options)); @@ -51,7 +50,7 @@ public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty(): void { $options = ['foo' => 'bar']; - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->once()) ->method('getValueOptions') ->will($this->returnValue($options)); @@ -64,7 +63,7 @@ public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty(): void public function testOptionsCanBeSetSingle(): void { - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]); $this->setProxyViaReflection($proxy); diff --git a/tests/Form/Element/ObjectSelectTest.php b/tests/Form/Element/ObjectSelectTest.php index 2899ae46..ac9ae603 100644 --- a/tests/Form/Element/ObjectSelectTest.php +++ b/tests/Form/Element/ObjectSelectTest.php @@ -5,10 +5,9 @@ namespace DoctrineModuleTest\Form\Element; use DoctrineModule\Form\Element\ObjectSelect; +use DoctrineModule\Form\Element\Proxy; use Laminas\Form\Element; -use function get_class; - /** * Tests for the ObjectSelect element * @@ -74,11 +73,11 @@ public function testSetValueSingleValue(): void public function testGetValueOptionsDoesntCauseInfiniteLoopIfProxyReturnsEmptyArrayAndValidatorIsInitialized(): void { - $element = $this->createPartialMock(get_class($this->element), ['setValueOptions']); + $element = $this->createPartialMock($this->element::class, ['setValueOptions']); $options = []; - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->exactly(2)) ->method('getValueOptions') ->will($this->returnValue($options)); @@ -95,7 +94,7 @@ public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty(): void { $options = ['foo' => 'bar']; - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->once()) ->method('getValueOptions') ->will($this->returnValue($options)); @@ -108,7 +107,7 @@ public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty(): void public function testOptionsCanBeSetSingle(): void { - $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); + $proxy = $this->createMock(Proxy::class); $proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]); $this->setProxyViaReflection($proxy); diff --git a/tests/Form/Element/ProxyAwareElementTestCase.php b/tests/Form/Element/ProxyAwareElementTestCase.php index 721ced1a..8148fbbf 100644 --- a/tests/Form/Element/ProxyAwareElementTestCase.php +++ b/tests/Form/Element/ProxyAwareElementTestCase.php @@ -6,6 +6,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Persistence\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectManager; +use Doctrine\Persistence\ObjectRepository; use DoctrineModuleTest\Form\Element\TestAsset\FormObject; use Laminas\Form\Element; use PHPUnit\Framework\MockObject\MockObject; @@ -15,7 +17,6 @@ use function array_shift; use function func_get_args; -use function get_class; use function method_exists; class ProxyAwareElementTestCase extends TestCase @@ -29,7 +30,7 @@ class ProxyAwareElementTestCase extends TestCase protected function prepareProxy(): void { - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; + $objectClass = FormObject::class; $objectOne = new FormObject(); $objectTwo = new FormObject(); @@ -50,7 +51,7 @@ protected function prepareProxy(): void $result = new ArrayCollection([$objectOne, $objectTwo]); $this->values = $result; - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $metadata = $this->createMock(ClassMetadata::class); $metadata ->expects($this->any()) ->method('getIdentifierValues') @@ -73,12 +74,12 @@ static function () use ($objectOne, $objectTwo) { ) ); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository->expects($this->any()) ->method('findAll') ->will($this->returnValue($result)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with($this->equalTo($objectClass)) @@ -111,7 +112,7 @@ protected function setProxyViaReflection(MockObject $proxy, ?MockObject $element $element = $this->element; } - $prop = new ReflectionProperty(get_class($this->element), 'proxy'); + $prop = new ReflectionProperty($this->element::class, 'proxy'); $prop->setAccessible(true); $prop->setValue($element, $proxy); } diff --git a/tests/Form/Element/ProxyTest.php b/tests/Form/Element/ProxyTest.php index 4462a172..c220603b 100644 --- a/tests/Form/Element/ProxyTest.php +++ b/tests/Form/Element/ProxyTest.php @@ -6,6 +6,9 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Persistence\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectManager; +use Doctrine\Persistence\ObjectRepository; +use DoctrineModule\Form\Element\Exception\InvalidRepositoryResultException; use DoctrineModule\Form\Element\Proxy; use DoctrineModuleTest\Form\Element\TestAsset\FormObject; use PHPUnit\Framework\MockObject\MockObject; @@ -15,7 +18,6 @@ use function array_shift; use function func_get_args; -use function get_class; use const PHP_VERSION_ID; @@ -45,7 +47,7 @@ public function testExceptionThrownForMissingObjectManager(): void $this->expectException(RuntimeException::class); $this->expectExceptionMessage('No object manager was set'); - $this->proxy->setOptions(['target_class' => 'DoctrineModuleTest\Form\Element\TestAsset\FormObject']); + $this->proxy->setOptions(['target_class' => FormObject::class]); $this->proxy->getValueOptions(); } @@ -55,7 +57,7 @@ public function testExceptionThrownForMissingTargetClass(): void $this->expectExceptionMessage('No target class was set'); $this->proxy->setOptions([ - 'object_manager' => $this->createMock('Doctrine\Persistence\ObjectManager'), + 'object_manager' => $this->createMock(ObjectManager::class), ]); $this->proxy->getValueOptions(); } @@ -65,10 +67,10 @@ public function testExceptionThrownForMissingFindMethodName(): void $this->expectException(RuntimeException::class); $this->expectExceptionMessage('No method name was set'); - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $objectClass = FormObject::class; + $metadata = $this->createMock(ClassMetadata::class); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->once()) ->method('getClassMetadata') ->with($this->equalTo($objectClass)) @@ -85,12 +87,12 @@ public function testExceptionThrownForMissingFindMethodName(): void public function testExceptionFindMethodNameNotExistentInRepository(): void { - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $objectClass = FormObject::class; + $metadata = $this->createMock(ClassMetadata::class); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->once()) ->method('getClassMetadata') ->with($this->equalTo($objectClass)) @@ -111,7 +113,7 @@ public function testExceptionFindMethodNameNotExistentInRepository(): void 'RuntimeException' ); $this->expectExceptionMessage( - 'Method "NotExistent" could not be found in repository "' . get_class($objectRepository) . '"' + 'Method "NotExistent" could not be found in repository "' . $objectRepository::class . '"' ); $this->proxy->getValueOptions(); @@ -119,12 +121,12 @@ public function testExceptionFindMethodNameNotExistentInRepository(): void public function testExceptionThrownForMissingRequiredParameter(): void { - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $objectClass = FormObject::class; + $metadata = $this->createMock(ClassMetadata::class); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->once()) ->method('getClassMetadata') ->with($this->equalTo($objectClass)) @@ -142,6 +144,7 @@ public function testExceptionThrownForMissingRequiredParameter(): void 'name' => 'findBy', 'params' => [], ], + 'property' => 'password', ]); $this->expectException( @@ -149,7 +152,7 @@ public function testExceptionThrownForMissingRequiredParameter(): void ); $this->expectExceptionMessage( 'Required parameter "criteria" with no default value for method "findBy" in repository "' - . get_class($objectRepository) . '" was not provided' + . $objectRepository::class . '" was not provided' ); $this->proxy->getValueOptions(); @@ -159,6 +162,13 @@ public function testToStringIsUsedForGetValueOptions(): void { $this->prepareProxy(); + $this->proxy->setOptions(['property' => 'username']); + + $this->metadata->expects($this->exactly(2)) + ->method('hasField') + ->with($this->equalTo('username')) + ->will($this->returnValue(true)); + $result = $this->proxy->getValueOptions(); $this->assertEquals($result[0]['label'], 'object one username'); $this->assertEquals($result[1]['label'], 'object two username'); @@ -230,8 +240,14 @@ public function testDisplayEmptyItemAndEmptyItemLabelOptionsUsedForGetValueOptio $this->proxy->setOptions([ 'display_empty_item' => true, 'empty_item_label' => '---', + 'property' => 'password', ]); + $this->metadata->expects($this->exactly(2)) + ->method('hasField') + ->with($this->equalTo('password')) + ->will($this->returnValue(true)); + $result = $this->proxy->getValueOptions(); $this->assertArrayHasKey('', $result); $this->assertEquals($result[''], '---'); @@ -242,9 +258,7 @@ public function testLabelGeneratorUsedForGetValueOptions(): void $this->prepareProxy(); $this->proxy->setOptions([ - 'label_generator' => static function ($targetEntity) { - return $targetEntity->getEmail(); - }, + 'label_generator' => static fn ($targetEntity) => $targetEntity->getEmail(), ]); $this->metadata->expects($this->never()) @@ -282,8 +296,14 @@ public function testUsingOptionAttributesOfTypeString(): void 'class' => 'foo', 'lang' => 'en', ], + 'property' => 'password', ]); + $this->metadata->expects($this->exactly(2)) + ->method('hasField') + ->with($this->equalTo('password')) + ->will($this->returnValue(true)); + $options = $this->proxy->getValueOptions(); $expectedAttributes = [ @@ -306,12 +326,16 @@ public function testUsingOptionAttributesOfTypeCallableReturningString(): void $this->proxy->setOptions([ 'option_attributes' => [ - 'data-id' => static function ($object) { - return $object->getId(); - }, + 'data-id' => static fn ($object) => $object->getId(), ], + 'property' => 'password', ]); + $this->metadata->expects($this->exactly(2)) + ->method('hasField') + ->with($this->equalTo('password')) + ->will($this->returnValue(true)); + $options = $this->proxy->getValueOptions(); $this->assertCount(2, $options); @@ -332,6 +356,7 @@ public function testRuntimeExceptionOnWrongOptionAttributesValue(): void $this->proxy->setOptions([ 'option_attributes' => ['data-id' => $stdClass], + 'property' => 'username', ]); $this->expectException('RuntimeException'); @@ -360,7 +385,7 @@ public function testExceptionThrownForNonTraversableResults(): void $this->prepareEmptyProxy(new stdClass()); $this->expectException( - 'DoctrineModule\Form\Element\Exception\InvalidRepositoryResultException' + InvalidRepositoryResultException::class ); $this->expectExceptionMessage( 'return value must be an array or Traversable' @@ -373,6 +398,13 @@ public function testUsingFindMethod(): void { $this->prepareFilteredProxy(); + $this->proxy->setOptions(['property' => 'password']); + + $this->metadata->expects($this->exactly(2)) + ->method('hasField') + ->with($this->equalTo('password')) + ->will($this->returnValue(true)); + $this->proxy->getValueOptions(); } @@ -384,7 +416,10 @@ public function testExceptionThrownWhenOptgroupIdentifiesNotCallable(): void { $this->prepareProxyWithOptgroupPreset(); - $this->proxy->setOptions(['optgroup_identifier' => 'NonExistantFunctionName']); + $this->proxy->setOptions([ + 'optgroup_identifier' => 'NonExistantFunctionName', + 'property' => 'username', + ]); $this->expectException('RuntimeException'); @@ -404,7 +439,15 @@ public function testValueOptionsGeneratedProperlyWithOptgroups(): void { $this->prepareProxyWithOptgroupPreset(); - $this->proxy->setOptions(['optgroup_identifier' => 'optgroup']); + $this->proxy->setOptions([ + 'optgroup_identifier' => 'optgroup', + 'property' => 'username', + ]); + + $this->metadata->expects($this->exactly(3)) + ->method('hasField') + ->with($this->equalTo('username')) + ->will($this->returnValue(true)); $valueOptions = $this->proxy->getValueOptions(); @@ -455,8 +498,14 @@ public function testEmptyOptgroupValueBelongsToOptgroupDefaultIfConfigured(): vo $this->proxy->setOptions([ 'optgroup_identifier' => 'optgroup', 'optgroup_default' => 'Others', + 'property' => 'username', ]); + $this->metadata->expects($this->exactly(2)) + ->method('hasField') + ->with($this->equalTo('username')) + ->will($this->returnValue(true)); + $valueOptions = $this->proxy->getValueOptions(); $expectedOutput = [ @@ -494,7 +543,15 @@ public function testEmptyOptgroupValueBelongsToNoOptgroupIfNotConfigured(): void { $this->prepareProxyWithOptgroupPresetThatHasPartiallyEmptyOptgroupValues(); - $this->proxy->setOptions(['optgroup_identifier' => 'optgroup']); + $this->proxy->setOptions([ + 'optgroup_identifier' => 'optgroup', + 'property' => 'username', + ]); + + $this->metadata->expects($this->exactly(2)) + ->method('hasField') + ->with($this->equalTo('username')) + ->will($this->returnValue(true)); $valueOptions = $this->proxy->getValueOptions(); @@ -521,7 +578,7 @@ public function testEmptyOptgroupValueBelongsToNoOptgroupIfNotConfigured(): void protected function prepareProxy(): void { - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; + $objectClass = FormObject::class; $objectOne = new FormObject(); $objectTwo = new FormObject(); @@ -541,7 +598,7 @@ protected function prepareProxy(): void $result = new ArrayCollection([$objectOne, $objectTwo]); - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $metadata = $this->createMock(ClassMetadata::class); $metadata ->expects($this->any()) ->method('getIdentifierValues') @@ -564,12 +621,12 @@ static function () use ($objectOne, $objectTwo) { ) ); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository->expects($this->any()) ->method('findAll') ->will($this->returnValue($result)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with($this->equalTo($objectClass)) @@ -591,7 +648,7 @@ static function () use ($objectOne, $objectTwo) { protected function prepareProxyWithOptgroupPreset(): void { - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; + $objectClass = FormObject::class; $objectOne = new FormObject(); $objectTwo = new FormObject(); $objectThree = new FormObject(); @@ -622,7 +679,7 @@ protected function prepareProxyWithOptgroupPreset(): void $result = new ArrayCollection([$objectOne, $objectTwo, $objectThree]); - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $metadata = $this->createMock(ClassMetadata::class); $metadata ->expects($this->any()) ->method('getIdentifierValues') @@ -649,12 +706,12 @@ static function () use ($objectOne, $objectTwo, $objectThree) { ) ); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository->expects($this->any()) ->method('findAll') ->will($this->returnValue($result)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with($this->equalTo($objectClass)) @@ -676,7 +733,7 @@ static function () use ($objectOne, $objectTwo, $objectThree) { protected function prepareProxyWithOptgroupPresetThatHasPartiallyEmptyOptgroupValues(): void { - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; + $objectClass = FormObject::class; $objectOne = new FormObject(); $objectTwo = new FormObject(); @@ -697,7 +754,7 @@ protected function prepareProxyWithOptgroupPresetThatHasPartiallyEmptyOptgroupVa $result = new ArrayCollection([$objectOne, $objectTwo]); - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $metadata = $this->createMock(ClassMetadata::class); $metadata ->expects($this->any()) ->method('getIdentifierValues') @@ -720,12 +777,12 @@ static function () use ($objectOne, $objectTwo) { ) ); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository->expects($this->any()) ->method('findAll') ->will($this->returnValue($result)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with($this->equalTo($objectClass)) @@ -747,7 +804,7 @@ static function () use ($objectOne, $objectTwo) { protected function prepareFilteredProxy(): void { - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; + $objectClass = FormObject::class; $objectOne = new FormObject(); $objectTwo = new FormObject(); @@ -767,7 +824,7 @@ protected function prepareFilteredProxy(): void $result = new ArrayCollection([$objectOne, $objectTwo]); - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $metadata = $this->createMock(ClassMetadata::class); $metadata ->expects($this->exactly(2)) ->method('getIdentifierValues') @@ -789,13 +846,13 @@ static function () use ($objectOne, $objectTwo) { ) ); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository ->expects($this->once()) ->method('findBy') ->will($this->returnValue($result)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager ->expects($this->once()) ->method('getClassMetadata') @@ -822,25 +879,22 @@ static function () use ($objectOne, $objectTwo) { $this->metadata = $metadata; } - /** - * @param mixed $result - */ - public function prepareEmptyProxy($result = null): void + public function prepareEmptyProxy(mixed $result = null): void { if ($result === null) { $result = new ArrayCollection(); } - $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; - $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); - $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $objectClass = FormObject::class; + $metadata = $this->createMock(ClassMetadata::class); + $objectRepository = $this->createMock(ObjectRepository::class); $objectRepository ->expects($this->once()) ->method('findAll') ->will($this->returnValue($result)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager ->expects($this->once()) ->method('getClassMetadata') diff --git a/tests/Form/Element/TestAsset/FormObject.php b/tests/Form/Element/TestAsset/FormObject.php index e5385f11..7a45b8fe 100644 --- a/tests/Form/Element/TestAsset/FormObject.php +++ b/tests/Form/Element/TestAsset/FormObject.php @@ -4,12 +4,14 @@ namespace DoctrineModuleTest\Form\Element\TestAsset; +use Stringable; + use function assert; /** * Simple mock object for form element adapter test */ -class FormObject +class FormObject implements Stringable { protected ?int $id = null; diff --git a/tests/ModuleTest.php b/tests/ModuleTest.php index 8dfb78a8..0cd017fd 100644 --- a/tests/ModuleTest.php +++ b/tests/ModuleTest.php @@ -21,25 +21,25 @@ class ModuleTest extends TestCase { /** @var MockObject&Application */ - private $application; + private MockObject $application; /** @var MockObject&MvcEvent */ - private $event; + private MockObject $event; /** @var MockObject&ServiceManager */ - private $serviceManager; + private MockObject $serviceManager; /** @var MockObject&SymfonyApplication */ - private $cli; + private MockObject $cli; protected function setUp(): void { - $this->application = $this->getMockBuilder('Laminas\Mvc\Application') + $this->application = $this->getMockBuilder(Application::class) ->disableOriginalConstructor() ->getMock(); - $this->event = $this->createMock('Laminas\Mvc\MvcEvent'); - $this->serviceManager = $this->createMock('Laminas\ServiceManager\ServiceManager'); - $this->cli = $this->createPartialMock('Symfony\Component\Console\Application', ['run']); + $this->event = $this->createMock(MvcEvent::class); + $this->serviceManager = $this->createMock(ServiceManager::class); + $this->cli = $this->createPartialMock(SymfonyApplication::class, ['run']); $this ->serviceManager diff --git a/tests/Paginator/Adapter/SelectableAdapterTest.php b/tests/Paginator/Adapter/SelectableAdapterTest.php index 60864362..cae2fb5b 100644 --- a/tests/Paginator/Adapter/SelectableAdapterTest.php +++ b/tests/Paginator/Adapter/SelectableAdapterTest.php @@ -6,6 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Selectable; use DoctrineModule\Paginator\Adapter\Selectable as SelectableAdapter; use PHPUnit\Framework\TestCase; @@ -21,7 +22,7 @@ class SelectableAdapterTest extends TestCase */ public function testGetItemsAtOffsetZeroWithEmptyCriteria(): void { - $selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); + $selectable = $this->createMock(Selectable::class); $adapter = new SelectableAdapter($selectable); $me = $this; @@ -52,7 +53,7 @@ static function (Criteria $criteria) use ($me) { */ public function testGetItemsAtOffsetZeroWithNonEmptyCriteria(): void { - $selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); + $selectable = $this->createMock(Selectable::class); $criteria = new Criteria(Criteria::expr()->eq('foo', 'bar')); $adapter = new SelectableAdapter($selectable, $criteria); @@ -86,7 +87,7 @@ static function (Criteria $innerCriteria) use ($criteria, $me) { */ public function testGetItemsAtOffsetTenWithEmptyCriteria(): void { - $selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); + $selectable = $this->createMock(Selectable::class); $adapter = new SelectableAdapter($selectable); $me = $this; @@ -116,7 +117,7 @@ static function (Criteria $criteria) use ($me) { */ public function testGetItemsAtOffsetTenWithNonEmptyCriteria(): void { - $selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); + $selectable = $this->createMock(Selectable::class); $criteria = new Criteria(Criteria::expr()->eq('foo', 'bar')); $adapter = new SelectableAdapter($selectable, $criteria); @@ -150,7 +151,7 @@ static function (Criteria $innerCriteria) use ($criteria, $me) { */ public function testReturnsCorrectCount(): void { - $selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); + $selectable = $this->createMock(Selectable::class); $expression = Criteria::expr()->eq('foo', 'bar'); $criteria = new Criteria($expression, ['baz' => Criteria::DESC], 10, 20); $adapter = new SelectableAdapter($selectable, $criteria); @@ -159,12 +160,10 @@ public function testReturnsCorrectCount(): void ->method('matching') ->with( $this->callback( - static function (Criteria $criteria) use ($expression) { - return $criteria->getWhereExpression() === $expression - && ($criteria->getOrderings() === ['baz' => Criteria::DESC]) - && $criteria->getFirstResult() === null - && $criteria->getMaxResults() === null; - } + static fn (Criteria $criteria) => $criteria->getWhereExpression() === $expression + && ($criteria->getOrderings() === ['baz' => Criteria::DESC]) + && $criteria->getFirstResult() === null + && $criteria->getMaxResults() === null ) ) ->will($this->returnValue(new ArrayCollection(range(1, 101)))); diff --git a/tests/Service/Authentication/AdapterFactoryTest.php b/tests/Service/Authentication/AdapterFactoryTest.php index 0751ed8e..043e02b2 100644 --- a/tests/Service/Authentication/AdapterFactoryTest.php +++ b/tests/Service/Authentication/AdapterFactoryTest.php @@ -4,8 +4,10 @@ namespace DoctrineModuleTest\Service\Authentication; +use Doctrine\Persistence\ObjectManager; use DoctrineModule\Authentication\Adapter\ObjectRepository; use DoctrineModule\Service\Authentication\AdapterFactory; +use DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject; use Laminas\ServiceManager\ServiceManager; use PHPUnit\Framework\TestCase as BaseTestCase; @@ -15,7 +17,7 @@ public function testWillInstantiateFromFQCN(): void { $name = 'testFactory'; $factory = new AdapterFactory($name); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $serviceManager = new ServiceManager(); $serviceManager->setService( 'config', @@ -24,7 +26,7 @@ public function testWillInstantiateFromFQCN(): void 'authentication' => [ $name => [ 'objectManager' => $objectManager, - 'identityClass' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + 'identityClass' => IdentityObject::class, 'identityProperty' => 'username', 'credentialProperty' => 'password', ], diff --git a/tests/Service/Authentication/AuthenticationServiceFactoryTest.php b/tests/Service/Authentication/AuthenticationServiceFactoryTest.php index edd11cb5..c8f94f2a 100644 --- a/tests/Service/Authentication/AuthenticationServiceFactoryTest.php +++ b/tests/Service/Authentication/AuthenticationServiceFactoryTest.php @@ -4,10 +4,13 @@ namespace DoctrineModuleTest\Service\Authentication; +use Doctrine\Persistence\ObjectManager; use DoctrineModule\Service\Authentication\AdapterFactory; use DoctrineModule\Service\Authentication\AuthenticationServiceFactory; use DoctrineModule\Service\Authentication\StorageFactory; +use DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject; use Laminas\Authentication\AuthenticationService; +use Laminas\Authentication\Storage\Session; use Laminas\ServiceManager\ServiceManager; use PHPUnit\Framework\TestCase as BaseTestCase; @@ -18,7 +21,7 @@ public function testWillInstantiateFromFQCN(): void $name = 'testFactory'; $factory = new AuthenticationServiceFactory($name); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $serviceManager = new ServiceManager(); $serviceManager->setService( @@ -28,7 +31,7 @@ public function testWillInstantiateFromFQCN(): void 'authentication' => [ $name => [ 'objectManager' => $objectManager, - 'identityClass' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + 'identityClass' => IdentityObject::class, 'identityProperty' => 'username', 'credentialProperty' => 'password', ], @@ -38,7 +41,7 @@ public function testWillInstantiateFromFQCN(): void ); $serviceManager->setInvokableClass( 'DoctrineModule\Authentication\Storage\Session', - 'Laminas\Authentication\Storage\Session' + Session::class ); $serviceManager->setFactory('doctrine.authenticationadapter.' . $name, new AdapterFactory($name)); $serviceManager->setFactory('doctrine.authenticationstorage.' . $name, new StorageFactory($name)); diff --git a/tests/Service/Authentication/StorageFactoryTest.php b/tests/Service/Authentication/StorageFactoryTest.php index 68b24674..607ce487 100644 --- a/tests/Service/Authentication/StorageFactoryTest.php +++ b/tests/Service/Authentication/StorageFactoryTest.php @@ -4,8 +4,12 @@ namespace DoctrineModuleTest\Service\Authentication; +use Doctrine\Persistence\ObjectManager; use DoctrineModule\Authentication\Storage\ObjectRepository; use DoctrineModule\Service\Authentication\StorageFactory; +use DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject; +use Laminas\Authentication\Storage\Session; +use Laminas\Authentication\Storage\StorageInterface; use Laminas\ServiceManager\ServiceManager; use PHPUnit\Framework\TestCase as BaseTestCase; @@ -16,12 +20,12 @@ public function testWillInstantiateFromFQCN(): void $name = 'testFactory'; $factory = new StorageFactory($name); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $serviceManager = new ServiceManager(); $serviceManager->setInvokableClass( 'DoctrineModule\Authentication\Storage\Session', - 'Laminas\Authentication\Storage\Session' + Session::class ); $serviceManager->setService( 'config', @@ -30,7 +34,7 @@ public function testWillInstantiateFromFQCN(): void 'authentication' => [ $name => [ 'objectManager' => $objectManager, - 'identityClass' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject', + 'identityClass' => IdentityObject::class, 'identityProperty' => 'username', 'credentialProperty' => 'password', ], @@ -46,12 +50,19 @@ public function testWillInstantiateFromFQCN(): void public function testCanInstantiateStorageFromServiceLocator(): void { $factory = new StorageFactory('testFactory'); - $serviceManager = $this->createMock('Laminas\ServiceManager\ServiceManager'); - $storage = $this->createMock('Laminas\Authentication\Storage\StorageInterface'); + $serviceManager = $this->createMock(ServiceManager::class); + $storage = $this->createMock(StorageInterface::class); + $objectManager = $this->createMock(ObjectManager::class); $config = [ 'doctrine' => [ 'authentication' => [ - 'testFactory' => ['storage' => 'some_storage'], + 'testFactory' => [ + 'storage' => 'some_storage', + 'objectManager' => $objectManager, + 'identityClass' => IdentityObject::class, + 'identityProperty' => 'username', + 'credentialProperty' => 'password', + ], ], ], ]; diff --git a/tests/Service/CacheFactoryTest.php b/tests/Service/CacheFactoryTest.php index 3c2e2ecc..18fa8b7b 100644 --- a/tests/Service/CacheFactoryTest.php +++ b/tests/Service/CacheFactoryTest.php @@ -15,6 +15,7 @@ use Laminas\Cache\Storage\Adapter\Memory; use Laminas\ServiceManager\ServiceManager; use PHPUnit\Framework\TestCase as BaseTestCase; +use Predis\ClientInterface; use function assert; @@ -67,7 +68,7 @@ public function testCreateLaminasCache(): void 'doctrine' => [ 'cache' => [ 'phpunit' => [ - 'class' => 'DoctrineModule\Cache\LaminasStorageCache', + 'class' => LaminasStorageCache::class, 'instance' => 'my-laminas-cache', 'namespace' => 'DoctrineModule', ], @@ -111,7 +112,7 @@ public function testCreatePredisCache(): void ); $serviceManager->setService( 'my_predis_alias', - $this->createMock('Predis\ClientInterface') + $this->createMock(ClientInterface::class) ); $cache = $factory->__invoke($serviceManager, PredisCache::class); @@ -141,9 +142,7 @@ public function testUseServiceFactory(): void $mock = $this->createMock(ChainCache::class); - $serviceManager->setFactory(ChainCache::class, static function () use ($mock) { - return $mock; - }); + $serviceManager->setFactory(ChainCache::class, static fn () => $mock); $cache = $factory->__invoke($serviceManager, ChainCache::class); diff --git a/tests/ServiceFactory/ModuleDefinedServicesTest.php b/tests/ServiceFactory/ModuleDefinedServicesTest.php index 6b904027..7c5d701b 100644 --- a/tests/ServiceFactory/ModuleDefinedServicesTest.php +++ b/tests/ServiceFactory/ModuleDefinedServicesTest.php @@ -12,6 +12,7 @@ use DoctrineModuleTest\ServiceManagerFactory; use Laminas\Cache\Storage\Adapter\Filesystem; use Laminas\Cache\Storage\Adapter\Memory; +use Laminas\ServiceManager\Exception\ServiceNotFoundException; use Laminas\ServiceManager\ServiceLocatorInterface; use PHPUnit\Framework\TestCase; @@ -54,7 +55,7 @@ public function testModuleFetchedService(string $serviceName, string $expectedCl */ public function testModuleInvalidService(string $serviceName): void { - $this->expectException('Laminas\ServiceManager\Exception\ServiceNotFoundException'); + $this->expectException(ServiceNotFoundException::class); $this->serviceManager->get($serviceName); } diff --git a/tests/ServiceManagerFactory.php b/tests/ServiceManagerFactory.php index 87289503..210f9582 100644 --- a/tests/ServiceManagerFactory.php +++ b/tests/ServiceManagerFactory.php @@ -5,6 +5,7 @@ namespace DoctrineModuleTest; use Laminas\ModuleManager\ModuleManagerInterface; +use Laminas\Mvc\Service\ServiceListenerFactory; use Laminas\Mvc\Service\ServiceManagerConfig; use Laminas\ServiceManager\ServiceManager; @@ -37,7 +38,7 @@ public static function getServiceManager(?array $configuration = null): ServiceM $serviceManager->setService('ApplicationConfig', $configuration); if (! $serviceManager->has('ServiceListener')) { - $serviceManager->setFactory('ServiceListener', 'Laminas\Mvc\Service\ServiceListenerFactory'); + $serviceManager->setFactory('ServiceListener', ServiceListenerFactory::class); } $moduleManager = $serviceManager->get('ModuleManager'); diff --git a/tests/Validator/NoObjectExistsTest.php b/tests/Validator/NoObjectExistsTest.php index a1654a9d..ca7ea249 100644 --- a/tests/Validator/NoObjectExistsTest.php +++ b/tests/Validator/NoObjectExistsTest.php @@ -4,6 +4,7 @@ namespace DoctrineModuleTest\Validator\Adapter; +use Doctrine\Persistence\ObjectRepository; use DoctrineModule\Validator\NoObjectExists; use PHPUnit\Framework\TestCase as BaseTestCase; use stdClass; @@ -17,7 +18,7 @@ class NoObjectExistsTest extends BaseTestCase { public function testCanValidateWithNoAvailableObjectInRepository(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->once()) @@ -31,7 +32,7 @@ public function testCanValidateWithNoAvailableObjectInRepository(): void public function testCannotValidateWithAvailableObjectInRepository(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->once()) @@ -45,7 +46,7 @@ public function testCannotValidateWithAvailableObjectInRepository(): void public function testErrorMessageIsStringInsteadArray(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->once()) ->method('findOneBy') diff --git a/tests/Validator/ObjectExistsTest.php b/tests/Validator/ObjectExistsTest.php index f49d56fe..505394e3 100644 --- a/tests/Validator/ObjectExistsTest.php +++ b/tests/Validator/ObjectExistsTest.php @@ -4,7 +4,10 @@ namespace DoctrineModuleTest\Validator\Adapter; +use Doctrine\Persistence\ObjectRepository; use DoctrineModule\Validator\ObjectExists; +use Laminas\Validator\Exception\InvalidArgumentException; +use Laminas\Validator\Exception\RuntimeException; use PHPUnit\Framework\TestCase as BaseTestCase; use stdClass; @@ -19,7 +22,7 @@ class ObjectExistsTest extends BaseTestCase { public function testCanValidateWithSingleField(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->exactly(2)) @@ -35,7 +38,7 @@ public function testCanValidateWithSingleField(): void public function testCanValidateWithMultipleFields(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->exactly(2)) ->method('findOneBy') @@ -60,7 +63,7 @@ public function testCanValidateWithMultipleFields(): void public function testCanValidateFalseOnNoResult(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->once()) ->method('findOneBy') @@ -75,49 +78,49 @@ public function testCanValidateFalseOnNoResult(): void public function testWillRefuseMissingRepository(): void { - $this->expectException('Laminas\Validator\Exception\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); new ObjectExists(['fields' => 'field']); } public function testWillRefuseNonObjectRepository(): void { - $this->expectException('Laminas\Validator\Exception\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); new ObjectExists(['object_repository' => 'invalid', 'fields' => 'field']); } public function testWillRefuseInvalidRepository(): void { - $this->expectException('Laminas\Validator\Exception\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); new ObjectExists(['object_repository' => new stdClass(), 'fields' => 'field']); } public function testWillRefuseMissingFields(): void { - $this->expectException('Laminas\Validator\Exception\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); new ObjectExists([ - 'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'), + 'object_repository' => $this->createMock(ObjectRepository::class), ]); } public function testWillRefuseEmptyFields(): void { - $this->expectException('Laminas\Validator\Exception\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); new ObjectExists([ - 'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'), + 'object_repository' => $this->createMock(ObjectRepository::class), 'fields' => [], ]); } public function testWillRefuseNonStringFields(): void { - $this->expectException('Laminas\Validator\Exception\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); new ObjectExists([ - 'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'), + 'object_repository' => $this->createMock(ObjectRepository::class), 'fields' => [123], ]); } @@ -125,13 +128,13 @@ public function testWillRefuseNonStringFields(): void public function testWillNotValidateOnFieldsCountMismatch(): void { $this->expectException( - 'Laminas\Validator\Exception\RuntimeException' + RuntimeException::class ); $this->expectExceptionMessage( 'Provided values count is 1, while expected number of fields to be matched is 2' ); $validator = new ObjectExists([ - 'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'), + 'object_repository' => $this->createMock(ObjectRepository::class), 'fields' => ['field1', 'field2'], ]); $validator->isValid(['field1Value']); @@ -140,14 +143,14 @@ public function testWillNotValidateOnFieldsCountMismatch(): void public function testWillNotValidateOnFieldKeysMismatch(): void { $this->expectException( - 'Laminas\Validator\Exception\RuntimeException' + RuntimeException::class ); $this->expectExceptionMessage( 'Field "field2" was not provided, but was expected since the configured field lists needs it for validation' ); $validator = new ObjectExists([ - 'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'), + 'object_repository' => $this->createMock(ObjectRepository::class), 'fields' => ['field1', 'field2'], ]); @@ -156,9 +159,9 @@ public function testWillNotValidateOnFieldKeysMismatch(): void public function testErrorMessageIsStringInsteadArray(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $validator = new ObjectExists([ - 'object_repository' => $this->createMock('Doctrine\Persistence\ObjectRepository'), + 'object_repository' => $this->createMock(ObjectRepository::class), 'fields' => 'field', ]); diff --git a/tests/Validator/UniqueObjectTest.php b/tests/Validator/UniqueObjectTest.php index 09bd702e..da904532 100644 --- a/tests/Validator/UniqueObjectTest.php +++ b/tests/Validator/UniqueObjectTest.php @@ -5,7 +5,9 @@ namespace DoctrineModuleTest\Validator\Adapter; use DateTime; +use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\ObjectManager; +use Doctrine\Persistence\ObjectRepository; use DoctrineModule\Validator\UniqueObject; use InvalidArgumentException; use Laminas\Validator\Exception\RuntimeException; @@ -22,14 +24,14 @@ class UniqueObjectTest extends BaseTestCase { public function testCanValidateWithNotAvailableObjectInRepository(): void { - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->once()) ->method('findOneBy') ->with(['matchKey' => 'matchValue']) ->will($this->returnValue(null)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $validator = new UniqueObject([ 'object_repository' => $repository, @@ -43,7 +45,7 @@ public function testCanValidateIfThereIsTheSameObjectInTheRepository(): void { $match = new stdClass(); - $classMetadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata ->expects($this->once()) ->method('getIdentifierFieldNames') @@ -54,13 +56,13 @@ public function testCanValidateIfThereIsTheSameObjectInTheRepository(): void ->with($match) ->will($this->returnValue(['id' => 'identifier'])); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with('stdClass') ->will($this->returnValue($classMetadata)); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->any()) ->method('getClassName') @@ -83,7 +85,7 @@ public function testCannotValidateIfThereIsAnotherObjectWithTheSameValueInTheRep { $match = new stdClass(); - $classMetadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata ->expects($this->once()) ->method('getIdentifierFieldNames') @@ -94,13 +96,13 @@ public function testCannotValidateIfThereIsAnotherObjectWithTheSameValueInTheRep ->with($match) ->will($this->returnValue(['id' => 'identifier'])); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with('stdClass') ->will($this->returnValue($classMetadata)); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->any()) ->method('getClassName') @@ -123,7 +125,7 @@ public function testCanFetchIdentifierFromContext(): void { $match = new stdClass(); - $classMetadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata ->expects($this->once()) ->method('getIdentifierFieldNames') @@ -134,13 +136,13 @@ public function testCanFetchIdentifierFromContext(): void ->with($match) ->will($this->returnValue(['id' => 'identifier'])); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with('stdClass') ->will($this->returnValue($classMetadata)); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->any()) ->method('getClassName') @@ -167,14 +169,14 @@ public function testThrowsAnExceptionOnUsedButMissingContext(): void $match = new stdClass(); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->once()) ->method('findOneBy') ->with(['matchKey' => 'matchValue']) ->will($this->returnValue($match)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $validator = new UniqueObject([ 'object_repository' => $repository, @@ -192,19 +194,19 @@ public function testThrowsAnExceptionOnMissingIdentifier(): void $match = new stdClass(); - $classMetadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata ->expects($this->once()) ->method('getIdentifierFieldNames') ->will($this->returnValue(['id'])); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with('stdClass') ->will($this->returnValue($classMetadata)); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->any()) ->method('getClassName') @@ -230,19 +232,19 @@ public function testThrowsAnExceptionOnMissingIdentifierInContext(): void $match = new stdClass(); - $classMetadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata ->expects($this->once()) ->method('getIdentifierFieldNames') ->will($this->returnValue(['id'])); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with('stdClass') ->will($this->returnValue($classMetadata)); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->any()) ->method('getClassName') @@ -270,7 +272,7 @@ public function testThrowsAnExceptionOnMissingObjectManager(): void ObjectManager::class )); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); new UniqueObject([ 'object_repository' => $repository, @@ -288,7 +290,7 @@ public function testThrowsAnExceptionOnWrongObjectManager(): void $objectManager = new stdClass(); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); new UniqueObject([ 'object_repository' => $repository, @@ -300,14 +302,14 @@ public function testThrowsAnExceptionOnWrongObjectManager(): void public function testCanValidateWithNotAvailableObjectInRepositoryByDateTimeObject(): void { $date = new DateTime('17 March 2014'); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->once()) ->method('findOneBy') ->with(['date' => $date]) ->will($this->returnValue(null)); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $validator = new UniqueObject([ 'object_repository' => $repository, @@ -325,7 +327,7 @@ public function testCanFetchIdentifierFromObjectContext(): void $match = new stdClass(); - $classMetadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata ->expects($this->atLeastOnce()) ->method('getIdentifierValues') @@ -334,13 +336,13 @@ public function testCanFetchIdentifierFromObjectContext(): void [$match, ['id' => 'identifier']], ]); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with('stdClass') ->will($this->returnValue($classMetadata)); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->any()) ->method('getClassName') @@ -365,7 +367,7 @@ public function testErrorMessageIsStringInsteadArray(): void { $match = new stdClass(); - $classMetadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); + $classMetadata = $this->createMock(ClassMetadata::class); $classMetadata ->expects($this->once()) ->method('getIdentifierFieldNames') @@ -376,13 +378,13 @@ public function testErrorMessageIsStringInsteadArray(): void ->with($match) ->will($this->returnValue(['id' => 'identifier'])); - $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); + $objectManager = $this->createMock(ObjectManager::class); $objectManager->expects($this->any()) ->method('getClassMetadata') ->with('stdClass') ->will($this->returnValue($classMetadata)); - $repository = $this->createMock('Doctrine\Persistence\ObjectRepository'); + $repository = $this->createMock(ObjectRepository::class); $repository ->expects($this->any()) ->method('getClassName')