Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: INFOPORTAL-5939: Add EntityFieldManagerTrait #27

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Core/Entity/EntityFieldManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace drunomics\ServiceUtils\Core\Entity;

use Drupal\Core\Entity\EntityFieldManagerInterface;

/**
* Allows setter injection and simple usage of the service.
*/
trait EntityFieldManagerTrait {

/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;

/**
* Sets the entity field manager.
*
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* The entity field manager.
*
* @return $this
*/
public function setEntityFieldManager(EntityFieldManagerInterface $entityFieldManager) {
$this->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;
}

}
73 changes: 73 additions & 0 deletions tests/src/Core/Entity/EntityFieldManagerTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace drunomics\ServiceUtils\Tests\Core\Entity;

use drunomics\ServiceUtils\Core\Entity\EntityFieldManagerTrait;
use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/**
* @coversDefaultClass \drunomics\ServiceUtils\Core\Entity\EntityFieldManagerTrait
* @group ServiceUtils
*/
class EntityFieldManagerTraitTest extends TestCase {

use EntityFieldManagerTrait;
use ProphecyTrait;

/**
* The id of the trait's service.
*
* @var string
*/
protected $serviceId = 'entity_field.manager';

/**
* @covers ::getEntityFieldManager
*/
public function testGetter() {
// Verify the container is used once and the right service is returned.
$service = $this->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;
}

}
24 changes: 12 additions & 12 deletions tests/src/Core/Entity/EntityRepositoryTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,50 @@

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;

/**
* The id of the trait's service.
*
* @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());
}

/**
Expand Down
24 changes: 12 additions & 12 deletions tests/src/Core/Entity/EntityTypeManagerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,50 @@

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;

/**
* The id of the trait's service.
*
* @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());
}

/**
Expand Down