Skip to content

Commit

Permalink
Merge branch 'master' of github.com:heimrichhannot/contao-utils-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Patzer committed Feb 1, 2018
2 parents 5a51479 + 8fa3c08 commit d643c52
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 18 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 7.2
env:
- CONTAO_VERSION="4.4.*"
- CONTAO_VERSION="4.5.*"
install:
- composer require contao/core-bundle:${CONTAO_VERSION}
sudo: false
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
[![](https://img.shields.io/packagist/v/heimrichhannot/contao-utils-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-utils-bundle)
[![](https://img.shields.io/packagist/dt/heimrichhannot/contao-utils-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-utils-bundle)
[![](https://img.shields.io/travis/heimrichhannot/contao-utils-bundle/master.svg)](https://travis-ci.org/heimrichhannot/contao-utils-bundle/)
[![](https://img.shields.io/coveralls/heimrichhannot/contao-utils-bundle/master.svg)](https://coveralls.io/github/heimrichhannot/contao-head-bundle)
[![](https://img.shields.io/coveralls/heimrichhannot/contao-utils-bundle/master.svg)](https://coveralls.io/github/heimrichhannot/contao-utils-bundle)

This bundle offers various utility functionality for the Contao CMS.

## Utils

### Models `huh.utils.model`

Method | Description
----------------------|------------
findModelInstanceByPk | Returns a model instance if for a given table and id or null if not exist.

### Routing `huh.utils.routing`

Method | Description
Expand Down
30 changes: 26 additions & 4 deletions src/Date/DateUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,36 @@ public function getTimePeriodInSeconds($timePeriod)
return $timePeriod['value'] * $factor;
}

/**
* Format a php date formate pattern to an RFC3339 compliant format
*
* @param string $format The php date format (see: http://php.net/manual/de/function.date.php#refsect1-function.date-parameters)
*
* @return string The RFC3339 compliant format (see: http://userguide.icu-project.org/formatparse/datetime)
*/
public function formatPhpDateToRFC3339(string $format): string
{
$mapping = [
'd' => 'dd', //Day of the month, 2 digits with leading zeros (01 to 31)
'D' => 'E', // A textual representation of a day, three letters (Mon through Sun)
'j' => 'd', // Day of the month without leading zeros (1 to 31)
'l' => 'EEEE', // A full textual representation of the day of the week (Sunday through Saturday)
'N' => 'd', // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) (1 (for Monday) through 7 (for Sunday))
'S' => '', // English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th. Works well with j)
'w' => 'e', // Numeric representation of the day of the week (0 (for Sunday) through 6 (for Saturday))
];

return str_replace(array_keys($mapping), array_values($mapping), $format);
}

/**
* Format a php date format string to javascript compatible date format string.
*
* @param string $php_format The date format (e.g. "d.m.y H:i")
*
* @return string The formatted js date string
*/
public static function formatPhpDateToJsDate($php_format)
public function formatPhpDateToJsDate($php_format)
{
$SYMBOLS_MATCHING = [
// Day
Expand Down Expand Up @@ -91,7 +113,7 @@ public static function formatPhpDateToJsDate($php_format)
];

$replacement = '';
$escaping = false;
$escaping = false;

for ($i = 0; $i < strlen($php_format); ++$i) {
$char = $php_format[$i];
Expand All @@ -100,13 +122,13 @@ public static function formatPhpDateToJsDate($php_format)
if ($escaping) {
$replacement .= $php_format[$i];
} else {
$replacement .= '\''.$php_format[$i];
$replacement .= '\'' . $php_format[$i];
}
$escaping = true;
} else {
if ($escaping) {
$replacement .= "'";
$escaping = false;
$escaping = false;
}
if (isset($SYMBOLS_MATCHING[$char])) {
$replacement .= $SYMBOLS_MATCHING[$char];
Expand Down
5 changes: 4 additions & 1 deletion src/Model/ModelUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function __construct(ContaoFrameworkInterface $framework)
}

/**
* Returns a model instance if for a given table and id(primary key).
* Return null, if model type or model instance with given id not exist.
*
* @param string $table
* @param mixed $pk
* @param array $options
Expand All @@ -30,7 +33,7 @@ public function __construct(ContaoFrameworkInterface $framework)
*/
public function findModelInstanceByPk(string $table, $pk, array $options = [])
{
if (!($modelClass = Model::getClassFromTable($table))) {
if (!($modelClass = $this->framework->getAdapter(Model::class)->getClassFromTable($table))) {
return null;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,73 @@ services:
# util services
huh.utils.array:
class: HeimrichHannot\UtilsBundle\Arrays\ArrayUtil
public: true
arguments:
- "@contao.framework"
huh.utils.container:
class: HeimrichHannot\UtilsBundle\Container\ContainerUtil
public: true
arguments:
- "@contao.framework"
huh.utils.database:
class: HeimrichHannot\UtilsBundle\Database\DatabaseUtil
public: true
arguments:
- "@contao.framework"
huh.utils.date:
class: HeimrichHannot\UtilsBundle\Date\DateUtil
public: true
arguments:
- "@contao.framework"
huh.utils.dca:
class: HeimrichHannot\UtilsBundle\Dca\DcaUtil
public: true
arguments:
- "@contao.framework"
huh.utils.file:
class: HeimrichHannot\UtilsBundle\File\FileUtil
public: true
arguments:
- "@contao.framework"
huh.utils.form:
class: HeimrichHannot\UtilsBundle\Form\FormUtil
public: true
arguments:
- "@contao.framework"
huh.utils.image:
class: HeimrichHannot\UtilsBundle\Image\Image
public: true
arguments:
- "@contao.framework"
huh.utils.model:
class: HeimrichHannot\UtilsBundle\Model\ModelUtil
public: true
arguments:
- "@contao.framework"
huh.utils.string:
class: HeimrichHannot\UtilsBundle\String\StringUtil
public: true
arguments:
- "@contao.framework"
huh.utils.url:
class: HeimrichHannot\UtilsBundle\Url\UrlUtil
public: true
arguments:
- "@contao.framework"
# choice
huh.utils.choice.field:
class: HeimrichHannot\UtilsBundle\Choice\FieldChoice
public: true
arguments:
- "@contao.framework"
huh.utils.choice.data_container:
class: HeimrichHannot\UtilsBundle\Choice\DataContainerChoice
public: true
arguments:
- "@contao.framework"
huh.utils.choice.message:
class: HeimrichHannot\UtilsBundle\Choice\MessageChoice
public: true
arguments:
- "@contao.framework"
huh.utils.routing:
Expand Down
91 changes: 91 additions & 0 deletions tests/Date/DateUtilTest.php
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'],
];
}

}
82 changes: 82 additions & 0 deletions tests/ModelUtilTest.php
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;
}
}
19 changes: 7 additions & 12 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/

error_reporting(E_ALL);
define('TL_MODE', 'FE'); // required by contao 3
define('UNIT_TESTING', true);

$include = function ($file) {
return file_exists($file) ? include $file : false;
Expand All @@ -19,21 +17,18 @@
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/../vendor/autoload.php');
}

if (false === ($loader = $include(__DIR__.'/../vendor/autoload.php'))
&& false === ($loader = $include(__DIR__.'/../../../../composer/vendor/autoload.php'))
&& false === ($loader = $include(__DIR__.'/../../../../autoload.php'))
if (
false === ($loader = $include(__DIR__.'/../vendor/autoload.php'))
&& false === ($loader = $include(__DIR__.'/../../../autoload.php'))
&& false === ($loader = $include(dirname(dirname(getenv('PWD'))).'/autoload.php'))) {
echo 'You must set up the project dependencies, run the following commands:'.PHP_EOL.'curl -sS https://getcomposer.org/installer | php'.PHP_EOL.'php composer.phar install'.PHP_EOL;
&& false === ($loader = $include(dirname(dirname(getenv('PWD'))).'/autoload.php'))
) {
echo 'You must set up the project dependencies, run the following commands:'.PHP_EOL
.'curl -sS https://getcomposer.org/installer | php'.PHP_EOL
.'php composer.phar install'.PHP_EOL;

exit(1);
}

if ((false !== ($contao3 = $include(__DIR__.'/../../../../../system/initialize.php'))
|| false !== ($contao3 = $include(__DIR__.'/../../../initialize.php')))) {
// contao 3
}

// Handle classes in the global namespace
$legacyLoader = function ($class) {
if (class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) {
Expand Down

0 comments on commit d643c52

Please sign in to comment.