class FacetsDateHandlerTest in Facets 8
Unit test for Date Handler Service.
@group facets
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\facets\Unit\Utility\FacetsDateHandlerTest
Expanded class hierarchy of FacetsDateHandlerTest
File
- tests/
src/ Unit/ Utility/ FacetsDateHandlerTest.php, line 21
Namespace
Drupal\Tests\facets\Unit\UtilityView source
class FacetsDateHandlerTest extends UnitTestCase {
/**
* Timestamp used by tests: Thu, 26 Nov 1987 20:43:04 GMT.
*/
const TIMESTAMP = 564957784;
/**
* ISO date used by tests: Thu, 26 Nov 1987 20:43:04 GMT.
*/
const ISO_DATE = '1987-11-26T20:43:04Z';
/**
* The system under test.
*
* @var \Drupal\facets\Utility\FacetsDateHandler
*/
protected $handler;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$entity_storage = $this
->createMock(EntityStorageInterface::class);
$em = $this
->createMock(EntityTypeManagerInterface::class);
$em
->expects($this
->any())
->method('getStorage')
->with('date_format')
->willReturn($entity_storage);
$language = new Language([
'id' => 'en',
]);
$lm = $this
->createMock(LanguageManagerInterface::class);
$lm
->method('getCurrentLanguage')
->willReturn($language);
$st = $this
->createMock(TranslationInterface::class);
$rs = $this
->createMock(RequestStack::class);
$cf = $this
->getConfigFactoryStub();
$config_factory = $this
->getConfigFactoryStub([
'system.date' => [
'country' => [
'default' => 'GB',
],
],
]);
$container = new ContainerBuilder();
$container
->set('config.factory', $config_factory);
\Drupal::setContainer($container);
$date_formatter = new DateFormatter($em, $lm, $st, $cf, $rs);
$this->handler = new FacetsDateHandler($date_formatter);
}
/**
* Tests the isoDate method.
*
* @dataProvider provideIsoDates
*/
public function testIsoDate($iso_date, $gap) {
$fd = $this->handler;
$this
->assertEquals($iso_date, $fd
->isoDate(static::TIMESTAMP, $gap));
}
/**
* Tests for ::getNextDateGap.
*/
public function testGetNextDateGap() {
$fd = $this->handler;
$gap = $fd
->getNextDateGap($fd::FACETS_DATE_SECOND);
$this
->assertEquals($fd::FACETS_DATE_SECOND, $gap);
$gap = $fd
->getNextDateGap($fd::FACETS_DATE_MINUTE);
$this
->assertEquals($fd::FACETS_DATE_SECOND, $gap);
$gap = $fd
->getNextDateGap($fd::FACETS_DATE_SECOND, $fd::FACETS_DATE_MINUTE);
$this
->assertEquals($fd::FACETS_DATE_MINUTE, $gap);
$gap = $fd
->getNextDateGap($fd::FACETS_DATE_MINUTE, $fd::FACETS_DATE_MINUTE);
$this
->assertEquals($fd::FACETS_DATE_MINUTE, $gap);
$gap = $fd
->getNextDateGap($fd::FACETS_DATE_SECOND, $fd::FACETS_DATE_HOUR);
$this
->assertEquals($fd::FACETS_DATE_HOUR, $gap);
$gap = $fd
->getNextDateGap($fd::FACETS_DATE_MINUTE, $fd::FACETS_DATE_HOUR);
$this
->assertEquals($fd::FACETS_DATE_HOUR, $gap);
$gap = $fd
->getNextDateGap($fd::FACETS_DATE_HOUR, $fd::FACETS_DATE_HOUR);
$this
->assertEquals($fd::FACETS_DATE_HOUR, $gap);
}
/**
* Tests for ::getTimestampGap.
*/
public function testGetTimestampGap() {
$fd = $this->handler;
// The best search gap between two dates must be a year.
$date_gap = $this->handler
->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 31536000);
$this
->assertEquals($fd::FACETS_DATE_YEAR, $date_gap);
// The best search gap between two dates must be a month.
$date_gap = $this->handler
->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 86400 * 60);
$this
->assertEquals($fd::FACETS_DATE_MONTH, $date_gap);
// The best search gap between two dates must be a day.
$date_gap = $this->handler
->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 86400);
$this
->assertEquals($fd::FACETS_DATE_DAY, $date_gap);
// The best search gap between two dates must be an hour.
$date_gap = $this->handler
->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 3600);
$this
->assertEquals($fd::FACETS_DATE_HOUR, $date_gap);
// The best search gap between two dates must be a minute.
$date_gap = $this->handler
->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 60);
$this
->assertEquals($fd::FACETS_DATE_MINUTE, $date_gap);
// The best search gap between two dates must be a second.
$date_gap = $this->handler
->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 59);
$this
->assertEquals($fd::FACETS_DATE_SECOND, $date_gap);
// When passing in a minimum gap it should be respected.
$date_gap = $this->handler
->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 3600, $fd::FACETS_DATE_DAY);
$this
->assertEquals($fd::FACETS_DATE_DAY, $date_gap);
}
/**
* Tests for ::getDateGap method.
*/
public function testGetDateGap() {
$fd = $this->handler;
// Cannot convert to timestamp.
$this
->assertFalse($fd
->getDateGap(static::TIMESTAMP, static::TIMESTAMP));
// The min. gap is MONTH but the result is larger.
$this
->assertEquals($fd::FACETS_DATE_YEAR, $fd
->getDateGap('1983-03-03T20:43:04Z', '1987-11-26T20:43:04Z', $fd::FACETS_DATE_MONTH));
// The gap is YEAR.
$this
->assertEquals($fd::FACETS_DATE_YEAR, $fd
->getDateGap('1983-03-03T20:43:04Z', '1987-11-26T20:43:04Z'));
// The gap is MONTH.
$this
->assertEquals($fd::FACETS_DATE_MONTH, $fd
->getDateGap('1983-03-03T20:43:04Z', '1983-11-26T20:43:04Z'));
// The gap is DAY.
$this
->assertEquals($fd::FACETS_DATE_DAY, $fd
->getDateGap('1983-03-03T20:43:04Z', '1983-03-26T20:43:04Z'));
// The gap is HOUR.
$this
->assertEquals($fd::FACETS_DATE_HOUR, $fd
->getDateGap('1983-03-03T20:43:04Z', '1983-03-03T21:44:04Z'));
// The gap is MINUTE.
$this
->assertEquals($fd::FACETS_DATE_MINUTE, $fd
->getDateGap('1983-03-03T20:43:04Z', '1983-03-03T20:44:04Z'));
// The gap is SECOND.
$this
->assertEquals($fd::FACETS_DATE_SECOND, $fd
->getDateGap('1983-03-03T20:43:04Z', '1983-03-03T20:43:55Z'));
}
/**
* Tests for ::nextDateIncrement method.
*
* @dataProvider provideNextDateIncrementData
*/
public function testNextDateIncrement($incremented_iso_date, $gap) {
$this
->assertEquals($incremented_iso_date, $this->handler
->getNextDateIncrement(static::ISO_DATE, $gap));
}
/**
* Tests for ::nextDateIncrement method.
*/
public function testInvalidNextDateIncrement() {
$this
->assertFalse($this->handler
->getNextDateIncrement('foo', FacetsDateHandler::FACETS_DATE_SECOND));
}
/**
* Tests for ::gapCompare method.
*/
public function testGapCompare() {
$fd = $this->handler;
// Timestamps are equals.
$this
->assertEquals(0, $fd
->gapCompare(static::TIMESTAMP, static::TIMESTAMP));
// Timestamps are equals.
$this
->assertEquals(0, $fd
->gapCompare($fd::FACETS_DATE_YEAR, $fd::FACETS_DATE_YEAR));
// gap1 is less than gap2.
$this
->assertEquals(-1, $fd
->gapCompare($fd::FACETS_DATE_MONTH, $fd::FACETS_DATE_YEAR));
// gap1 is less than gap2.
$this
->assertEquals(1, $fd
->gapCompare($fd::FACETS_DATE_MONTH, $fd::FACETS_DATE_DAY));
}
/**
* Tests for ::formatTimestamp method.
*/
public function testFormatTimestamp() {
$fd = $this->handler;
$formatted = $fd
->formatTimestamp(static::TIMESTAMP);
$this
->assertEquals('1987', $formatted);
$formatted = $fd
->formatTimestamp(static::TIMESTAMP, 'llama');
$this
->assertEquals('1987', $formatted);
$formatted = $fd
->formatTimestamp(static::TIMESTAMP, $fd::FACETS_DATE_YEAR);
$this
->assertEquals('1987', $formatted);
}
/**
* Test extract items.
*/
public function testExtractActiveItems() {
$this
->assertFalse($this->handler
->extractActiveItems('foo'));
$range = '[2016-03-01T00:00:00Z TO 2016-04-01T00:00:00Z]';
$extracted = $this->handler
->extractActiveItems($range);
$this
->assertSame('array', gettype($extracted));
$this
->assertEquals('1456790400', $extracted['start']['timestamp']);
$this
->assertEquals('2016-03-01T00:00:00Z', $extracted['start']['iso']);
}
/**
* Returns a data provider for the ::testIsoDate().
*
* @return array
* Arrays with data for the test data.
*/
public function provideIsoDates() {
return [
[
'1987-11-26T20:43:04Z',
FacetsDateHandler::FACETS_DATE_SECOND,
],
[
'1987-11-26T20:43:00Z',
FacetsDateHandler::FACETS_DATE_MINUTE,
],
[
'1987-11-26T20:00:00Z',
FacetsDateHandler::FACETS_DATE_HOUR,
],
[
'1987-11-26T00:00:00Z',
FacetsDateHandler::FACETS_DATE_DAY,
],
[
'1987-11-01T00:00:00Z',
FacetsDateHandler::FACETS_DATE_MONTH,
],
[
'1987-01-01T00:00:00Z',
FacetsDateHandler::FACETS_DATE_YEAR,
],
[
'1987-11-26T20:43:04Z',
FacetsDateHandler::FACETS_DATE_ISO8601,
],
];
}
/**
* Returns a data provider for the ::testNextDateIncrement().
*
* @return array
* Arrays with data for the test data.
*/
public function provideNextDateIncrementData() {
return [
[
'1987-11-26T20:43:05Z',
FacetsDateHandler::FACETS_DATE_SECOND,
],
[
'1987-11-26T20:44:04Z',
FacetsDateHandler::FACETS_DATE_MINUTE,
],
[
'1987-11-26T21:43:04Z',
FacetsDateHandler::FACETS_DATE_HOUR,
],
[
'1987-11-27T20:43:04Z',
FacetsDateHandler::FACETS_DATE_DAY,
],
[
'1987-12-26T20:43:04Z',
FacetsDateHandler::FACETS_DATE_MONTH,
],
[
'1988-11-26T20:43:04Z',
FacetsDateHandler::FACETS_DATE_YEAR,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FacetsDateHandlerTest:: |
protected | property | The system under test. | |
FacetsDateHandlerTest:: |
constant | ISO date used by tests: Thu, 26 Nov 1987 20:43:04 GMT. | ||
FacetsDateHandlerTest:: |
public | function | Returns a data provider for the ::testIsoDate(). | |
FacetsDateHandlerTest:: |
public | function | Returns a data provider for the ::testNextDateIncrement(). | |
FacetsDateHandlerTest:: |
public | function |
Overrides UnitTestCase:: |
|
FacetsDateHandlerTest:: |
public | function | Test extract items. | |
FacetsDateHandlerTest:: |
public | function | Tests for ::formatTimestamp method. | |
FacetsDateHandlerTest:: |
public | function | Tests for ::gapCompare method. | |
FacetsDateHandlerTest:: |
public | function | Tests for ::getDateGap method. | |
FacetsDateHandlerTest:: |
public | function | Tests for ::getNextDateGap. | |
FacetsDateHandlerTest:: |
public | function | Tests for ::getTimestampGap. | |
FacetsDateHandlerTest:: |
public | function | Tests for ::nextDateIncrement method. | |
FacetsDateHandlerTest:: |
public | function | Tests the isoDate method. | |
FacetsDateHandlerTest:: |
public | function | Tests for ::nextDateIncrement method. | |
FacetsDateHandlerTest:: |
constant | Timestamp used by tests: Thu, 26 Nov 1987 20:43:04 GMT. | ||
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |