class ChainEntityResolverTest

Same name and namespace in other branches
  1. 9 core/modules/serialization/tests/src/Unit/EntityResolver/ChainEntityResolverTest.php \Drupal\Tests\serialization\Unit\EntityResolver\ChainEntityResolverTest
  2. 8.9.x core/modules/serialization/tests/src/Unit/EntityResolver/ChainEntityResolverTest.php \Drupal\Tests\serialization\Unit\EntityResolver\ChainEntityResolverTest
  3. 11.x core/modules/serialization/tests/src/Unit/EntityResolver/ChainEntityResolverTest.php \Drupal\Tests\serialization\Unit\EntityResolver\ChainEntityResolverTest

@coversDefaultClass \Drupal\serialization\EntityResolver\ChainEntityResolver
@group serialization

Hierarchy

Expanded class hierarchy of ChainEntityResolverTest

File

core/modules/serialization/tests/src/Unit/EntityResolver/ChainEntityResolverTest.php, line 14

Namespace

Drupal\Tests\serialization\Unit\EntityResolver
View source
class ChainEntityResolverTest extends UnitTestCase {
  
  /**
   * A mocked normalizer.
   *
   * @var \Symfony\Component\Serializer\Normalizer\NormalizerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $testNormalizer;
  
  /**
   * Test data passed to the resolve method.
   *
   * @var object
   */
  protected $testData;
  
  /**
   * A test entity type.
   *
   * @var string
   */
  protected $testEntityType = 'test_type';
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->testNormalizer = $this->createMock('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
    $this->testData = new \stdClass();
  }
  
  /**
   * Tests the resolve method with no matching resolvers.
   *
   * @covers ::__construct
   * @covers ::resolve
   */
  public function testResolverWithNoneResolved() : void {
    $resolvers = [
      $this->createEntityResolverMock(),
      $this->createEntityResolverMock(),
    ];
    $resolver = new ChainEntityResolver($resolvers);
    $this->assertNull($resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }
  
  /**
   * Tests the resolve method with no matching resolvers, using addResolver.
   *
   * @covers ::addResolver
   * @covers ::resolve
   */
  public function testResolverWithNoneResolvedUsingAddResolver() : void {
    $resolver = new ChainEntityResolver();
    $resolver->addResolver($this->createEntityResolverMock());
    $resolver->addResolver($this->createEntityResolverMock());
    $this->assertNull($resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }
  
  /**
   * Tests the resolve method with a matching resolver first.
   *
   * @covers ::__construct
   * @covers ::resolve
   */
  public function testResolverWithFirstResolved() : void {
    $resolvers = [
      $this->createEntityResolverMock(10),
      $this->createEntityResolverMock(NULL, FALSE),
    ];
    $resolver = new ChainEntityResolver($resolvers);
    $this->assertSame(10, $resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }
  
  /**
   * Tests the resolve method with a matching resolver last.
   *
   * @covers ::__construct
   * @covers ::resolve
   */
  public function testResolverWithLastResolved() : void {
    $resolvers = [
      $this->createEntityResolverMock(),
      $this->createEntityResolverMock(10),
    ];
    $resolver = new ChainEntityResolver($resolvers);
    $this->assertSame(10, $resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }
  
  /**
   * Tests the resolve method where one resolver returns 0.
   *
   * @covers ::__construct
   * @covers ::resolve
   */
  public function testResolverWithResolvedToZero() : void {
    $resolvers = [
      $this->createEntityResolverMock(0),
      $this->createEntityResolverMock(NULL, FALSE),
    ];
    $resolver = new ChainEntityResolver($resolvers);
    $this->assertSame(0, $resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }
  
  /**
   * Creates a mock entity resolver.
   *
   * @param null|int $return
   *   Whether the mocked resolve method should return TRUE or FALSE.
   * @param bool $called
   *   Whether or not the resolve method is expected to be called.
   *
   * @return \Drupal\serialization\EntityResolver\EntityResolverInterface|\PHPUnit\Framework\MockObject\MockObject
   *   The mocked entity resolver.
   */
  protected function createEntityResolverMock($return = NULL, $called = TRUE) {
    $mock = $this->createMock('Drupal\\serialization\\EntityResolver\\EntityResolverInterface');
    if ($called) {
      $mock->expects($this->once())
        ->method('resolve')
        ->with($this->testNormalizer, $this->testData, $this->testEntityType)
        ->willReturn($return);
    }
    else {
      $mock->expects($this->never())
        ->method('resolve');
    }
    return $mock;
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
ChainEntityResolverTest::$testData protected property Test data passed to the resolve method.
ChainEntityResolverTest::$testEntityType protected property A test entity type.
ChainEntityResolverTest::$testNormalizer protected property A mocked normalizer.
ChainEntityResolverTest::createEntityResolverMock protected function Creates a mock entity resolver.
ChainEntityResolverTest::setUp protected function Overrides UnitTestCase::setUp
ChainEntityResolverTest::testResolverWithFirstResolved public function Tests the resolve method with a matching resolver first.
ChainEntityResolverTest::testResolverWithLastResolved public function Tests the resolve method with a matching resolver last.
ChainEntityResolverTest::testResolverWithNoneResolved public function Tests the resolve method with no matching resolvers.
ChainEntityResolverTest::testResolverWithNoneResolvedUsingAddResolver public function Tests the resolve method with no matching resolvers, using addResolver.
ChainEntityResolverTest::testResolverWithResolvedToZero public function Tests the resolve method where one resolver returns 0.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.