123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Validator\Test\Unit;
- /**
- * Class BuilderTest
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class BuilderTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $_objectManager;
- /**
- * @var \Magento\Framework\ObjectManager\ObjectManager
- */
- protected $_realObjectManager;
- protected function setUp()
- {
- $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $config = new \Magento\Framework\ObjectManager\Config\Config(
- new \Magento\Framework\ObjectManager\Relations\Runtime()
- );
- $factory = new \Magento\Framework\ObjectManager\Factory\Dynamic\Developer($config);
- $this->_realObjectManager = new \Magento\Framework\ObjectManager\ObjectManager($factory, $config);
- $factory->setObjectManager($this->_realObjectManager);
- }
- /**
- * Test createValidator method
- *
- * @dataProvider createValidatorDataProvider
- *
- * @param array $constraints
- * @param \Magento\Framework\Validator\ValidatorInterface $expectedValidator
- */
- public function testCreateValidator(array $constraints, $expectedValidator)
- {
- /** @var $builder \Magento\Framework\Validator\Builder */
- $builder = $this->_objectManager->getObject(
- \Magento\Framework\Validator\Builder::class,
- [
- 'constraintFactory' => new \Magento\Framework\Validator\ConstraintFactory($this->_realObjectManager),
- 'validatorFactory' => new \Magento\Framework\ValidatorFactory($this->_realObjectManager),
- 'oneValidatorFactory' => new \Magento\Framework\Validator\UniversalFactory($this->_realObjectManager),
- 'constraints' => $constraints
- ]
- );
- $actualValidator = $builder->createValidator();
- $this->assertEquals($expectedValidator, $actualValidator);
- }
- /**
- * Data provider for
- *
- * @return array
- */
- public function createValidatorDataProvider()
- {
- $result = [];
- /** @var \Magento\Framework\Translate\AbstractAdapter $translator */
- $translator = $this->getMockBuilder(
- \Magento\Framework\Translate\AbstractAdapter::class
- )->getMockForAbstractClass();
- \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator);
- // Case 1. Check constructor with arguments
- $actualConstraints = [
- [
- 'alias' => 'name_alias',
- 'class' => \Magento\Framework\Validator\Test\Unit\Test\StringLength::class,
- 'options' => [
- 'arguments' => [
- 'options' => ['min' => 1, 'max' => new \Magento\Framework\Validator\Constraint\Option(20)],
- ],
- ],
- 'property' => 'name',
- 'type' => 'property',
- ],
- ];
- $expectedValidator = new \Magento\Framework\Validator();
- $expectedValidator->addValidator(
- new \Magento\Framework\Validator\Constraint\Property(
- new \Magento\Framework\Validator\Test\Unit\Test\StringLength(1, 20),
- 'name',
- 'name_alias'
- )
- );
- $result[] = [$actualConstraints, $expectedValidator];
- // Case 2. Check method calls
- $actualConstraints = [
- [
- 'alias' => 'description_alias',
- 'class' => \Magento\Framework\Validator\Test\Unit\Test\StringLength::class,
- 'options' => [
- 'methods' => [
- ['method' => 'setMin', 'arguments' => [10]],
- ['method' => 'setMax', 'arguments' => [1000]],
- ],
- ],
- 'property' => 'description',
- 'type' => 'property',
- ],
- ];
- $expectedValidator = new \Magento\Framework\Validator();
- $expectedValidator->addValidator(
- new \Magento\Framework\Validator\Constraint\Property(
- new \Magento\Framework\Validator\Test\Unit\Test\StringLength(10, 1000),
- 'description',
- 'description_alias'
- )
- );
- $result[] = [$actualConstraints, $expectedValidator];
- // Case 3. Check callback on validator
- $actualConstraints = [
- [
- 'alias' => 'sku_alias',
- 'class' => \Magento\Framework\Validator\Test\Unit\Test\StringLength::class,
- 'options' => [
- 'callback' => [
- new \Magento\Framework\Validator\Constraint\Option\Callback(
- function ($validator) {
- $validator->setMin(20);
- $validator->setMax(100);
- }
- ), ], ],'property' => 'sku', 'type' => 'property', ], ];
- $expectedValidator = new \Magento\Framework\Validator();
- $expectedValidator->addValidator(
- new \Magento\Framework\Validator\Constraint\Property(
- new \Magento\Framework\Validator\Test\Unit\Test\StringLength(20, 100),
- 'sku',
- 'sku_alias'
- )
- );
- $result[] = [$actualConstraints, $expectedValidator];
- return $result;
- }
- /**
- * Check addConfiguration logic
- *
- * @dataProvider configurationDataProvider
- *
- * @param array $constraints
- * @param string $alias
- * @param array $configuration
- * @param array $expected
- */
- public function testAddConfiguration($constraints, $alias, $configuration, $expected)
- {
- /** @var $builder \Magento\Framework\Validator\Builder */
- $builder = $this->_objectManager->getObject(
- \Magento\Framework\Validator\Builder::class,
- ['constraints' => $constraints]
- );
- $builder->addConfiguration($alias, $configuration);
- $this->assertAttributeEquals($expected, '_constraints', $builder);
- }
- /**
- * Check addConfigurations logic
- *
- * @dataProvider configurationDataProvider
- *
- * @param array $constraints
- * @param string $alias
- * @param array $configuration
- * @param array $expected
- */
- public function testAddConfigurations($constraints, $alias, $configuration, $expected)
- {
- /** @var $builder \Magento\Framework\Validator\Builder */
- $builder = $this->_objectManager->getObject(
- \Magento\Framework\Validator\Builder::class,
- ['constraints' => $constraints]
- );
- $configurations = [$alias => [$configuration]];
- $builder->addConfigurations($configurations);
- $this->assertAttributeEquals($expected, '_constraints', $builder);
- }
- /**
- * Builder configurations data provider
- *
- * @return array
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function configurationDataProvider()
- {
- $callback = new \Magento\Framework\Validator\Constraint\Option\Callback(
- [\Magento\Framework\Validator\Test\Unit\Test\Callback::class, 'getId']
- );
- $someMethod = ['method' => 'getMessages'];
- $methodWithArgs = ['method' => 'someMethod', 'arguments' => ['some_value_to_pass']];
- $callbackConfig = ['callback' => $callback];
- $configuredConstraint = [
- 'alias' => 'current_alias',
- 'class' => 'Some\Validator\Class',
- 'options' => [
- 'arguments' => ['some_argument' => 'some_value'],
- 'callback' => [$callback],
- 'methods' => [$someMethod],
- ],
- 'property' => 'int',
- 'type' => 'property',
- ];
- $emptyConstraint = [
- 'alias' => 'current_alias',
- 'class' => 'Some\Validator\Class',
- 'options' => null,
- 'property' => 'int',
- 'type' => 'property',
- ];
- $constraintWithArgs = [
- 'alias' => 'current_alias',
- 'class' => 'Some\Validator\Class',
- 'options' => ['arguments' => ['some_argument' => 'some_value']],
- 'property' => 'int',
- 'type' => 'property',
- ];
- return [
- 'constraint is unchanged when alias not found' => [
- [$emptyConstraint],
- 'some_alias',
- $someMethod,
- [$emptyConstraint],
- ],
- 'constraint options initialized with method' => [
- [$emptyConstraint],
- 'current_alias',
- $someMethod,
- [$this->_getExpectedConstraints($emptyConstraint, 'methods', [$someMethod])],
- ],
- 'constraint options initialized with callback' => [
- [$emptyConstraint],
- 'current_alias',
- $callbackConfig,
- [$this->_getExpectedConstraints($emptyConstraint, 'callback', [$callback])],
- ],
- 'constraint options initialized with arguments' => [
- [$emptyConstraint],
- 'current_alias',
- ['arguments' => ['some_argument' => 'some_value']],
- [
- $this->_getExpectedConstraints(
- $emptyConstraint,
- 'arguments',
- ['some_argument' => 'some_value']
- )
- ],
- ],
- 'constraint options arguments overwritten by newer arguments' => [
- [$configuredConstraint],
- 'current_alias',
- ['arguments' => ['some_argument' => 'some_value']],
- [
- $this->_getExpectedConstraints(
- $configuredConstraint,
- 'arguments',
- ['some_argument' => 'some_value']
- )
- ],
- ],
- 'methods initialized' => [
- [$constraintWithArgs],
- 'current_alias',
- $methodWithArgs,
- [$this->_getExpectedConstraints($constraintWithArgs, 'methods', [$methodWithArgs])],
- ],
- 'method added' => [
- [$configuredConstraint],
- 'current_alias',
- $methodWithArgs,
- [
- $this->_getExpectedConstraints(
- $configuredConstraint,
- 'methods',
- [$someMethod, $methodWithArgs]
- )
- ],
- ],
- 'callback initialized' => [
- [$constraintWithArgs],
- 'current_alias',
- $callbackConfig,
- [$this->_getExpectedConstraints($constraintWithArgs, 'callback', [$callback])],
- ],
- 'callback added' => [
- [$configuredConstraint],
- 'current_alias',
- $callbackConfig,
- [$this->_getExpectedConstraints($configuredConstraint, 'callback', [$callback, $callback])],
- ]
- ];
- }
- /**
- * Get expected constraint configuration by actual and changes
- *
- * @param array $constraint
- * @param string $optionKey
- * @param mixed $optionValue
- * @return array
- */
- protected function _getExpectedConstraints($constraint, $optionKey, $optionValue)
- {
- if (!is_array($constraint['options'])) {
- $constraint['options'] = [];
- }
- $constraint['options'][$optionKey] = $optionValue;
- return $constraint;
- }
- /**
- * Check arguments validation passed into constructor
- *
- * @dataProvider invalidArgumentsDataProvider
- *
- * @param array $options
- * @param string $exception
- * @param string $exceptionMessage
- */
- public function testConstructorConfigValidation(array $options, $exception, $exceptionMessage)
- {
- $this->expectException($exception);
- $this->expectExceptionMessage($exceptionMessage);
- if (array_key_exists('method', $options)) {
- $options = ['methods' => [$options]];
- }
- $constraints = [
- ['alias' => 'alias', 'class' => 'Some\Validator\Class', 'options' => $options, 'type' => 'entity'],
- ];
- $this->_objectManager->getObject(\Magento\Framework\Validator\Builder::class, ['constraints' => $constraints]);
- }
- /**
- * Check arguments validation passed into configuration
- *
- * @dataProvider invalidArgumentsDataProvider
- *
- * @param array $options
- * @param string $exception
- * @param string $exceptionMessage
- */
- public function testAddConfigurationConfigValidation(array $options, $exception, $exceptionMessage)
- {
- $this->expectException($exception);
- $this->expectExceptionMessage($exceptionMessage);
- $constraints = [
- ['alias' => 'alias', 'class' => 'Some\Validator\Class', 'options' => null, 'type' => 'entity'],
- ];
- /** @var $builder \Magento\Framework\Validator\Builder */
- $builder = $this->_objectManager->getObject(
- \Magento\Framework\Validator\Builder::class,
- ['constraints' => $constraints]
- );
- $builder->addConfiguration('alias', $options);
- }
- /**
- * Data provider for testing configuration validation
- *
- * @return array
- */
- public function invalidArgumentsDataProvider()
- {
- return [
- 'constructor invalid arguments' => [
- ['arguments' => 'invalid_argument'],
- 'InvalidArgumentException',
- 'Arguments must be an array',
- ],
- 'methods invalid arguments' => [
- ['method' => 'setValue', 'arguments' => 'invalid_argument'],
- 'InvalidArgumentException',
- 'Method arguments must be an array',
- ],
- 'methods invalid format' => [
- ['method' => ['name' => 'setValue']],
- 'InvalidArgumentException',
- 'Method has to be passed as string',
- ],
- 'constructor arguments invalid callback' => [
- ['callback' => ['invalid', 'callback']],
- 'InvalidArgumentException',
- 'Callback must be instance of \Magento\Framework\Validator\Constraint\Option\Callback',
- ]
- ];
- }
- /**
- * Check exception is thrown if validator is not an instance of \Magento\Framework\Validator\ValidatorInterface
- */
- public function testCreateValidatorInvalidInstance()
- {
- $this->expectException('InvalidArgumentException');
- $this->expectExceptionMessage(
- 'Constraint class "StdClass" must implement \Magento\Framework\Validator\ValidatorInterface'
- );
-
- $builder = $this->_objectManager->getObject(
- \Magento\Framework\Validator\Builder::class,
- [
- 'constraints' => [
- ['alias' => 'alias', 'class' => 'StdClass', 'options' => null, 'type' => 'entity'],
- ],
- 'validatorFactory' => new \Magento\Framework\ValidatorFactory($this->_realObjectManager)
- ]
- );
- $builder->createValidator();
- }
- /**
- * Test invalid configuration formats
- *
- * @dataProvider invalidConfigurationFormatDataProvider
- *
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Configuration has incorrect format
- *
- * @param mixed $configuration
- */
- public function testAddConfigurationInvalidFormat($configuration)
- {
- $constraints = [
- ['alias' => 'alias', 'class' => 'Some\Validator\Class', 'options' => null, 'type' => 'entity'],
- ];
- /** @var $builder \Magento\Framework\Validator\Builder */
- $builder = $this->_objectManager->getObject(
- \Magento\Framework\Validator\Builder::class,
- ['constraints' => $constraints]
- );
- $builder->addConfigurations($configuration);
- }
- /**
- * Data provider for incorrect configurations
- *
- * @return array
- */
- public function invalidConfigurationFormatDataProvider()
- {
- return [
- 'configuration incorrect method call' => [
- ['alias' => ['method' => ['name' => 'incorrectMethodCall']]],
- ],
- 'configuration incorrect configuration' => [
- ['alias' => [['data' => ['incorrectData']]]],
- ]
- ];
- }
- }
|