Verified Commit 78f32f6b authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3583606 Fix usage of with*() without expects() in mocks - round 1

By: mondrake
By: dcam
parent 0f17f619
Loading
Loading
Loading
Loading
Loading
+2 −2
Changes for core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -516,7 +516,7 @@ public function testQueryWithGroupByForBaseField(): void {
    $entity_storage
      ->method('getTableMapping')
      ->willReturn($table_mapping);
    $this->entityTypeManager
    $this->entityTypeManager->expects($this->once())
      ->method('getStorage')
      ->with('test_entity')
      ->willReturn($entity_storage);
@@ -579,7 +579,7 @@ public function testQueryWithGroupByForConfigField(): void {
    $entity_storage
      ->method('getTableMapping')
      ->willReturn($table_mapping);
    $this->entityTypeManager
    $this->entityTypeManager->expects($this->once())
      ->method('getStorage')
      ->with('test_entity')
      ->willReturn($entity_storage);
+17 −11
Changes for core/modules/views/tests/src/Unit/ViewExecutableTest.php: 17 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -289,7 +289,7 @@ public function testGetUrlWithPlaceholdersAndWithoutArgs(): void {
      ->willReturn('test-path/%/%');

    $route = new Route('/test-path/{arg_0}/{arg_1}');
    $this->routeProvider
    $this->routeProvider->expects($this->once())
      ->method('getRouteByName')
      ->with('views.test.page_1')
      ->willReturn($route);
@@ -317,7 +317,7 @@ public function testGetUrlWithPlaceholdersAndWithoutArgsAndExceptionValue(): voi
      ->willReturn('test-path/%/%');

    $route = new Route('/test-path/{arg_0}/{arg_1}');
    $this->routeProvider
    $this->routeProvider->expects($this->once())
      ->method('getRouteByName')
      ->with('views.test.page_1')
      ->willReturn($route);
@@ -565,10 +565,14 @@ public static function providerAttachDisplays(): array {
  /**
   * Setups a view executable and default display.
   *
   * @param int $getPluginCallCount
   *   (optional) The number of expected calls to the ::getPlugin method.
   *   Defaults to 0.
   *
   * @return array
   *   Returns the view executable and default display.
   */
  protected function setupBaseViewAndDisplay(): array {
  protected function setupBaseViewAndDisplay(int $getPluginCallCount = 0): array {
    $config = [
      'id' => 'test_view',
      'tag' => 'OnE, TWO, and three',
@@ -584,7 +588,7 @@ protected function setupBaseViewAndDisplay(): array {
    $storage = new View($config, 'view');
    $view = new ViewExecutable($storage, $this->user, $this->viewsData, $this->routeProvider, $this->displayPluginManager);
    $display = $this->createMock(DisplayPluginBase::class);
    $display
    $display->expects($this->exactly($getPluginCallCount))
      ->method('getPlugin')
      ->with($this->equalTo('cache'))
      ->willReturn($this->successCache);
@@ -733,7 +737,7 @@ public function testCacheIsIgnoredDuringPreview(): void {

    $noneCache = $this->createMock(NoneCache::class);
    $noneCache->expects($this->once())->method('cacheGet');
    $this->cacheManager->method('createInstance')
    $this->cacheManager->expects($this->once())->method('createInstance')
      ->with('none')
      ->willReturn($noneCache);
    $query->expects($this->once())->method('execute');
@@ -748,12 +752,14 @@ public function testCacheIsIgnoredDuringPreview(): void {
   *   Whether the display to test should be enabled.
   * @param bool $expected_result
   *   The expected result when calling execute().
   * @param int $getPluginCallCount
   *   The number of expected calls to the ::getPlugin method.
   */
  #[DataProvider('providerExecuteReturn')]
  public function testExecuteReturn($display_enabled, bool $expected_result): void {
    /** @var \Drupal\views\ViewExecutable|\PHPUnit\Framework\MockObject\MockObject $view */
    /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit\Framework\MockObject\MockObject $display */
    [$view, $display] = $this->setupBaseViewAndDisplay();
  public function testExecuteReturn($display_enabled, bool $expected_result, int $getPluginCallCount): void {
    /** @var \Drupal\views\ViewExecutable&\PHPUnit\Framework\MockObject\MockObject $view */
    /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase&\PHPUnit\Framework\MockObject\MockObject $display */
    [$view, $display] = $this->setupBaseViewAndDisplay($getPluginCallCount);

    $display
      ->method('isEnabled')
@@ -778,8 +784,8 @@ public function testExecuteReturn($display_enabled, bool $expected_result): void
   */
  public static function providerExecuteReturn(): array {
    return [
      'enabled' => [static::DISPLAY_ENABLED, TRUE],
      'disabled' => [static::DISPLAY_DISABLED, FALSE],
      'enabled' => [static::DISPLAY_ENABLED, TRUE, 1],
      'disabled' => [static::DISPLAY_DISABLED, FALSE, 0],
    ];
  }

+1 −0
Changes for core/tests/Drupal/Tests/Core/Access/AccessResultTest.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -494,6 +494,7 @@ public function testCacheContexts(): void {
    // ::allowIfHasPermission and ::allowedIfHasPermission convenience methods.
    $account = $this->createMock('\Drupal\Core\Session\AccountInterface');
    $account
      ->expects($this->once())
      ->method('hasPermission')
      ->with('may herd llamas')
      ->willReturn(FALSE);
+4 −5
Changes for core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php: 4 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;

/**
 * Tests Drupal\Core\Asset\LibraryDependencyResolver.
@@ -30,7 +30,7 @@ class LibraryDependencyResolverTest extends UnitTestCase {
  /**
   * The mocked library discovery service.
   */
  protected LibraryDiscoveryInterface&MockObject $libraryDiscovery;
  protected LibraryDiscoveryInterface&Stub $libraryDiscovery;

  /**
   * Test library data.
@@ -57,13 +57,12 @@ class LibraryDependencyResolverTest extends UnitTestCase {
  protected function setUp(): void {
    parent::setUp();

    $this->libraryDiscovery = $this->getMockBuilder(LibraryDiscoveryCollector::class)
    $this->libraryDiscovery = $this->getStubBuilder(LibraryDiscoveryCollector::class)
      ->disableOriginalConstructor()
      ->onlyMethods(['getLibrariesByExtension'])
      ->getMock();
      ->getStub();
    $this->libraryDiscovery
      ->method('getLibrariesByExtension')
      ->with('test')
      ->willReturn($this->libraryData);
    $this->libraryDependencyResolver = new LibraryDependencyResolver($this->libraryDiscovery);
  }
+58 −18
Changes for core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php: 58 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheTagsInvalidator;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityBase;
@@ -16,12 +17,14 @@
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteProvider;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;

/**
 * Tests Drupal\Core\Entity\EntityBase.
@@ -33,10 +36,8 @@ class EntityUnitTest extends UnitTestCase {

  /**
   * The entity under test.
   *
   * @var \Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entity;
  protected StubEntityBase $entity;

  /**
   * The entity type used for testing.
@@ -50,17 +51,13 @@ class EntityUnitTest extends UnitTestCase {

  /**
   * The ID of the type of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;
  protected string $entityTypeId;

  /**
   * The route provider used for testing.
   *
   * @var \Drupal\Core\Routing\RouteProvider|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $routeProvider;
  protected RouteProvider&MockObject $routeProvider;

  /**
   * The UUID generator used for testing.
@@ -75,16 +72,16 @@ class EntityUnitTest extends UnitTestCase {
  /**
   * The mocked cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\Prophecy\Prophecy\ObjectProphecy
   * @var \Prophecy\Prophecy\ObjectProphecy<\Drupal\Core\Cache\CacheTagsInvalidatorInterface>
   */
  protected $cacheTagsInvalidator;
  protected ObjectProphecy $cacheTagsInvalidator;

  /**
   * The entity values.
   *
   * @var array
   */
  protected $values;
  protected array $values;

  /**
   * {@inheritdoc}
@@ -98,31 +95,46 @@ protected function setUp(): void {
      'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
    ];
    $this->entityTypeId = $this->randomMachineName();
  }

    $this->entityType = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
  /**
   * Sets up the entity under test.
   *
   * @param int $getBundleListCacheTagsCallsCount
   *   The number of expected calls to the
   *   EntityTypeInterface::getBundleListCacheTags() method.
   * @param int $getDefinitionCallsCount
   *   The number of expected calls to the
   *   EntityTypeManagerInterface::getDefinition() method.
   * @param int $getLanguageCallsCount
   *   The number of expected calls to the
   *   LanguageManagerInterface::getLanguage() method.
   */
  protected function setUpEntity(int $getBundleListCacheTagsCallsCount, int $getDefinitionCallsCount, int $getLanguageCallsCount): void {
    $this->entityType = $this->createMock(EntityTypeInterface::class);
    $this->entityType
      ->method('getListCacheTags')
      ->willReturn([$this->entityTypeId . '_list']);
    $this->entityType
    $this->entityType->expects($this->exactly($getBundleListCacheTagsCallsCount))
      ->method('getBundleListCacheTags')
      ->with($this->entityTypeId)
      ->willReturn([$this->entityTypeId . '_list:' . $this->entityTypeId]);

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

    $this->uuid = $this->createStub(UuidInterface::class);

    $this->languageManager = $this->createMock('\Drupal\Core\Language\LanguageManagerInterface');
    $this->languageManager
    $this->languageManager = $this->createMock(LanguageManagerInterface::class);
    $this->languageManager->expects($this->exactly($getLanguageCallsCount))
      ->method('getLanguage')
      ->with('en')
      ->willReturn(new Language(['id' => 'en']));

    $this->cacheTagsInvalidator = $this->prophesize('Drupal\Core\Cache\CacheTagsInvalidator');
    $this->cacheTagsInvalidator = $this->prophesize(CacheTagsInvalidator::class);

    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $this->entityTypeManager);
@@ -138,6 +150,7 @@ protected function setUp(): void {
   * Tests id.
   */
  public function testId(): void {
    $this->setUpEntity(0, 0, 0);
    $this->assertSame($this->values['id'], $this->entity->id());
  }

@@ -145,6 +158,7 @@ public function testId(): void {
   * Tests uuid.
   */
  public function testUuid(): void {
    $this->setUpEntity(0, 0, 0);
    $this->assertSame($this->values['uuid'], $this->entity->uuid());
  }

@@ -155,6 +169,7 @@ public function testUuid(): void {
   * @legacy-covers ::enforceIsNew
   */
  public function testIsNew(): void {
    $this->setUpEntity(0, 0, 0);
    // We provided an ID, so the entity is not new.
    $this->assertFalse($this->entity->isNew());
    // Force it to be new.
@@ -166,6 +181,7 @@ public function testIsNew(): void {
   * Tests get entity type.
   */
  public function testGetEntityType(): void {
    $this->setUpEntity(0, 1, 0);
    $this->assertSame($this->entityType, $this->entity->getEntityType());
  }

@@ -173,6 +189,7 @@ public function testGetEntityType(): void {
   * Tests bundle.
   */
  public function testBundle(): void {
    $this->setUpEntity(0, 0, 0);
    $this->assertSame($this->entityTypeId, $this->entity->bundle());
  }

@@ -180,6 +197,7 @@ public function testBundle(): void {
   * Tests label.
   */
  public function testLabel(): void {
    $this->setUpEntity(0, 1, 0);
    $property_label = $this->randomMachineName();
    $this->entityType->expects($this->atLeastOnce())
      ->method('getKey')
@@ -205,6 +223,7 @@ public function testLabel(): void {
   * Tests access.
   */
  public function testAccess(): void {
    $this->setUpEntity(0, 0, 0);
    $access = $this->createMock('\Drupal\Core\Entity\EntityAccessControlHandlerInterface');
    $operation = $this->randomMachineName();
    $access->expects($this->once())
@@ -226,6 +245,7 @@ public function testAccess(): void {
   * Tests language.
   */
  public function testLanguage(): void {
    $this->setUpEntity(0, 1, 1);
    $this->entityType
      ->method('getKey')
      ->willReturnMap([
@@ -238,6 +258,7 @@ public function testLanguage(): void {
   * Setup for the tests of the ::load() method.
   */
  public function setupTestLoad(): void {
    $this->setUpEntity(0, 0, 0);
    // Base our test entity on a real entity class so we can test if calling
    // EntityBase::load() on the base class will bubble up to an actual entity.
    $this->entityTypeId = 'stub_entity_base';
@@ -250,6 +271,7 @@ public function setupTestLoad(): void {
   * When called statically on a subclass of Entity.
   */
  public function testLoad(): void {
    $this->setUpEntity(0, 0, 0);
    $this->setupTestLoad();

    $class_name = get_class($this->entity);
@@ -284,6 +306,7 @@ public function testLoad(): void {
   * When called statically on a subclass of Entity.
   */
  public function testLoadMultiple(): void {
    $this->setUpEntity(0, 0, 0);
    $this->setupTestLoad();

    $class_name = get_class($this->entity);
@@ -316,6 +339,7 @@ public function testLoadMultiple(): void {
   * Tests create.
   */
  public function testCreate(): void {
    $this->setUpEntity(0, 0, 0);
    $this->setupTestLoad();

    $class_name = get_class($this->entity);
@@ -348,6 +372,7 @@ public function testCreate(): void {
   * Tests save.
   */
  public function testSave(): void {
    $this->setUpEntity(0, 0, 0);
    $storage = $this->createMock('\Drupal\Core\Entity\EntityStorageInterface');
    $storage->expects($this->once())
      ->method('save')
@@ -365,6 +390,7 @@ public function testSave(): void {
   * Tests delete.
   */
  public function testDelete(): void {
    $this->setUpEntity(0, 0, 0);
    $this->entity->id = $this->randomMachineName();
    $storage = $this->createMock('\Drupal\Core\Entity\EntityStorageInterface');
    // Testing the argument of the delete() method consumes too much memory.
@@ -383,6 +409,7 @@ public function testDelete(): void {
   * Tests get entity type id.
   */
  public function testGetEntityTypeId(): void {
    $this->setUpEntity(0, 0, 0);
    $this->assertSame($this->entityTypeId, $this->entity->getEntityTypeId());
  }

@@ -390,6 +417,7 @@ public function testGetEntityTypeId(): void {
   * Tests pre save.
   */
  public function testPreSave(): void {
    $this->setUpEntity(0, 1, 0);
    // This method is internal, so check for errors on calling it only.
    $storage = $this->createStub(EntityStorageInterface::class);
    // Our mocked entity->preSave() returns NULL, so assert that.
@@ -400,6 +428,7 @@ public function testPreSave(): void {
   * Tests post save.
   */
  public function testPostSave(): void {
    $this->setUpEntity(0, 6, 0);
    // This method is internal, so check for errors on calling it only.
    $storage = $this->createStub(EntityStorageInterface::class);

@@ -422,6 +451,7 @@ public function testPostSave(): void {
   * Tests post save bundle.
   */
  public function testPostSaveBundle(): void {
    $this->setUpEntity(2, 8, 0);
    $this->entityType->expects($this->atLeastOnce())
      ->method('hasKey')
      ->with('bundle')
@@ -452,6 +482,7 @@ public function testPostSaveBundle(): void {
   * Tests pre create.
   */
  public function testPreCreate(): void {
    $this->setUpEntity(0, 0, 0);
    // This method is internal, so check for errors on calling it only.
    $storage = $this->createStub(EntityStorageInterface::class);
    $values = [];
@@ -463,6 +494,7 @@ public function testPreCreate(): void {
   * Tests post create.
   */
  public function testPostCreate(): void {
    $this->setUpEntity(0, 0, 0);
    // This method is internal, so check for errors on calling it only.
    $storage = $this->createStub(EntityStorageInterface::class);
    // Our mocked entity->postCreate() returns NULL, so assert that.
@@ -473,6 +505,7 @@ public function testPostCreate(): void {
   * Tests pre delete.
   */
  public function testPreDelete(): void {
    $this->setUpEntity(0, 0, 0);
    // This method is internal, so check for errors on calling it only.
    $storage = $this->createStub(EntityStorageInterface::class);
    // Our mocked entity->preDelete() returns NULL, so assert that.
@@ -483,6 +516,7 @@ public function testPreDelete(): void {
   * Tests post delete.
   */
  public function testPostDelete(): void {
    $this->setUpEntity(0, 2, 0);
    $storage = $this->createMock('\Drupal\Core\Entity\EntityStorageInterface');
    $storage->expects($this->once())
      ->method('getEntityType')
@@ -501,6 +535,7 @@ public function testPostDelete(): void {
   * Tests post delete bundle.
   */
  public function testPostDeleteBundle(): void {
    $this->setUpEntity(1, 3, 0);
    $this->entityType->expects($this->atLeastOnce())
      ->method('hasKey')
      ->with('bundle')
@@ -526,6 +561,7 @@ public function testPostDeleteBundle(): void {
   * Tests post load.
   */
  public function testPostLoad(): void {
    $this->setUpEntity(0, 0, 0);
    // This method is internal, so check for errors on calling it only.
    $storage = $this->createStub(EntityStorageInterface::class);
    $entities = [$this->entity];
@@ -537,6 +573,7 @@ public function testPostLoad(): void {
   * Tests referenced entities.
   */
  public function testReferencedEntities(): void {
    $this->setUpEntity(0, 0, 0);
    $this->assertSame([], $this->entity->referencedEntities());
  }

@@ -548,6 +585,7 @@ public function testReferencedEntities(): void {
   * @legacy-covers ::addCacheTags
   */
  public function testCacheTags(): void {
    $this->setUpEntity(0, 0, 0);
    // Ensure that both methods return the same by default.
    $this->assertEqualsCanonicalizing([$this->entityTypeId . ':' . 1], $this->entity->getCacheTags());
    $this->assertEqualsCanonicalizing([$this->entityTypeId . ':' . 1], $this->entity->getCacheTagsToInvalidate());
@@ -570,6 +608,7 @@ public function testCacheTags(): void {
   * @legacy-covers ::addCacheContexts
   */
  public function testCacheContexts(): void {
    $this->setUpEntity(0, 0, 0);
    $cache_contexts_manager = $this->createStub(CacheContextsManager::class);
    $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);

@@ -592,6 +631,7 @@ public function testCacheContexts(): void {
   * @legacy-covers ::mergeCacheMaxAge
   */
  public function testCacheMaxAge(): void {
    $this->setUpEntity(0, 0, 0);
    // Cache max age is permanent by default.
    $this->assertEquals(Cache::PERMANENT, $this->entity->getCacheMaxAge());

Loading