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 15, 2018
2 parents 4494f43 + 27f73a1 commit f93e9ee
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Arrays/ArrayUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function filterByPrefixes(array $data = [], $prefixes = [])
*
* @param array $array
*/
public function aasort(&$array, $key)
public function aasort(array &$array, $key)
{
$sorter = [];
$ret = [];
Expand Down
81 changes: 81 additions & 0 deletions tests/Arrays/ArrayUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Tests\Arrays;

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\Arrays\ArrayUtil;
use HeimrichHannot\UtilsBundle\String\StringUtil;

class ArrayUtilTest extends ContaoTestCase
{
public function setUp()
{
parent::setUp();

$stringUtil = new StringUtil($this->mockContaoFramework());

$container = $this->mockContainer();
$container->set('huh.utils.string', $stringUtil);
System::setContainer($container);
}

/**
* Tests the object instantiation.
*/
public function testCanBeInstantiated()
{
$framework = $this->mockContaoFramework();
$instance = new ArrayUtil($framework);
$this->assertInstanceOf(ArrayUtil::class, $instance);
}

public function testAasort()
{
$framework = $this->mockContaoFramework();
$arrayUtil = new ArrayUtil($framework);

$array = [0 => ['filename' => 'testfile3'], 1 => ['filename' => 'testfile1'], 2 => ['filename' => 'testfile2']];

$arrayUtil->aasort($array, 'filename');
$this->assertSame([1 => ['filename' => 'testfile1'], 2 => ['filename' => 'testfile2'], 0 => ['filename' => 'testfile3']], $array);
}

public function testRemoveValue()
{
$framework = $this->mockContaoFramework();
$arrayUtil = new ArrayUtil($framework);

$array = [0 => 0, 1 => 1, 2 => 2];
$result = $arrayUtil->removeValue(1, $array);
$this->assertTrue($result);
$this->assertCount(2, $array);
$this->assertArrayHasKey(0, $array);
$this->assertArrayHasKey(2, $array);

$result = $arrayUtil->removeValue(1, $array);
$this->assertFalse($result);
}

public function testFilterByPrefixes()
{
$framework = $this->mockContaoFramework();
$arrayUtil = new ArrayUtil($framework);

$array = ['ls_0' => 0, 1 => 1, 2 => 2];
$result = $arrayUtil->filterByPrefixes($array);
$this->assertSame($array, $result);

$result = $arrayUtil->filterByPrefixes($array, [1]);
$this->assertSame([], $result);

$result = $arrayUtil->filterByPrefixes($array, ['ls']);
$this->assertSame(['ls_0' => 0], $result);
}
}
16 changes: 2 additions & 14 deletions tests/File/FileUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
use Contao\FilesModel;
use Contao\Folder;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\Arrays\ArrayUtil;
use HeimrichHannot\UtilsBundle\File\FileUtil;
use HeimrichHannot\UtilsBundle\String\StringUtil;
use HeimrichHannot\UtilsBundle\Tests\TestCaseEnvironment;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\RequestStack;

class FileUtilTest extends ContaoTestCase
class FileUtilTest extends TestCaseEnvironment
{
public static function tearDownAfterClass(): void
{
Expand All @@ -46,7 +45,6 @@ public function setUp()
$filesAdapter = $this->mockAdapter(['findByUuid']);
$filesAdapter->method('findByUuid')->willReturn($filesModel);
$container->set('contao.framework', $this->mockContaoFramework([FilesModel::class => $filesAdapter]));
$container->set('request_stack', $this->createRequestStackMock());

$utilsString = new StringUtil($this->mockContaoFramework());
$container->set('huh.utils.string', $utilsString);
Expand All @@ -57,16 +55,6 @@ public function setUp()
}
}

public function createRequestStackMock()
{
$requestStack = new RequestStack();
$request = new \Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_contao_referer_id', 'foobar');
$requestStack->push($request);

return $requestStack;
}

public function testGetFileList()
{
file_put_contents($this->getTempDir().'/files/testfile1', 'test');
Expand Down
113 changes: 113 additions & 0 deletions tests/Image/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Tests\Image;

use Contao\FilesModel;
use Contao\Image\ImageInterface;
use Contao\Image\Picture;
use Contao\PageModel;
use Contao\System;
use HeimrichHannot\UtilsBundle\Image\Image;
use HeimrichHannot\UtilsBundle\Tests\TestCaseEnvironment;

class ImageTest extends TestCaseEnvironment
{
public function setUp()
{
parent::setUp();

if (!defined('TL_FILES_URL')) {
define('TL_FILES_URL', '');
}

if (!defined('TL_ERROR')) {
define('TL_ERROR', 'ERROR: ');
}

if (!isset($GLOBALS['TL_LANGUAGE'])) {
$GLOBALS['TL_LANGUAGE'] = 'de';
}

$container = System::getContainer();

$utilsContainer = $this->mockAdapter(['isBackend', 'isFrontend']);
$utilsContainer->method('isBackend')->willReturn(false);
$utilsContainer->method('isFrontend')->willReturn(true);
$container->set('huh.utils.container', $utilsContainer);

$imageAdapter = $this->mockAdapter(['getUrl']);
$imageAdapter->method('getUrl')->willReturn(__DIR__.'/../data/screenshot.jpg');
$imageFactoryAdapter = $this->mockAdapter(['create']);
$imageFactoryAdapter->method('create')->willReturn($imageAdapter);
$container->set('contao.image.image_factory', $imageFactoryAdapter);

$imageMock = $this->createMock(ImageInterface::class);
$pictureMock = new Picture(['src' => $imageMock, 'srcset' => []], []);
$pictureFactoryAdapter = $this->mockAdapter(['create']);
$pictureFactoryAdapter->method('create')->willReturn($pictureMock);
$container->set('contao.image.picture_factory', $pictureFactoryAdapter);
System::setContainer($container);
}

public function testAddToTemplateDataWithoutModel()
{
$imageArray['imagemargin'] = 'a:5:{s:6:"bottom";s:0:"";s:4:"left";s:0:"";s:5:"right";s:0:"";s:3:"top";s:0:"";s:4:"unit";s:0:"";}';
$imageArray['singleSRC'] = __DIR__.'/../data/screenshot.jpg';
$imageArray['size'] = 'a:3:{i:0;s:0:"2";i:1;s:0:"2";i:2;s:0:"2";}';
$imageArray['alt'] = '';
$imageArray['fullsize'] = false;
$imageArray['floating'] = false;
$imageArray['imageUrl'] = __DIR__.'/../data/screenshot.jpg';
$imageArray['linkTitle'] = true;

$templateData['images']['href'] = true;
$templateData['images']['singleSRC'] = [];

$image = new Image();
$image->addToTemplateData('singleSRC', 'addImage', $templateData['images'], $imageArray);

$this->assertNotSame(['images' => ['href' => true, 'singleSRC' => []]], $templateData);
$this->assertSame(__DIR__.'/../data/screenshot.jpg', $templateData['images']['singleSRC']);
}

public function testAddToTemplateDataWithModel()
{
global $objPage;

$objPage = $this->mockClassWithProperties(PageModel::class, ['language' => 'de', 'rootFallbackLanguage' => 'de']);

$GLOBALS['TL_DCA']['tl_files']['fields']['meta']['eval']['metaFields'] = ['title' => 'maxlenght="255"', 'alt' => 'maxlenght="255"', 'link' => 'maxlenght="255"', 'caption' => 'maxlenght="255"'];

$imageArray['imagemargin'] = 'a:5:{s:6:"bottom";i:10;s:4:"left";i:10;s:5:"right";i:10;s:3:"top";i:10;s:4:"unit";s:2:"px";}';
$imageArray['singleSRC'] = __DIR__.'/../data/screenshot.jpg';
$imageArray['size'] = 'a:3:{i:0;s:0:"2";i:1;s:0:"2";i:2;s:0:"2";}';
$imageArray['alt'] = '';
$imageArray['fullsize'] = true;
$imageArray['floating'] = false;
$imageArray['imageUrl'] = __DIR__.'/../data/screenshot.jpg';
$imageArray['linkTitle'] = 'linkTitle';
$imageArray['overwriteMeta'] = false;
$imageArray['caption'] = [];
$imageArray['id'] = 12;
$imageArray['imageTitle'] = 'imageTitle';

$data['images']['href'] = true;
$data['images']['singleSRC'] = [];

$model = $this->mockClassWithProperties(FilesModel::class, ['meta' => 'a:1:{s:2:"de";a:4:{s:5:"title";s:9:"Diebstahl";s:3:"alt";s:0:"";s:4:"link";s:0:"";s:7:"caption";s:209:"Ob Stifte, Druckerpapier oder Büroklammern: Jeder vierte Arbeitnehmer lässt im Büro etwas mitgehen. Doch egal, wie günstig die gestohlenen Gegenstände sein mögen: Eine Abmahnung ist gerechtfertigt.";}}']);

$image = new Image();
$image->addToTemplateData('singleSRC', 'addImage', $data['images'], $imageArray, 400, null, null, $model);

$this->assertNotSame(['images' => ['href' => true, 'singleSRC' => []]], $data);
$this->assertSame(__DIR__.'/../data/screenshot.jpg', $data['images']['singleSRC']);
$this->assertSame('margin:10px;', $data['images']['margin']);
$this->assertSame('Diebstahl', $data['images']['imageTitle']);
}
}
39 changes: 39 additions & 0 deletions tests/TestCaseEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Tests;

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Log\Logger;

abstract class TestCaseEnvironment extends ContaoTestCase
{
public function setUp()
{
parent::setUp();

$container = $this->mockContainer();
$container->set('request_stack', $this->createRequestStackMock());

$logger = new Logger();
$container->set('monolog.logger.contao', $logger);
System::setContainer($container);
}

public function createRequestStackMock()
{
$requestStack = new RequestStack();
$request = new \Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_contao_referer_id', 'foobar');
$requestStack->push($request);

return $requestStack;
}
}
12 changes: 5 additions & 7 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/../vendor/autoload.php');
}

if (
false === ($loader = $include(__DIR__.'/../vendor/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);
}

require 'TestCaseEnvironment.php';

// Handle classes in the global namespace
$legacyLoader = function ($class) {
if (class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) {
Expand Down
Binary file added tests/data/screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f93e9ee

Please sign in to comment.