class ContextTest in Zircon Profile 8
Same name in this branch
- 8 vendor/sebastian/recursion-context/tests/ContextTest.php \SebastianBergmann\RecursionContext\ContextTest
- 8 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
- 8 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
@coversDefaultClass \Drupal\Core\Plugin\Context\Context @group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\Plugin\Context\ContextTest
Expanded class hierarchy of ContextTest
File
- core/
tests/ Drupal/ Tests/ Core/ Plugin/ Context/ ContextTest.php, line 20 - Contains \Drupal\Tests\Core\Plugin\Context\ContextTest.
Namespace
Drupal\Tests\Core\Plugin\ContextView source
class ContextTest extends UnitTestCase {
/**
* The mocked context definition object.
*
* @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $contextDefinition;
/**
* The mocked Typed Data manager.
*
* @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $typedDataManager;
/**
* The mocked Typed Data object.
*
* @var \Drupal\Core\TypedData\TypedDataInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $typedData;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->typedDataManager = $this
->getMockBuilder('Drupal\\Core\\TypedData\\TypedDataManager')
->disableOriginalConstructor()
->setMethods(array(
'create',
))
->getMock();
}
/**
* @covers ::getContextValue
*/
public function testDefaultValue() {
$this
->setUpDefaultValue('test');
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$this
->assertEquals('test', $context
->getContextValue());
}
/**
* @covers ::getContextData
*/
public function testDefaultDataValue() {
$this
->setUpDefaultValue('test');
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$this
->assertEquals($this->typedData, $context
->getContextData());
}
/**
* @covers ::getContextData
*/
public function testNullDataValue() {
$this
->setUpDefaultValue(NULL);
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$this
->assertEquals($this->typedData, $context
->getContextData());
}
/**
* @covers ::setContextValue
*/
public function testSetContextValueTypedData() {
$this->contextDefinition = $this
->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')
->setMethods(array(
'getDefaultValue',
'getDataDefinition',
))
->getMockForAbstractClass();
$typed_data = $this
->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
$context = new Context($this->contextDefinition, $typed_data);
$this
->assertSame($typed_data, $context
->getContextData());
}
/**
* @covers ::setContextValue
*/
public function testSetContextValueCacheableDependency() {
$container = new Container();
$cache_context_manager = $this
->getMockBuilder('Drupal\\Core\\Cache\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$container
->set('cache_contexts_manager', $cache_context_manager);
$cache_context_manager
->expects($this
->any())
->method('validateTokens')
->with([
'route',
])
->willReturn([
'route',
]);
\Drupal::setContainer($container);
$this->contextDefinition = $this
->getMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$cacheable_dependency = $this
->getMock('Drupal\\Tests\\Core\\Plugin\\Context\\TypedDataCacheableDependencyInterface');
$cacheable_dependency
->expects($this
->once())
->method('getCacheTags')
->willReturn([
'node:1',
]);
$cacheable_dependency
->expects($this
->once())
->method('getCacheContexts')
->willReturn([
'route',
]);
$cacheable_dependency
->expects($this
->once())
->method('getCacheMaxAge')
->willReturn(60);
$context = Context::createFromContext($context, $cacheable_dependency);
$this
->assertSame($cacheable_dependency, $context
->getContextData());
$this
->assertEquals([
'node:1',
], $context
->getCacheTags());
$this
->assertEquals([
'route',
], $context
->getCacheContexts());
$this
->assertEquals(60, $context
->getCacheMaxAge());
}
/**
* Set up mocks for the getDefaultValue() method call.
*
* @param mixed $default_value
* The default value to assign to the mock context definition.
*/
protected function setUpDefaultValue($default_value = NULL) {
$mock_data_definition = $this
->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
$this->contextDefinition = $this
->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')
->setMethods(array(
'getDefaultValue',
'getDataDefinition',
))
->getMockForAbstractClass();
$this->contextDefinition
->expects($this
->once())
->method('getDefaultValue')
->willReturn($default_value);
$this->contextDefinition
->expects($this
->once())
->method('getDataDefinition')
->willReturn($mock_data_definition);
$this->typedData = $this
->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
$this->typedDataManager
->expects($this
->once())
->method('create')
->with($mock_data_definition, $default_value)
->willReturn($this->typedData);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContextTest:: |
protected | property | The mocked context definition object. | |
ContextTest:: |
protected | property | The mocked Typed Data object. | |
ContextTest:: |
protected | property | The mocked Typed Data manager. | |
ContextTest:: |
public | function |
Overrides UnitTestCase:: |
|
ContextTest:: |
protected | function | Set up mocks for the getDefaultValue() method call. | |
ContextTest:: |
public | function | @covers ::getContextData | |
ContextTest:: |
public | function | @covers ::getContextValue | |
ContextTest:: |
public | function | @covers ::getContextData | |
ContextTest:: |
public | function | @covers ::setContextValue | |
ContextTest:: |
public | function | @covers ::setContextValue | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |