-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from drunomics/feature/ODT-1634
WV-4745: Add mudule_hander service trait.
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace drunomics\ServiceUtils\Core\Extension; | ||
|
||
use Drupal\Core\Extension\ModuleHandlerInterface; | ||
|
||
/** | ||
* Allows setter injection and simple usage of the service. | ||
*/ | ||
trait ModuleHandlerTrait { | ||
|
||
/** | ||
* The module handler. | ||
* | ||
* @var \Drupal\Core\Extension\ModuleHandlerInterface | ||
*/ | ||
protected $moduleHandler; | ||
|
||
/** | ||
* Sets the module handler. | ||
* | ||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler | ||
* The module handler. | ||
* | ||
* @return $this | ||
*/ | ||
public function setModuleHandler(ModuleHandlerInterface $moduleHandler) { | ||
$this->moduleHandler = $moduleHandler; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the module handler. | ||
* | ||
* @return \Drupal\Core\Extension\ModuleHandlerInterface | ||
* The module handler. | ||
*/ | ||
public function getModuleHandler() { | ||
if (empty($this->moduleHandler)) { | ||
$this->moduleHandler = \Drupal::service('module_handler'); | ||
} | ||
return $this->moduleHandler; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace drunomics\ServiceUtils\Tests\Core\Extension; | ||
|
||
use drunomics\ServiceUtils\Core\Extension\ModuleHandlerTrait; | ||
use Drupal\Core\DependencyInjection\Container; | ||
use Drupal\Core\Extension\ModuleHandlerInterface; | ||
|
||
/** | ||
* @coversDefaultClass \drunomics\ServiceUtils\Core\Extension\ModuleHandlerTrait | ||
* @group ServiceUtils | ||
*/ | ||
class ModuleHandlerTraitTest extends \PHPUnit_Framework_TestCase { | ||
|
||
use ModuleHandlerTrait; | ||
|
||
/** | ||
* The id of the trait's service. | ||
* | ||
* @var string | ||
*/ | ||
protected $serviceId = 'module_handler'; | ||
|
||
/** | ||
* @covers ::getModuleHandler | ||
*/ | ||
public function testGetter() { | ||
// Verify the container is used once and the right service is returned. | ||
$service = $this->mockContainerWithFakeService(['calls' => 1]); | ||
$this->assertsame($service, $this->getModuleHandler()); | ||
// Multiple calls should fetch the service from the container only once. | ||
$this->getModuleHandler(); | ||
} | ||
|
||
/** | ||
* @covers ::setModuleHandler | ||
*/ | ||
public function testSetter() { | ||
// Verify the set service is returned. | ||
$this->mockContainerWithFakeService(['calls' => 0]); | ||
$service = $this->prophesize() | ||
->willImplement(ModuleHandlerInterface::class) | ||
->reveal(); | ||
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $service */ | ||
$this->setModuleHandler($service); | ||
$this->assertsame($service, $this->getModuleHandler()); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
} |