class UuidResolverTest

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

@coversDefaultClass \Drupal\serialization\EntityResolver\UuidResolver
@group serialization

Hierarchy

Expanded class hierarchy of UuidResolverTest

File

core/modules/serialization/tests/src/Unit/EntityResolver/UuidResolverTest.php, line 15

Namespace

Drupal\Tests\serialization\Unit\EntityResolver
View source
class UuidResolverTest extends UnitTestCase {
  
  /**
   * The UuidResolver instance.
   *
   * @var \Drupal\serialization\EntityResolver\UuidResolver
   */
  protected $resolver;
  
  /**
   * The mock entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityRepository;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityRepository = $this->createMock(EntityRepositoryInterface::class);
    $this->resolver = new UuidResolver($this->entityRepository);
  }
  
  /**
   * Tests resolve() with a class using the incorrect interface.
   */
  public function testResolveNotInInterface() : void {
    $this->entityRepository
      ->expects($this->never())
      ->method('loadEntityByUuid');
    $normalizer = $this->createMock('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
    $this->assertNull($this->resolver
      ->resolve($normalizer, [], 'test_type'));
  }
  
  /**
   * Tests resolve() with a class using the correct interface but no UUID.
   */
  public function testResolveNoUuid() : void {
    $this->entityRepository
      ->expects($this->never())
      ->method('loadEntityByUuid');
    $normalizer = $this->createMock('Drupal\\serialization\\EntityResolver\\UuidReferenceInterface');
    $normalizer->expects($this->once())
      ->method('getUuid')
      ->with([])
      ->willReturn(NULL);
    $this->assertNull($this->resolver
      ->resolve($normalizer, [], 'test_type'));
  }
  
  /**
   * Tests resolve() with correct interface but no matching entity for the UUID.
   */
  public function testResolveNoEntity() : void {
    $uuid = '392eab92-35c2-4625-872d-a9dab4da008e';
    $this->entityRepository
      ->expects($this->once())
      ->method('loadEntityByUuid')
      ->with('test_type')
      ->willReturn(NULL);
    $normalizer = $this->createMock('Drupal\\serialization\\EntityResolver\\UuidReferenceInterface');
    $normalizer->expects($this->once())
      ->method('getUuid')
      ->with([])
      ->willReturn($uuid);
    $this->assertNull($this->resolver
      ->resolve($normalizer, [], 'test_type'));
  }
  
  /**
   * Tests resolve() when a UUID corresponds to an entity.
   */
  public function testResolveWithEntity() : void {
    $uuid = '392eab92-35c2-4625-872d-a9dab4da008e';
    $entity = $this->createMock('Drupal\\Core\\Entity\\EntityInterface');
    $entity->expects($this->once())
      ->method('id')
      ->willReturn(1);
    $this->entityRepository
      ->expects($this->once())
      ->method('loadEntityByUuid')
      ->with('test_type', $uuid)
      ->willReturn($entity);
    $normalizer = $this->createMock('Drupal\\serialization\\EntityResolver\\UuidReferenceInterface');
    $normalizer->expects($this->once())
      ->method('getUuid')
      ->with([])
      ->willReturn($uuid);
    $this->assertSame(1, $this->resolver
      ->resolve($normalizer, [], 'test_type'));
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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.
UnitTestCase::$root protected property The app root.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UuidResolverTest::$entityRepository protected property The mock entity repository service.
UuidResolverTest::$resolver protected property The UuidResolver instance.
UuidResolverTest::setUp protected function Overrides UnitTestCase::setUp
UuidResolverTest::testResolveNoEntity public function Tests resolve() with correct interface but no matching entity for the UUID.
UuidResolverTest::testResolveNotInInterface public function Tests resolve() with a class using the incorrect interface.
UuidResolverTest::testResolveNoUuid public function Tests resolve() with a class using the correct interface but no UUID.
UuidResolverTest::testResolveWithEntity public function Tests resolve() when a UUID corresponds to an entity.

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