Commit 5c503f4a authored by larowlan's avatar larowlan

Issue #2825078 by Mile23, dawehner, Wim Leers, Gábor Hojtsy: Deprecate...

Issue #2825078 by Mile23, dawehner, Wim Leers, Gábor Hojtsy: Deprecate UnitTestCase dependency on block module
parent 63a95d58
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Drupal\Tests\block\Unit; namespace Drupal\Tests\block\Unit;
use Drupal\block\BlockForm; use Drupal\block\BlockForm;
use Drupal\block\Entity\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\PluginFormFactoryInterface; use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
...@@ -82,6 +84,32 @@ protected function setUp() { ...@@ -82,6 +84,32 @@ protected function setUp() {
$this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class); $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class);
} }
/**
* Mocks a block with a block plugin.
*
* @param string $machine_name
* The machine name of the block plugin.
*
* @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
* The mocked block.
*/
protected function getBlockMockWithMachineName($machine_name) {
$plugin = $this->getMockBuilder(BlockBase::class)
->disableOriginalConstructor()
->getMock();
$plugin->expects($this->any())
->method('getMachineNameSuggestion')
->will($this->returnValue($machine_name));
$block = $this->getMockBuilder(Block::class)
->disableOriginalConstructor()
->getMock();
$block->expects($this->any())
->method('getPlugin')
->will($this->returnValue($plugin));
return $block;
}
/** /**
* Tests the unique machine name generator. * Tests the unique machine name generator.
* *
......
...@@ -180,6 +180,12 @@ public function getConfigStorageStub(array $configs) { ...@@ -180,6 +180,12 @@ public function getConfigStorageStub(array $configs) {
* *
* @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
* The mocked block. * The mocked block.
*
* @deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test
* base classes should not have dependencies on extensions. Set up mocks in
* individual tests.
*
* @see https://www.drupal.org/node/2896072
*/ */
protected function getBlockMockWithMachineName($machine_name) { protected function getBlockMockWithMachineName($machine_name) {
$plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase') $plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase')
...@@ -195,6 +201,7 @@ protected function getBlockMockWithMachineName($machine_name) { ...@@ -195,6 +201,7 @@ protected function getBlockMockWithMachineName($machine_name) {
$block->expects($this->any()) $block->expects($this->any())
->method('getPlugin') ->method('getPlugin')
->will($this->returnValue($plugin)); ->will($this->returnValue($plugin));
@trigger_error(__METHOD__ . ' is deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test base classes should not have dependencies on extensions. Set up mocks in individual tests.', E_USER_DEPRECATED);
return $block; return $block;
} }
......
<?php
namespace Drupal\Tests;
/**
* @coversDefaultClass \Drupal\Tests\UnitTestCase
*
* @group Test
* @group legacy
*/
class UnitTestCaseDeprecationTest extends UnitTestCase {
/**
* @covers ::getBlockMockWithMachineName
* @expectedDeprecation Drupal\Tests\UnitTestCase::getBlockMockWithMachineName is deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test base classes should not have dependencies on extensions. Set up mocks in individual tests.
*/
public function testDeprecatedGetBlockMockWithMachineName() {
$block_mock = $this->getBlockMockWithMachineName('test_name');
$this->assertEquals('test_name', $block_mock->getPlugin()->getMachineNameSuggestion());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment