-
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.
LDP-1321: Add more traits for FileUrlGenerator and DateFormatter (#21)
* LDP-1321: Add DateFormatterTrait. * LDP-1321: Add FileUrlGeneratorTrait. * LDP-1321: CleanUp. * LDP-1321: Add DateFormatterTrait 2. * LDP-1321: Add Tests. * LDP-1321: Add Tests 2.
- Loading branch information
Showing
4 changed files
with
182 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,32 @@ | ||
<?php | ||
|
||
namespace drunomics\ServiceUtils\Core\Datetime; | ||
|
||
/** | ||
* Allows setter injection and simple usage of the service. | ||
*/ | ||
trait DateFormatterTrait { | ||
|
||
/** | ||
* Date formatter. | ||
* | ||
* @var \Drupal\Core\Datetime\DateFormatter | ||
*/ | ||
protected $dateFormatter; | ||
|
||
/** | ||
* Gets the date formatter. | ||
* | ||
* @todo Add trait for proper dependency injection. | ||
* | ||
* @return \Drupal\Core\Datetime\DateFormatter | ||
* The date formatter. | ||
*/ | ||
public function getDateFormatter() { | ||
if (!isset($this->dateFormatter)) { | ||
$this->dateFormatter = \Drupal::service('date.formatter'); | ||
} | ||
return $this->dateFormatter; | ||
} | ||
|
||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace drunomics\ServiceUtils\Core\File; | ||
|
||
/** | ||
* Allows setter injection and simple usage of the service. | ||
*/ | ||
trait FileUrlGeneratorTrait { | ||
|
||
/** | ||
* The file URL generator. | ||
* | ||
* @var \Drupal\Core\File\FileUrlGeneratorInterface | ||
*/ | ||
protected $fileUrlGenerator; | ||
|
||
/** | ||
* Returns the file URL generator. | ||
* | ||
* This is provided for BC as sub-classes may not call the parent constructor. | ||
* | ||
* @return \Drupal\Core\File\FileUrlGeneratorInterface | ||
* The file URL generator. | ||
*/ | ||
public function getFileUrlGenerator() { | ||
if (!$this->fileUrlGenerator) { | ||
$this->fileUrlGenerator = \Drupal::service('file_url_generator'); | ||
} | ||
return $this->fileUrlGenerator; | ||
} | ||
|
||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace drunomics\ServiceUtils\Tests\Core\Datetime; | ||
|
||
use drunomics\ServiceUtils\Core\Datetime\DateFormatterTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Drupal\Core\DependencyInjection\Container; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** | ||
* @coversDefaultClass \drunomics\ServiceUtils\Core\Datetime\DateFormatterTrait | ||
* @group ServiceUtils | ||
*/ | ||
class DateFormatterTraitTest extends TestCase { | ||
|
||
use DateFormatterTrait; | ||
use ProphecyTrait; | ||
|
||
/** | ||
* The id of the trait's service. | ||
* | ||
* @var string | ||
*/ | ||
protected $serviceId = 'date.formatter'; | ||
|
||
/** | ||
* @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->getDateFormatter()); | ||
// Multiple calls should fetch the service from the container only once. | ||
$this->getDateFormatter(); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace drunomics\ServiceUtils\Tests\Core\File; | ||
|
||
use drunomics\ServiceUtils\Core\File\FileUrlGeneratorTrait; | ||
use Drupal\Core\DependencyInjection\Container; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** | ||
* @coversDefaultClass \drunomics\ServiceUtils\Core\File\FileUrlGeneratorTrait | ||
* @group ServiceUtils | ||
*/ | ||
class FileUrlGeneratorTraitTest extends TestCase { | ||
|
||
use FileUrlGeneratorTrait; | ||
use ProphecyTrait; | ||
|
||
/** | ||
* The id of the trait's service. | ||
* | ||
* @var string | ||
*/ | ||
protected $serviceId = 'file_url_generator'; | ||
|
||
/** | ||
* @covers ::getFileSystem | ||
*/ | ||
public function testGetter() { | ||
// Verify the container is used once and the right service is returned. | ||
$service = $this->mockContainerWithFakeService(['calls' => 1]); | ||
$this->assertsame($service, $this->getFileUrlGenerator()); | ||
// Multiple calls should fetch the service from the container only once. | ||
$this->getFileUrlGenerator(); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
} |