EntityContextDefinitionDeprecationTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionDeprecationTest.php
View source
<?php
namespace Drupal\Tests\Core\Plugin\Context;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\Core\Validation\ConstraintManager;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class EntityContextDefinitionDeprecationTest extends UnitTestCase {
protected $definition;
protected $compatibilityLayer;
protected function setUp() {
parent::setUp();
$constraint_manager = $this
->prophesize(ConstraintManager::class);
$constraint_manager
->create(Argument::type('string'), Argument::any())
->willReturn(TRUE);
$typed_data_manager = $this
->prophesize(TypedDataManagerInterface::class);
$typed_data_manager
->getValidationConstraintManager()
->willReturn($constraint_manager
->reveal());
$validator = $this
->prophesize(ValidatorInterface::class)
->reveal();
$typed_data_manager
->getValidator()
->willReturn($validator);
$container = new ContainerBuilder();
$container
->set('typed_data_manager', $typed_data_manager
->reveal());
\Drupal::setContainer($container);
$this->definition = new ContextDefinition('entity:node');
$reflector = new \ReflectionObject($this->definition);
$this
->assertTrue($reflector
->hasProperty('entityContextDefinition'));
$this->compatibilityLayer = $reflector
->getProperty('entityContextDefinition');
$this->compatibilityLayer
->setAccessible(TRUE);
$this
->assertInstanceOf(EntityContextDefinition::class, $this->compatibilityLayer
->getValue($this->definition));
}
public function testSerialization() {
$this->definition
->addConstraint('foo');
$definition = unserialize(serialize($this->definition));
$this
->assertEquals([
'foo' => NULL,
], $definition
->getConstraints());
$bc_layer = $this->compatibilityLayer
->getValue($definition);
$this
->assertInstanceOf(EntityContextDefinition::class, $bc_layer);
}
public function testGetConstraints() {
$bc_mock = $this
->getMockBuilder(EntityContextDefinition::class)
->setMethods([
'getConstraints',
])
->getMock();
$constraints = [
'test_constraint',
];
$bc_mock
->expects($this
->once())
->method('getConstraints')
->willReturn($constraints);
$this->compatibilityLayer
->setValue($this->definition, $bc_mock);
$this
->assertSame($constraints, $this->definition
->getConstraints());
}
public function testGetConstraint() {
$bc_mock = $this
->getMockBuilder(EntityContextDefinition::class)
->setMethods([
'getConstraint',
])
->getMock();
$bc_mock
->expects($this
->once())
->method('getConstraint')
->with('constraint_name')
->willReturn('test_constraint');
$this->compatibilityLayer
->setValue($this->definition, $bc_mock);
$this
->assertSame('test_constraint', $this->definition
->getConstraint('constraint_name'));
}
public function testSetConstraints() {
$bc_mock = $this
->getMockBuilder(EntityContextDefinition::class)
->setMethods([
'setConstraints',
])
->getMock();
$constraints = [
'TestConstraint' => [],
];
$bc_mock
->expects($this
->once())
->method('setConstraints')
->with($constraints)
->willReturnSelf();
$this->compatibilityLayer
->setValue($this->definition, $bc_mock);
$this
->assertSame($this->definition, $this->definition
->setConstraints($constraints));
}
public function testAddConstraint() {
$bc_mock = $this
->getMockBuilder(EntityContextDefinition::class)
->setMethods([
'addConstraint',
])
->getMock();
$options = [
'options',
];
$bc_mock
->expects($this
->once())
->method('addConstraint')
->with('constraint_name', $options)
->willReturnSelf();
$this->compatibilityLayer
->setValue($this->definition, $bc_mock);
$this
->assertSame($this->definition, $this->definition
->addConstraint('constraint_name', $options));
}
public function testIsSatisfiedBy() {
$bc_mock = $this
->getMockBuilder(EntityContextDefinition::class)
->setMethods([
'getSampleValues',
])
->getMock();
$bc_mock
->expects($this
->atLeastOnce())
->method('getSampleValues')
->willReturn([]);
$this->compatibilityLayer
->setValue($this->definition, $bc_mock);
$this->definition
->isSatisfiedBy(new Context($this->definition));
}
public function testGetConstraintObjects() {
$reflector = new \ReflectionObject($this->definition);
$method = $reflector
->getMethod('getConstraintObjects');
$method
->setAccessible(TRUE);
$this
->assertArrayHasKey('EntityType', $method
->invoke($this->definition));
}
}