Commit 19d08d31 authored by catch's avatar catch
Browse files

Issue #3351379 by Spokje, mondrake, longwave: Only mock methods defined in interfaces

parent 4104aed9
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -47,9 +47,9 @@ class ConfigNamesMapperTest extends UnitTestCase {
  protected $localeConfigManager;

  /**
   * The locale configuration manager.
   * The typed configuration manager.
   *
   * @var \Drupal\locale\LocaleConfigManager|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $typedConfigManager;

+15 −35
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\Tests\help_topics\Unit;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\help_topics\HelpTopicTwigLoader;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
@@ -36,10 +38,19 @@ protected function setUp(): void {
    parent::setUp();

    $this->setUpVfs();
    $this->helpLoader = new HelpTopicTwigLoader('\fake\root\path',
      $this->getHandlerMock('module'),
      $this->getHandlerMock('theme')
    );

    $module_handler = $this->createMock(ModuleHandlerInterface::class);
    $module_handler
      ->method('getModuleDirectories')
      ->willReturn($this->directories['module']);

    /** @var \Drupal\Core\Extension\ThemeHandlerInterface|\Prophecy\Prophecy\ObjectProphecy $module_handler */
    $theme_handler = $this->createMock(ThemeHandlerInterface::class);
    $theme_handler
      ->method('getThemeDirectories')
      ->willReturn($this->directories['theme']);

    $this->helpLoader = new HelpTopicTwigLoader('\fake\root\path', $module_handler, $theme_handler);
  }

  /**
@@ -72,37 +83,6 @@ public function testGetSourceContextException() {
    $this->helpLoader->getSourceContext('@' . HelpTopicTwigLoader::MAIN_NAMESPACE . '/test.invalid_yaml.html.twig');
  }

  /**
   * Creates a mock module or theme handler class for the test.
   *
   * @param string $type
   *   Type of handler to return: 'module' or 'theme'.
   *
   * @return \PHPUnit\Framework\MockObject\MockObject
   *   The mock of module or theme handler.
   */
  protected function getHandlerMock($type) {
    if ($type == 'module') {
      $class = 'Drupal\Core\Extension\ModuleHandlerInterface';
      $method = 'getModuleDirectories';
    }
    else {
      $class = 'Drupal\Core\Extension\ThemeHandlerInterface';
      $method = 'getThemeDirectories';
    }

    $handler = $this
      ->getMockBuilder($class)
      ->disableOriginalConstructor()
      ->getMock();

    $handler
      ->method($method)
      ->willReturn($this->directories[$type]);

    return $handler;
  }

  /**
   * Sets up the virtual file system.
   */
+10 −14
Original line number Diff line number Diff line
@@ -5,11 +5,14 @@
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\serialization\Normalizer\ContentEntityNormalizer;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\Serializer\Serializer;

/**
 * @coversDefaultClass \Drupal\serialization\Normalizer\ContentEntityNormalizer
@@ -20,7 +23,7 @@ class ContentEntityNormalizerTest extends UnitTestCase {
  /**
   * The mock serializer.
   *
   * @var \Symfony\Component\Serializer\SerializerInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Symfony\Component\Serializer\Serializer|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $serializer;

@@ -43,11 +46,8 @@ protected function setUp(): void {

    $this->contentEntityNormalizer = new ContentEntityNormalizer($entity_type_manager, $entity_type_repository, $entity_field_manager);

    $this->serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')
      ->disableOriginalConstructor()
      ->onlyMethods(['normalize'])
      ->getMock();
    $this->contentEntityNormalizer->setSerializer($this->serializer);
    $this->serializer = $this->prophesize(Serializer::class);
    $this->contentEntityNormalizer->setSerializer($this->serializer->reveal());
  }

  /**
@@ -66,10 +66,8 @@ public function testSupportsNormalization() {
   * @covers ::normalize
   */
  public function testNormalize() {
    $this->serializer->expects($this->any())
      ->method('normalize')
      ->with($this->containsOnlyInstancesOf('Drupal\Core\Field\FieldItemListInterface'), 'test_format', ['account' => NULL])
      ->willReturn('test');
    $this->serializer->normalize(Argument::type(FieldItemListInterface::class),
      'test_format', ['account' => NULL])->willReturn('test');

    $definitions = [
      'field_accessible_external' => $this->createMockFieldListItem(TRUE, FALSE),
@@ -100,10 +98,8 @@ public function testNormalizeWithAccountContext() {
      'account' => $mock_account,
    ];

    $this->serializer->expects($this->any())
      ->method('normalize')
      ->with($this->containsOnlyInstancesOf('Drupal\Core\Field\FieldItemListInterface'), 'test_format', $context)
      ->willReturn('test');
    $this->serializer->normalize(Argument::type(FieldItemListInterface::class),
      'test_format', $context)->willReturn('test');

    // The mock account should get passed directly into the access() method on
    // field items from $context['account'].
+2 −2
Original line number Diff line number Diff line
@@ -28,9 +28,9 @@ class RouteSubscriberTest extends UnitTestCase {
  protected $entityTypeManager;

  /**
   * The mocked view storage.
   * The mocked config entity storage.
   *
   * @var \Drupal\Tests\views\Kernel\ViewStorageTest|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $viewStorage;

+2 −2
Original line number Diff line number Diff line
@@ -84,9 +84,9 @@ class ViewExecutableTest extends UnitTestCase {
  protected $viewsData;

  /**
   * The mocked display handler.
   * The mocked display router handler.
   *
   * @var \Drupal\views\Plugin\views\display\DisplayPluginInterface|\PHPUnit\Framework\MockObject\MockObject
   * @var \Drupal\views\Plugin\views\display\DisplayRouterInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $displayHandler;

Loading