Verified Commit a71cbc33 authored by Dave Long's avatar Dave Long
Browse files

test: #3569418 Convert expectation-less test mocks to stubs - Block module

By: dcam
By: smustgrave
(cherry picked from commit f72041cd)
parent e82c7aee
Loading
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@
namespace Drupal\Tests\block\Unit;

use Drupal\block\Entity\Block;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
@@ -24,14 +26,14 @@ class BlockConfigEntityUnitTest extends UnitTestCase {
  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $entityType;

  /**
   * The entity type manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $entityTypeManager;

@@ -45,7 +47,7 @@ class BlockConfigEntityUnitTest extends UnitTestCase {
  /**
   * The UUID generator used for testing.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $uuid;

@@ -71,18 +73,15 @@ protected function setUp(): void {

    $this->entityTypeId = $this->randomMachineName();

    $this->entityType = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
    $this->entityType->expects($this->any())
      ->method('getProvider')
      ->willReturn('block');
    $this->entityType = $this->createStub(EntityTypeInterface::class);

    $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
    $this->entityTypeManager->expects($this->any())
    $this->entityTypeManager->expects($this->once())
      ->method('getDefinition')
      ->with($this->entityTypeId)
      ->willReturn($this->entityType);

    $this->uuid = $this->createMock('\Drupal\Component\Uuid\UuidInterface');
    $this->uuid = $this->createStub(UuidInterface::class);

    $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
    $this->themeHandler = $this->prophesize(ThemeHandlerInterface::class);
+28 −25
Original line number Diff line number Diff line
@@ -8,7 +8,14 @@
use Drupal\block\BlockRepository;
use Drupal\block\Entity\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
@@ -23,7 +30,7 @@ class BlockFormTest extends UnitTestCase {
  /**
   * The condition plugin manager.
   *
   * @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $conditionManager;

@@ -37,42 +44,42 @@ class BlockFormTest extends UnitTestCase {
  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $language;

  /**
   * The theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $themeHandler;

  /**
   * The theme manager service.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $themeManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $entityTypeManager;

  /**
   * The mocked context handler.
   *
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $contextHandler;

  /**
   * The mocked context repository.
   *
   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $contextRepository;

@@ -96,21 +103,21 @@ class BlockFormTest extends UnitTestCase {
  protected function setUp(): void {
    parent::setUp();

    $this->conditionManager = $this->createMock('Drupal\Core\Executable\ExecutableManagerInterface');
    $this->language = $this->createMock('Drupal\Core\Language\LanguageManagerInterface');
    $this->contextRepository = $this->createMock('Drupal\Core\Plugin\Context\ContextRepositoryInterface');
    $this->conditionManager = $this->createStub(ExecutableManagerInterface::class);
    $this->language = $this->createStub(LanguageManagerInterface::class);
    $this->contextRepository = $this->createStub(ContextRepositoryInterface::class);

    $this->entityTypeManager = $this->createMock('Drupal\Core\Entity\EntityTypeManagerInterface');
    $this->entityTypeManager = $this->createStub(EntityTypeManagerInterface::class);
    $this->storage = $this->createMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
    $this->themeHandler = $this->createMock('Drupal\Core\Extension\ThemeHandlerInterface');
    $this->entityTypeManager->expects($this->any())
    $this->themeHandler = $this->createStub(ThemeHandlerInterface::class);
    $this->entityTypeManager
      ->method('getStorage')
      ->willReturn($this->storage);

    $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class);

    $this->themeManager = $this->createMock('\Drupal\Core\Theme\ThemeManagerInterface');
    $this->contextHandler = $this->createMock('Drupal\Core\Plugin\Context\ContextHandlerInterface');
    $this->themeManager = $this->createStub(ThemeManagerInterface::class);
    $this->contextHandler = $this->createStub(ContextHandlerInterface::class);
    $this->blockRepository = new BlockRepository($this->entityTypeManager, $this->themeManager, $this->contextHandler);
  }

@@ -120,21 +127,17 @@ protected function setUp(): void {
   * @param string $machine_name
   *   The machine name of the block plugin.
   *
   * @return \Drupal\block\BlockInterface|\PHPUnit\Framework\MockObject\MockObject
   *   The mocked block.
   * @return \Drupal\block\BlockInterface|\PHPUnit\Framework\MockObject\Stub
   *   The stub block.
   */
  protected function getBlockMockWithMachineName($machine_name) {
    $plugin = $this->getMockBuilder(BlockBase::class)
      ->disableOriginalConstructor()
      ->getMock();
    $plugin->expects($this->any())
    $plugin = $this->createStub(BlockBase::class);
    $plugin
      ->method('getMachineNameSuggestion')
      ->willReturn($machine_name);

    $block = $this->getMockBuilder(Block::class)
      ->disableOriginalConstructor()
      ->getMock();
    $block->expects($this->any())
    $block = $this->createStub(Block::class);
    $block
      ->method('getPlugin')
      ->willReturn($plugin);
    return $block;
+7 −6
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\block\BlockRepository;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
@@ -43,7 +44,7 @@ class BlockRepositoryTest extends UnitTestCase {
  /**
   * The context handler of a mock.
   *
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit\Framework\MockObject\Stub
   */
  protected $contextHandler;

@@ -72,11 +73,11 @@ protected function setUp(): void {
      ->method('getActiveTheme')
      ->willReturn($active_theme);

    $this->contextHandler = $this->createMock('Drupal\Core\Plugin\Context\ContextHandlerInterface');
    $this->contextHandler = $this->createStub(ContextHandlerInterface::class);
    $this->blockStorage = $this->createMock('Drupal\Core\Entity\EntityStorageInterface');
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject $entity_type_manager */
    $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
    $entity_type_manager->expects($this->any())
    $entity_type_manager = $this->createStub(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->method('getStorage')
      ->willReturn($this->blockStorage);

@@ -97,10 +98,10 @@ public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expec
      $block->expects($block_config[0] ? $this->atLeastOnce() : $this->never())
        ->method('getRegion')
        ->willReturn($block_config[1]);
      $block->expects($this->any())
      $block
        ->method('label')
        ->willReturn($block_id);
      $block->expects($this->any())
      $block
        ->method('getWeight')
        ->willReturn($block_config[2]);
      $blocks[$block_id] = $block;
+3 −2
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

use Drupal\block\Controller\CategoryAutocompleteController;
use Drupal\Component\Utility\Html;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
@@ -32,8 +33,8 @@ class CategoryAutocompleteTest extends UnitTestCase {
  protected function setUp(): void {
    parent::setUp();

    $block_manager = $this->createMock('Drupal\Core\Block\BlockManagerInterface');
    $block_manager->expects($this->any())
    $block_manager = $this->createStub(BlockManagerInterface::class);
    $block_manager
      ->method('getCategories')
      ->willReturn(['Comment', 'Node', 'None & Such', 'User']);

+4 −3
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

namespace Drupal\Tests\block\Unit\Menu;

use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
@@ -46,11 +47,11 @@ protected function setUp(): void {
        'name' => 'test_c',
      ],
    ];
    $theme_handler = $this->createMock('Drupal\Core\Extension\ThemeHandlerInterface');
    $theme_handler->expects($this->any())
    $theme_handler = $this->createStub(ThemeHandlerInterface::class);
    $theme_handler
      ->method('listInfo')
      ->willReturn($themes);
    $theme_handler->expects($this->any())
    $theme_handler
      ->method('hasUi')
      ->willReturnMap([
        ['test_a', FALSE],
Loading