-
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
7 changed files
with
241 additions
and
22 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
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); | ||
} | ||
} |
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,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']); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.