Skip to content

Commit

Permalink
Merge pull request #67 from sitegeist/task/testing
Browse files Browse the repository at this point in the history
Task/testing
  • Loading branch information
s2b authored Oct 13, 2020
2 parents 1b6cfa2 + bf7a9eb commit 2b544d0
Show file tree
Hide file tree
Showing 30 changed files with 730 additions and 11 deletions.
44 changes: 40 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: composer lint:editorconfig


test:
unittest:
runs-on: ubuntu-latest

strategy:
Expand All @@ -43,7 +43,7 @@ jobs:
php-versions: [7.4, 7.3, 7.2]
typo3-versions: [10.4, 9.5]

name: PHP ${{ matrix.php-versions }} with TYPO3 ${{ matrix.typo3-versions }}
name: Unit (PHP ${{ matrix.php-versions }}, TYPO3 ${{ matrix.typo3-versions }})
steps:
-
uses: actions/checkout@v2
Expand All @@ -67,5 +67,41 @@ jobs:
run: composer require typo3/minimal "^${{ matrix.typo3-versions }}" --prefer-dist --no-progress --no-suggest

-
name: Automated Testing
run: composer test
name: Automated Unit Testing
run: composer test:unit

integrationtest:
runs-on: ubuntu-latest

strategy:
max-parallel: 2
matrix:
php-versions: [7.4, 7.3, 7.2]
typo3-versions: [9.5]

name: Integration (PHP ${{ matrix.php-versions }}, TYPO3 ${{ matrix.typo3-versions }})
steps:
-
uses: actions/checkout@v2

-
name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: intl, mbstring, pdo_sqlite

-
name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache
key: php-${{ matrix.php-versions }}-typo3-${{ matrix.typo3-versions }}

-
name: Install composer dependencies
run: composer require typo3/minimal "^${{ matrix.typo3-versions }}" --prefer-dist --no-progress --no-suggest

-
name: Automated Integration Testing
run: composer test:integration
6 changes: 4 additions & 2 deletions Classes/Fluid/ViewHelper/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ protected function getTemplateIdentifier(string $pathAndFilename, string $prefix
protected function getComponentPrefixer()
{
if (!isset(self::$componentPrefixerCache[$this->componentNamespace])) {
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['prefixer'])) {
if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['prefixer']) &&
is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['prefixer'])
) {
arsort($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['prefixer']);
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['prefixer'] as $namespace => $prefixer) {
$namespace = ltrim($namespace, '\\');
Expand All @@ -483,7 +485,7 @@ protected function getComponentPrefixer()
}
}

if (!$componentPrefixerClass) {
if (empty($componentPrefixerClass)) {
$componentPrefixerClass = GenericComponentPrefixer::class;
}

Expand Down
5 changes: 4 additions & 1 deletion Classes/Utility/ComponentArgumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function __construct()
public function addConversionInterface(string $fromType, string $interface, string $constructor): self
{
$this->conversionInterfaces[$fromType] = [$interface, $constructor];
$this->conversionCache = [];
return $this;
}

Expand All @@ -112,6 +113,7 @@ public function addConversionInterface(string $fromType, string $interface, stri
public function removeConversionInterface(string $fromType): self
{
unset($this->conversionInterfaces[$fromType]);
$this->conversionCache = [];
return $this;
}

Expand Down Expand Up @@ -257,6 +259,7 @@ protected function extractCollectionItemType(string $type): string
*/
protected function isAccessibleArray(string $typeOrClassName): bool
{
return $typeOrClassName === 'array' || is_subclass_of($typeOrClassName, \ArrayAccess::class);
return $typeOrClassName === 'array' ||
(is_subclass_of($typeOrClassName, \ArrayAccess::class) && is_subclass_of($typeOrClassName, \Traversable::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GenericComponentPrefixer implements ComponentPrefixerInterface
*/
public function prefix(string $namespace): string
{
$namespace = explode('\\', $namespace);
$namespace = explode('\\', trim($namespace, '\\'));
unset($namespace[1], $namespace[2]);
return GeneralUtility::underscoredToLowerCamelCase(
implode('_', $namespace)
Expand Down
2 changes: 1 addition & 1 deletion Classes/Utility/ComponentSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function get(string $path)
public function set(string $path, $value)
{
$path = explode('.', $path);
$variable = $this->settings;
$variable =& $this->settings;
foreach ($path as $segment) {
if (!isset($variable[$segment])) {
$variable[$segment] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<fc:component>
<fc:renderer>{content}</fc:renderer>
</fc:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<fc:component>
<fc:param name="date" type="string" />
<fc:renderer><f:format.date date="{date}" format="r" /></fc:renderer>
</fc:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<fc:component>
<fc:param name="stringParameter" type="string" />
<fc:param name="integerParameter" type="integer" />
<fc:param name="trueParameter" type="boolean" />
<fc:param name="falseParameter" type="boolean" />

<fc:renderer>
{stringParameter}|{integerParameter}|{trueParameter}|{falseParameter}
</fc:renderer>
</fc:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<fc:component>
<fc:renderer>
Just a renderer.
</fc:renderer>
</fc:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace SMS\FluidComponents\Tests\Helpers\ComponentArgumentConverter;

interface DummyConversionInterface
{
public static function fromString(string $value);
}
17 changes: 17 additions & 0 deletions Tests/Helpers/ComponentArgumentConverter/DummyValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace SMS\FluidComponents\Tests\Helpers\ComponentArgumentConverter;

class DummyValue implements DummyConversionInterface
{
public $value;

public function __construct(string $value)
{
$this->value = $value;
}

public static function fromString(string $value)
{
return new static($value);
}
}
91 changes: 91 additions & 0 deletions Tests/Integration/ComponentRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace SMS\FluidComponents\Tests\Integration;

use SMS\FluidComponents\Fluid\ViewHelper\ComponentRenderer;
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInvoker;

class ComponentRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
{
protected $fluidNamespaces;
protected $componentNamespaces;

protected $testNamespace = 'SMS\\FluidComponents\\Tests\\Fixtures\\Integration\\Components';

protected $resetSingletonInstances = true;

public function setUp(): void
{
parent::setUp();

// Add fc ViewHelpers
$this->fluidNamespaces = $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces'] ?? null;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['fc'][] = 'SMS\\FluidComponents\\ViewHelpers';

// Register test components
$this->componentNamespaces = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['namespaces'] ?? null;
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['namespaces'] = [
$this->testNamespace => realpath(dirname(__FILE__) . '/../Fixtures/Integration/Components/')
];

// Register and then disable fluid cache
// This is necessary because of the way the RenderingContext deals with the fluid cache
$cacheClass = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['fluid_template']['frontend'];
$cacheManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
$cacheManager->registerCache(new $cacheClass('fluid_template', new NullBackend(null)));

$this->invoker = GeneralUtility::makeInstance(ViewHelperInvoker::class);
$this->renderer = GeneralUtility::makeInstance(ComponentRenderer::class);
}

public function renderComponentProvider()
{
return [
['WithoutParameters', [], '', 'Just a renderer.'],
['ContentParameter', ['content' => 'children'], '', 'children'],
['ContentParameter', [], 'children', 'children'],
['ScalarParameters', [
'stringParameter' => 'This is a string',
'integerParameter' => 123,
'trueParameter' => true, // strange fluid behavior: will be 1
'falseParameter' => false, // strange fluid behavior: will be empty string
], '', 'This is a string|123|1|'],
['DateTimeParameter', ['date' => 1601371704], '', 'Tue, 29 Sep 2020 09:28:24 +0000']
];
}

/**
* @test
* @dataProvider renderComponentProvider
*/
public function renderComponent($component, $arguments, $content, $expected)
{
// Render single component
$this->renderer->setComponentNamespace($this->testNamespace . '\\' . $component);
$renderingContext = GeneralUtility::makeInstance(RenderingContext::class);
$output = $this->invoker->invoke(
$this->renderer,
$arguments,
$renderingContext,
function () use ($content) {
return $content;
}
);

// Ignore whitespace output from components
$output = trim($output);

$this->assertEquals($expected, $output);
}

public function tearDown(): void
{
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['fluid_components']['namespaces'] = $this->componentNamespaces;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces'] = $this->fluidNamespaces;
parent::tearDown();
}
}
Loading

0 comments on commit 2b544d0

Please sign in to comment.