class EntityConditionTest

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Unit/Query/EntityConditionTest.php \Drupal\Tests\jsonapi\Unit\Query\EntityConditionTest
  2. 8.9.x core/modules/jsonapi/tests/src/Unit/Query/EntityConditionTest.php \Drupal\Tests\jsonapi\Unit\Query\EntityConditionTest
  3. 11.x core/modules/jsonapi/tests/src/Unit/Query/EntityConditionTest.php \Drupal\Tests\jsonapi\Unit\Query\EntityConditionTest

@coversDefaultClass \Drupal\jsonapi\Query\EntityCondition @group jsonapi

@internal

Hierarchy

Expanded class hierarchy of EntityConditionTest

File

core/modules/jsonapi/tests/src/Unit/Query/EntityConditionTest.php, line 20

Namespace

Drupal\Tests\jsonapi\Unit\Query
View source
class EntityConditionTest extends UnitTestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $container = new Container();
        $cache_context_manager = $this->prophesize(CacheContextsManager::class);
        $cache_context_manager->assertValidTokens(Argument::any())
            ->willReturn(TRUE);
        $container->set('cache_contexts_manager', $cache_context_manager->reveal());
        \Drupal::setContainer($container);
    }
    
    /**
     * @covers ::createFromQueryParameter
     * @dataProvider queryParameterProvider
     */
    public function testCreateFromQueryParameter($case) : void {
        $condition = EntityCondition::createFromQueryParameter($case);
        $this->assertEquals($case['path'], $condition->field());
        $this->assertEquals($case['value'], $condition->value());
        if (isset($case['operator'])) {
            $this->assertEquals($case['operator'], $condition->operator());
        }
    }
    
    /**
     * Data provider for testDenormalize.
     */
    public static function queryParameterProvider() {
        return [
            [
                [
                    'path' => 'some_field',
                    'value' => NULL,
                    'operator' => '=',
                ],
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => '=',
                    'value' => 'some_string',
                ],
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => '<>',
                    'value' => 'some_string',
                ],
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'NOT BETWEEN',
                    'value' => 'some_string',
                ],
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'BETWEEN',
                    'value' => [
                        'some_string',
                    ],
                ],
            ],
        ];
    }
    
    /**
     * @covers ::validate
     * @dataProvider validationProvider
     */
    public function testValidation($input, $exception) : void {
        if ($exception) {
            $this->expectException(get_class($exception));
            $this->expectExceptionMessage($exception->getMessage());
        }
        EntityCondition::createFromQueryParameter($input);
        $this->assertNull($exception, 'No exception was expected.');
    }
    
    /**
     * Data provider for testValidation.
     */
    public static function validationProvider() {
        return [
            [
                [
                    'path' => 'some_field',
                    'value' => 'some_value',
                ],
                NULL,
            ],
            [
                [
                    'path' => 'some_field',
                    'value' => 'some_value',
                    'operator' => '=',
                ],
                NULL,
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'IS NULL',
                ],
                NULL,
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'IS NOT NULL',
                ],
                NULL,
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'IS',
                    'value' => 'some_value',
                ],
                new BadRequestHttpException("The 'IS' operator is not allowed in a filter parameter."),
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'NOT_ALLOWED',
                    'value' => 'some_value',
                ],
                new BadRequestHttpException("The 'NOT_ALLOWED' operator is not allowed in a filter parameter."),
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'IS NULL',
                    'value' => 'should_not_be_here',
                ],
                new BadRequestHttpException("Filters using the 'IS NULL' operator should not provide a value."),
            ],
            [
                [
                    'path' => 'some_field',
                    'operator' => 'IS NOT NULL',
                    'value' => 'should_not_be_here',
                ],
                new BadRequestHttpException("Filters using the 'IS NOT NULL' operator should not provide a value."),
            ],
            [
                [
                    'path' => 'path_only',
                ],
                new BadRequestHttpException("Filter parameter is missing a '" . EntityCondition::VALUE_KEY . "' key."),
            ],
            [
                [
                    'value' => 'value_only',
                ],
                new BadRequestHttpException("Filter parameter is missing a '" . EntityCondition::PATH_KEY . "' key."),
            ],
        ];
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityConditionTest::queryParameterProvider public static function Data provider for testDenormalize.
EntityConditionTest::setUp protected function Overrides UnitTestCase::setUp
EntityConditionTest::testCreateFromQueryParameter public function @covers ::createFromQueryParameter
@dataProvider queryParameterProvider
EntityConditionTest::testValidation public function @covers ::validate
@dataProvider validationProvider
EntityConditionTest::validationProvider public static function Data provider for testValidation.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.