diff --git a/src/Core/Entity/EntityFieldManagerTrait.php b/src/Core/Entity/EntityFieldManagerTrait.php new file mode 100644 index 0000000..a9c34a8 --- /dev/null +++ b/src/Core/Entity/EntityFieldManagerTrait.php @@ -0,0 +1,45 @@ +entityFieldManager = $entityFieldManager; + return $this; + } + + /** + * Gets the entity field manager. + * + * @return \Drupal\Core\Entity\EntityFieldManagerInterface + * The entity field manager. + */ + public function getEntityFieldManager() { + if (empty($this->entityFieldManager)) { + $this->entityFieldManager = \Drupal::service('entity_field.manager'); + } + return $this->entityFieldManager; + } + +} diff --git a/tests/src/Core/Entity/EntityFieldManagerTraitTest.php b/tests/src/Core/Entity/EntityFieldManagerTraitTest.php new file mode 100644 index 0000000..6f1a630 --- /dev/null +++ b/tests/src/Core/Entity/EntityFieldManagerTraitTest.php @@ -0,0 +1,73 @@ +mockContainerWithFakeService(['calls' => 1]); + $this->assertsame($service, $this->getEntityFieldManager()); + // Multiple calls should fetch the service from the container only once. + $this->getEntityFieldManager(); + } + + /** + * @covers ::setEntityFieldManager + */ + public function testSetter() { + // Verify the set service is returned. + $this->mockContainerWithFakeService(['calls' => 0]); + $service = $this->prophesize() + ->willImplement(EntityFieldManagerInterface::class) + ->reveal(); + $this->setEntityFieldManager($service); + $this->assertsame($service, $this->getEntityFieldManager()); + } + + /** + * Helper to mock the container with a stub service. + * + * @param int[] $options + * An array with the following keys: + * - calls: The number of calls to get the service the mocked container + * expects. + * + * @return object + * The fake service returned by the container. + */ + protected function mockContainerWithFakeService(array $options) { + $service = new \Stdclass(); + $container = $this->prophesize(Container::class); + $prophecy = $container->get($this->serviceId); + /** @var \Prophecy\Prophecy\MethodProphecy $prophecy */ + $prophecy->shouldBeCalledTimes($options['calls']); + $prophecy->willReturn($service); + \Drupal::setContainer($container->reveal()); + return $service; + } + +} diff --git a/tests/src/Core/Entity/EntityRepositoryTraitTest.php b/tests/src/Core/Entity/EntityRepositoryTraitTest.php index 49fc184..5633a4e 100644 --- a/tests/src/Core/Entity/EntityRepositoryTraitTest.php +++ b/tests/src/Core/Entity/EntityRepositoryTraitTest.php @@ -2,19 +2,19 @@ namespace drunomics\ServiceUtils\Tests\Core\Entity; -use drunomics\ServiceUtils\Core\Entity\EntityTypeManagerTrait; +use drunomics\ServiceUtils\Core\Entity\EntityRepositoryTrait; use Drupal\Core\DependencyInjection\Container; -use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; /** - * @coversDefaultClass \drunomics\ServiceUtils\Core\Entity\EntityTypeManagerTrait + * @coversDefaultClass \drunomics\ServiceUtils\Core\Entity\EntityRepositoryTrait * @group ServiceUtils */ class EntityRepositoryTraitTest extends TestCase { - use EntityTypeManagerTrait; + use EntityRepositoryTrait; use ProphecyTrait; /** @@ -22,30 +22,30 @@ class EntityRepositoryTraitTest extends TestCase { * * @var string */ - protected $serviceId = 'entity_type.manager'; + protected $serviceId = 'entity.repository'; /** - * @covers ::getEntityTypeManager + * @covers ::getEntityRepository */ public function testGetter() { // Verify the container is used once and the right service is returned. $service = $this->mockContainerWithFakeService(['calls' => 1]); - $this->assertsame($service, $this->getEntityTypeManager()); + $this->assertsame($service, $this->getEntityRepository()); // Multiple calls should fetch the service from the container only once. - $this->getEntityTypeManager(); + $this->getEntityRepository(); } /** - * @covers ::setEntityTypeManager + * @covers ::setEntityRepository */ public function testSetter() { // Verify the set service is returned. $this->mockContainerWithFakeService(['calls' => 0]); $service = $this->prophesize() - ->willImplement(EntityTypeManagerInterface::class) + ->willImplement(EntityRepositoryInterface::class) ->reveal(); - $this->setEntityTypeManager($service); - $this->assertsame($service, $this->getEntityTypeManager()); + $this->setEntityRepository($service); + $this->assertsame($service, $this->getEntityRepository()); } /** diff --git a/tests/src/Core/Entity/EntityTypeManagerTraitTest.php b/tests/src/Core/Entity/EntityTypeManagerTraitTest.php index 63cf8c4..c001132 100644 --- a/tests/src/Core/Entity/EntityTypeManagerTraitTest.php +++ b/tests/src/Core/Entity/EntityTypeManagerTraitTest.php @@ -2,19 +2,19 @@ namespace drunomics\ServiceUtils\Tests\Core\Entity; -use drunomics\ServiceUtils\Core\Entity\EntityRepositoryTrait; +use drunomics\ServiceUtils\Core\Entity\EntityTypeManagerTrait; use Drupal\Core\DependencyInjection\Container; -use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; /** - * @coversDefaultClass \drunomics\ServiceUtils\Core\Entity\EntityRepositoryTrait + * @coversDefaultClass \drunomics\ServiceUtils\Core\Entity\EntityTypeManagerTrait * @group ServiceUtils */ class EntityTypeManagerTraitTest extends TestCase { - use EntityRepositoryTrait; + use EntityTypeManagerTrait; use ProphecyTrait; /** @@ -22,30 +22,30 @@ class EntityTypeManagerTraitTest extends TestCase { * * @var string */ - protected $serviceId = 'entity.repository'; + protected $serviceId = 'entity_type.manager'; /** - * @covers ::getEntityRepository + * @covers ::getEntityTypeManager */ public function testGetter() { // Verify the container is used once and the right service is returned. $service = $this->mockContainerWithFakeService(['calls' => 1]); - $this->assertsame($service, $this->getEntityRepository()); + $this->assertsame($service, $this->getEntityTypeManager()); // Multiple calls should fetch the service from the container only once. - $this->getEntityRepository(); + $this->getEntityTypeManager(); } /** - * @covers ::setEntityRepository + * @covers ::setEntityTypeManager */ public function testSetter() { // Verify the set service is returned. $this->mockContainerWithFakeService(['calls' => 0]); $service = $this->prophesize() - ->willImplement(EntityRepositoryInterface::class) + ->willImplement(EntityTypeManagerInterface::class) ->reveal(); - $this->setEntityRepository($service); - $this->assertsame($service, $this->getEntityRepository()); + $this->setEntityTypeManager($service); + $this->assertsame($service, $this->getEntityTypeManager()); } /**