Skip to content

Commit

Permalink
LDP-1321: Add more traits for FileUrlGenerator and DateFormatter (#21)
Browse files Browse the repository at this point in the history
* 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
vasike authored May 4, 2022
1 parent 3c001be commit 4954c47
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Core/Datetime/DateFormatterTrait.php
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;
}

}
32 changes: 32 additions & 0 deletions src/Core/File/FileUrlGeneratorTrait.php
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;
}

}
59 changes: 59 additions & 0 deletions tests/src/Core/Datetime/DateFormatterTraitTest.php
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;
}

}
59 changes: 59 additions & 0 deletions tests/src/Core/File/FileUrlGeneratorTraitTest.php
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;
}

}

0 comments on commit 4954c47

Please sign in to comment.