Skip to content

Commit 4954c47

Browse files
authored
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.
1 parent 3c001be commit 4954c47

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace drunomics\ServiceUtils\Core\Datetime;
4+
5+
/**
6+
* Allows setter injection and simple usage of the service.
7+
*/
8+
trait DateFormatterTrait {
9+
10+
/**
11+
* Date formatter.
12+
*
13+
* @var \Drupal\Core\Datetime\DateFormatter
14+
*/
15+
protected $dateFormatter;
16+
17+
/**
18+
* Gets the date formatter.
19+
*
20+
* @todo Add trait for proper dependency injection.
21+
*
22+
* @return \Drupal\Core\Datetime\DateFormatter
23+
* The date formatter.
24+
*/
25+
public function getDateFormatter() {
26+
if (!isset($this->dateFormatter)) {
27+
$this->dateFormatter = \Drupal::service('date.formatter');
28+
}
29+
return $this->dateFormatter;
30+
}
31+
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace drunomics\ServiceUtils\Core\File;
4+
5+
/**
6+
* Allows setter injection and simple usage of the service.
7+
*/
8+
trait FileUrlGeneratorTrait {
9+
10+
/**
11+
* The file URL generator.
12+
*
13+
* @var \Drupal\Core\File\FileUrlGeneratorInterface
14+
*/
15+
protected $fileUrlGenerator;
16+
17+
/**
18+
* Returns the file URL generator.
19+
*
20+
* This is provided for BC as sub-classes may not call the parent constructor.
21+
*
22+
* @return \Drupal\Core\File\FileUrlGeneratorInterface
23+
* The file URL generator.
24+
*/
25+
public function getFileUrlGenerator() {
26+
if (!$this->fileUrlGenerator) {
27+
$this->fileUrlGenerator = \Drupal::service('file_url_generator');
28+
}
29+
return $this->fileUrlGenerator;
30+
}
31+
32+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace drunomics\ServiceUtils\Tests\Core\Datetime;
4+
5+
use drunomics\ServiceUtils\Core\Datetime\DateFormatterTrait;
6+
use PHPUnit\Framework\TestCase;
7+
use Drupal\Core\DependencyInjection\Container;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
10+
/**
11+
* @coversDefaultClass \drunomics\ServiceUtils\Core\Datetime\DateFormatterTrait
12+
* @group ServiceUtils
13+
*/
14+
class DateFormatterTraitTest extends TestCase {
15+
16+
use DateFormatterTrait;
17+
use ProphecyTrait;
18+
19+
/**
20+
* The id of the trait's service.
21+
*
22+
* @var string
23+
*/
24+
protected $serviceId = 'date.formatter';
25+
26+
/**
27+
* @covers ::getModuleHandler
28+
*/
29+
public function testGetter() {
30+
// Verify the container is used once and the right service is returned.
31+
$service = $this->mockContainerWithFakeService(['calls' => 1]);
32+
$this->assertsame($service, $this->getDateFormatter());
33+
// Multiple calls should fetch the service from the container only once.
34+
$this->getDateFormatter();
35+
}
36+
37+
/**
38+
* Helper to mock the container with a stub service.
39+
*
40+
* @param int[] $options
41+
* An array with the following keys:
42+
* - calls: The number of calls to get the service the mocked container
43+
* expects.
44+
*
45+
* @return object
46+
* The fake service returned by the container.
47+
*/
48+
protected function mockContainerWithFakeService(array $options) {
49+
$service = new \Stdclass();
50+
$container = $this->prophesize(Container::class);
51+
$prophecy = $container->get($this->serviceId);
52+
/** @var \Prophecy\Prophecy\MethodProphecy $prophecy */
53+
$prophecy->shouldBeCalledTimes($options['calls']);
54+
$prophecy->willReturn($service);
55+
\Drupal::setContainer($container->reveal());
56+
return $service;
57+
}
58+
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace drunomics\ServiceUtils\Tests\Core\File;
4+
5+
use drunomics\ServiceUtils\Core\File\FileUrlGeneratorTrait;
6+
use Drupal\Core\DependencyInjection\Container;
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
10+
/**
11+
* @coversDefaultClass \drunomics\ServiceUtils\Core\File\FileUrlGeneratorTrait
12+
* @group ServiceUtils
13+
*/
14+
class FileUrlGeneratorTraitTest extends TestCase {
15+
16+
use FileUrlGeneratorTrait;
17+
use ProphecyTrait;
18+
19+
/**
20+
* The id of the trait's service.
21+
*
22+
* @var string
23+
*/
24+
protected $serviceId = 'file_url_generator';
25+
26+
/**
27+
* @covers ::getFileSystem
28+
*/
29+
public function testGetter() {
30+
// Verify the container is used once and the right service is returned.
31+
$service = $this->mockContainerWithFakeService(['calls' => 1]);
32+
$this->assertsame($service, $this->getFileUrlGenerator());
33+
// Multiple calls should fetch the service from the container only once.
34+
$this->getFileUrlGenerator();
35+
}
36+
37+
/**
38+
* Helper to mock the container with a stub service.
39+
*
40+
* @param int[] $options
41+
* An array with the following keys:
42+
* - calls: The number of calls to get the service the mocked container
43+
* expects.
44+
*
45+
* @return object
46+
* The fake service returned by the container.
47+
*/
48+
protected function mockContainerWithFakeService(array $options) {
49+
$service = new \Stdclass();
50+
$container = $this->prophesize(Container::class);
51+
$prophecy = $container->get($this->serviceId);
52+
/** @var \Prophecy\Prophecy\MethodProphecy $prophecy */
53+
$prophecy->shouldBeCalledTimes($options['calls']);
54+
$prophecy->willReturn($service);
55+
\Drupal::setContainer($container->reveal());
56+
return $service;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)