Skip to content

Commit

Permalink
[TASK] Add unit test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ayacoo committed Feb 10, 2024
1 parent 0c21980 commit 254597d
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 3 deletions.
53 changes: 51 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,55 @@ jobs:
fail-fast: false
matrix:
command:
- "php:csfixer"
- "php:csfix"
php-version:
- "8.3"
- "8.3"
unit-tests:
name: "Unit tests"
runs-on: ubuntu-22.04
needs: php-lint
steps:
- name: "Checkout"
uses: actions/checkout@v4
- name: "Install PHP"
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php-version }}"
coverage: none
tools: composer:v2
- name: "Show Composer version"
run: "composer --version"
- name: "Show the Composer configuration"
run: "composer config --global --list"
- name: "Cache dependencies installed with composer"
uses: actions/cache@v4
with:
key: "php${{ matrix.php-version }}-typo3${{ matrix.typo3-version }}-${{ matrix.composer-dependencies }}-composer-${{ hashFiles('**/composer.json') }}"
path: ~/.cache/composer
restore-keys: "php${{ matrix.php-version }}-typo3${{ matrix.typo3-version }}-${{ matrix.composer-dependencies }}-composer-\n"
- name: "Install TYPO3 Core"
env:
TYPO3: "${{ matrix.typo3-version }}"
run: |
composer require --no-ansi --no-interaction --no-progress --no-install typo3/cms-core:"$TYPO3"
composer show
- name: "Install highest dependencies with composer"
if: "matrix.composer-dependencies == 'highest'"
run: |
composer update --no-ansi --no-interaction --no-progress --with-dependencies
composer show
- name: "Run unit tests"
run: "composer ci:tests:unit"
strategy:
fail-fast: false
matrix:
include:
- typo3-version: "^12.4"
php-version: "8.1"
composer-dependencies: highest
- typo3-version: "^12.4"
php-version: "8.2"
composer-dependencies: highest
- typo3-version: "^12.4"
php-version: "8.3"
composer-dependencies: highest
40 changes: 40 additions & 0 deletions Configuration/UnitTests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
backupGlobals="true"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="../.Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTestsBootstrap.php"
cacheResult="false"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
forceCoversAnnotation="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false"
>
<coverage/>
<testsuites>
<testsuite name="Unit tests">
<!--
This path either needs an adaption in extensions, or an extension's
test location path needs to be given to phpunit.
-->
<directory suffix="Test.php">./</directory>
</testsuite>
</testsuites>
<php>
<!-- @deprecated: will be removed with next major version, constant TYPO3_MODE is deprecated -->
<const name="TYPO3_MODE" value="BE"/>
<ini name="display_errors" value="1"/>
<env name="TYPO3_CONTEXT" value="Testing"/>
</php>
</phpunit>
72 changes: 72 additions & 0 deletions Tests/Unit/Helper/SoundcloudHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Ayacoo\AyacooSoundcloud\Tests\Unit\Domain\Model;

use Ayacoo\AyacooSoundcloud\Helper\SoundcloudHelper;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

final class SoundcloudHelperTest extends UnitTestCase
{
private SoundcloudHelper $subject;

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

$this->subject = new SoundcloudHelper('soundcloud');
}

/**
* @test
*/
public function isAbstractEntity(): void
{
self::assertInstanceOf(AbstractOEmbedHelper::class, $this->subject);
}

/**
* @test
* @dataProvider handleSoundcloudTitleDataProvider
*/
public function handleSoundcloudTitleReturnsFilteredTitle(string $input, string $expected)
{
$params = [$input];
$methodName = 'handleSoundcloudTitle';
$result = $this->buildReflectionForProtectedFunction($methodName, $params);

self::assertEquals($expected, $result);
}

public function handleSoundcloudTitleDataProvider(): array
{
return [
'No filter needed' => [
'Test',
'Test'
],
'Strip Tags' => [
'<h1>Test</h1>',
'Test'
],
'Trim' => [
'Test ',
'Test'
],
'MaxLength' => [
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lore',
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata'
],
];
}

private function buildReflectionForProtectedFunction(string $methodName, array $params)
{
$reflectionCalendar = new \ReflectionClass($this->subject);
$method = $reflectionCalendar->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($this->subject, $params);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"ci:tests:functional": [
"@ci:tests:create-directories",
"export typo3DatabaseName=\"typo3_test\" && export typo3DatabaseUsername=\"root\" && export typo3DatabasePassword=\"root\" && export typo3DatabaseHost=\"db\" && export TYPO3_PATH_WEB=\".Build/public\" && ./.Build/bin/phpunit -c ./Configuration/FunctionalTests.xml Tests/Functional"
]
],
"ci:tests:unit": ".Build/bin/phpunit -c ./Configuration/UnitTests.xml Tests/Unit"
},
"extra": {
"typo3/cms": {
Expand Down

0 comments on commit 254597d

Please sign in to comment.