class UuidResolverTest in Drupal 8
Same name and namespace in other branches
- 9 core/modules/serialization/tests/src/Unit/EntityResolver/UuidResolverTest.php \Drupal\Tests\serialization\Unit\EntityResolver\UuidResolverTest
- 10 core/modules/serialization/tests/src/Unit/EntityResolver/UuidResolverTest.php \Drupal\Tests\serialization\Unit\EntityResolver\UuidResolverTest
@coversDefaultClass \Drupal\serialization\EntityResolver\UuidResolver @group serialization
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\serialization\Unit\EntityResolver\UuidResolverTest
Expanded class hierarchy of UuidResolverTest
File
- core/
modules/ serialization/ tests/ src/ Unit/ EntityResolver/ UuidResolverTest.php, line 13
Namespace
Drupal\Tests\serialization\Unit\EntityResolverView 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() {
$this->entityRepository = $this
->createMock(EntityRepositoryInterface::class);
$this->resolver = new UuidResolver($this->entityRepository);
}
/**
* Test resolve() with a class using the incorrect interface.
*/
public function testResolveNotInInterface() {
$this->entityRepository
->expects($this
->never())
->method('loadEntityByUuid');
$normalizer = $this
->createMock('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
$this
->assertNull($this->resolver
->resolve($normalizer, [], 'test_type'));
}
/**
* Test resolve() with a class using the correct interface but no UUID.
*/
public function testResolveNoUuid() {
$this->entityRepository
->expects($this
->never())
->method('loadEntityByUuid');
$normalizer = $this
->createMock('Drupal\\serialization\\EntityResolver\\UuidReferenceInterface');
$normalizer
->expects($this
->once())
->method('getUuid')
->with([])
->will($this
->returnValue(NULL));
$this
->assertNull($this->resolver
->resolve($normalizer, [], 'test_type'));
}
/**
* Test resolve() with correct interface but no matching entity for the UUID.
*/
public function testResolveNoEntity() {
$uuid = '392eab92-35c2-4625-872d-a9dab4da008e';
$this->entityRepository
->expects($this
->once())
->method('loadEntityByUuid')
->with('test_type')
->will($this
->returnValue(NULL));
$normalizer = $this
->createMock('Drupal\\serialization\\EntityResolver\\UuidReferenceInterface');
$normalizer
->expects($this
->once())
->method('getUuid')
->with([])
->will($this
->returnValue($uuid));
$this
->assertNull($this->resolver
->resolve($normalizer, [], 'test_type'));
}
/**
* Test resolve() when a UUID corresponds to an entity.
*/
public function testResolveWithEntity() {
$uuid = '392eab92-35c2-4625-872d-a9dab4da008e';
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->once())
->method('id')
->will($this
->returnValue(1));
$this->entityRepository
->expects($this
->once())
->method('loadEntityByUuid')
->with('test_type', $uuid)
->will($this
->returnValue($entity));
$normalizer = $this
->createMock('Drupal\\serialization\\EntityResolver\\UuidReferenceInterface');
$normalizer
->expects($this
->once())
->method('getUuid')
->with([])
->will($this
->returnValue($uuid));
$this
->assertSame(1, $this->resolver
->resolve($normalizer, [], 'test_type'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UuidResolverTest:: |
protected | property | The mock entity repository service. | |
UuidResolverTest:: |
protected | property | The UuidResolver instance. | |
UuidResolverTest:: |
protected | function |
Overrides UnitTestCase:: |
|
UuidResolverTest:: |
public | function | Test resolve() with correct interface but no matching entity for the UUID. | |
UuidResolverTest:: |
public | function | Test resolve() with a class using the incorrect interface. | |
UuidResolverTest:: |
public | function | Test resolve() with a class using the correct interface but no UUID. | |
UuidResolverTest:: |
public | function | Test resolve() when a UUID corresponds to an entity. |