-
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 branch 'master' of github.com:heimrichhannot/contao-utils-bundle
- Loading branch information
Showing
8 changed files
with
232 additions
and
18 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
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
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
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
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
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,91 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2018 Heimrich & Hannot GmbH | ||
* | ||
* @author Rico Kaltofen <r.kaltofen@heimrich-hannot.de> | ||
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL | ||
*/ | ||
|
||
namespace HeimrichHannot\UtilsBundle\Tests; | ||
|
||
use Contao\TestCase\ContaoTestCase; | ||
use HeimrichHannot\UtilsBundle\Date\DateUtil; | ||
|
||
class DateUtilTest extends ContaoTestCase | ||
{ | ||
/** | ||
* Tests the object instantiation. | ||
*/ | ||
public function testCanBeInstantiated() | ||
{ | ||
$instance = new DateUtilTest(); | ||
|
||
$this->assertInstanceOf(DateUtilTest::class, $instance); | ||
} | ||
|
||
/** | ||
* @dataProvider phpDateRFC3339Provider | ||
*/ | ||
public function testFormatPhpDateToRFC3339($format, $expected) | ||
{ | ||
$instance = new DateUtil($this->mockContaoFramework()); | ||
|
||
$this->assertEquals($expected, $instance->formatPhpDateToRFC3339($format)); | ||
} | ||
|
||
/** | ||
* @dataProvider transformPhpDateRFC3339Provider | ||
*/ | ||
public function testTransformPhpDateToRFC3339($format, $locale, $date, $expected) | ||
{ | ||
$timezone = 'UTC'; | ||
$calendar = \IntlDateFormatter::GREGORIAN; | ||
$pattern = null; | ||
$dateFormat = \IntlDateFormatter::MEDIUM; // default from Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer | ||
$timeFormat = \IntlDateFormatter::SHORT; // default from Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer | ||
|
||
$utils = new DateUtil($this->mockContaoFramework()); | ||
$rfc3339Format = $utils->formatPhpDateToRFC3339($format); | ||
|
||
$intlDateFormatter = new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $timezone, $calendar, $pattern); | ||
$intlDateFormatter->setPattern($rfc3339Format); | ||
|
||
$this->assertEquals($expected, $intlDateFormatter->format($date)); | ||
} | ||
|
||
/** | ||
* The php to rfc3339 test data provider | ||
*/ | ||
public function phpDateRFC3339Provider() | ||
{ | ||
return [ | ||
['d', 'dd'], | ||
['D', 'E'], | ||
['j', 'd'], | ||
['l', 'EEEE'], | ||
['N', 'd'], | ||
['S', ''], | ||
['w', 'e'], | ||
]; | ||
} | ||
|
||
/** | ||
* The php to rfc3339 test data provider | ||
*/ | ||
public function transformPhpDateRFC3339Provider() | ||
{ | ||
$date = new \DateTime(); | ||
$date->setDate('2018', '02', '04'); | ||
$date->setTime('16', '33', '12', '0'); | ||
|
||
return [ | ||
['d', 'en-EN', $date, '04'], | ||
['D', 'en-EN', $date, 'Sun'], | ||
['j', 'en-EN', $date, '4'], | ||
['N', 'en-EN', $date, '4'], | ||
['S', 'en-EN', $date, ''], | ||
['w', 'en-EN', $date, '7'], | ||
]; | ||
} | ||
|
||
} |
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,82 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) 2018 Heimrich & Hannot GmbH | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace HeimrichHannot\UtilsBundle\Tests\Model; | ||
|
||
use Contao\ContentModel; | ||
use Contao\Model; | ||
use Contao\TestCase\ContaoTestCase; | ||
use HeimrichHannot\UtilsBundle\Model\ModelUtil; | ||
|
||
class ModelUtilTest extends ContaoTestCase | ||
{ | ||
public function testInstantiation() | ||
{ | ||
$util = new ModelUtil($this->mockContaoFramework()); | ||
$this->assertInstanceOf(ModelUtil::class, $util); | ||
} | ||
|
||
public function testFindModelInstanceByPk() | ||
{ | ||
$util = new ModelUtil($this->prepareFramework()); | ||
$this->assertNull($util->findModelInstanceByPk('tl_null', 5)); | ||
$this->assertNull($util->findModelInstanceByPk('tl_null_class', 5)); | ||
$this->assertNull($util->findModelInstanceByPk('tl_content', 4)); | ||
$this->assertNull($util->findModelInstanceByPk('tl_content', 'content_null')); | ||
$this->assertSame(5, $util->findModelInstanceByPk('tl_content', 5)->id); | ||
$this->assertSame('alias', $util->findModelInstanceByPk('tl_content', 'alias')->alias); | ||
} | ||
|
||
public function prepareFramework() | ||
{ | ||
$modelAdapter = $this->mockAdapter([ | ||
'getClassFromTable', | ||
]); | ||
$modelAdapter | ||
->method('getClassFromTable') | ||
->with($this->anything())->willReturnCallback(function ($table) { | ||
switch ($table) { | ||
case 'tl_content': | ||
return ContentModel::class; | ||
case 'tl_null_class': | ||
return 'Huh\Null\Class\Nullclass'; | ||
default: | ||
return null; | ||
} | ||
}) | ||
; | ||
|
||
$newsModel = $this->mockClassWithProperties(ContentModel::class, [ | ||
'id' => 5, | ||
'alias' => 'alias', | ||
]); | ||
|
||
$contentModelAdapter = $this->mockAdapter(['findByPk']); | ||
$contentModelAdapter | ||
->method('findByPk') | ||
->with($this->anything(), $this->anything()) | ||
->willReturnCallback(function ($pk, $option) use ($newsModel) { | ||
switch ($pk) { | ||
case 'alias': | ||
return $newsModel; | ||
case 5: | ||
return $newsModel; | ||
default: | ||
return null; | ||
} | ||
}) | ||
; | ||
|
||
$framework = $this->mockContaoFramework([ | ||
Model::class => $modelAdapter, | ||
ContentModel::class => $contentModelAdapter, | ||
]); | ||
|
||
return $framework; | ||
} | ||
} |
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