Skip to content

Commit 208ea05

Browse files
committed
[TASK] Use dependency injection in commands
Injecting an instance of the Setup class in the commands allows us to add tests for those commands. The tests just check if the right Setup method is called and the output is correct.
1 parent cd977ef commit 208ea05

File tree

7 files changed

+216
-8
lines changed

7 files changed

+216
-8
lines changed

.github/workflows/ci.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
name: Build PHP/TYPO3
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
include:
15+
- php-versions: '8.1'
16+
typo3-versions: '^11'
17+
- php-versions: '8.1'
18+
typo3-versions: '^12'
19+
- php-versions: '8.2'
20+
typo3-versions: '^11'
21+
- php-versions: '8.2'
22+
typo3-versions: '^12'
23+
- php-versions: '8.2'
24+
typo3-versions: '^13'
25+
- php-versions: '8.3'
26+
typo3-versions: '^11'
27+
- php-versions: '8.3'
28+
typo3-versions: '^12'
29+
- php-versions: '8.3'
30+
typo3-versions: '^13'
31+
- php-versions: '8.4'
32+
typo3-versions: '^12'
33+
- php-versions: '8.4'
34+
typo3-versions: '^13'
35+
steps:
36+
- name: Check out repository
37+
uses: actions/checkout@v5
38+
with:
39+
fetch-depth: 1
40+
- name: Setup PHP version
41+
uses: shivammathur/setup-php@v2
42+
with:
43+
php-version: ${{ matrix.php-versions }}
44+
extensions: mbstring
45+
- name: Get Composer Cache Directory
46+
id: composer-cache
47+
run: |
48+
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
49+
- name: Cache composer dependencies
50+
uses: actions/cache@v4
51+
with:
52+
path: ${{ steps.composer-cache.outputs.dir }}
53+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
54+
restore-keys: ${{ runner.os }}-composer-
55+
- name: Install composer dependencies
56+
run: |
57+
composer require typo3/cms-core=${{ matrix.typo3-versions }} --no-progress --prefer-dist --optimize-autoloader
58+
- name: Run PHP linter
59+
run: |
60+
find . -type f -name '*.php' ! -path "./.Build/*" -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" )
61+
- name: Run unit tests
62+
run: |
63+
.Build/bin/phpunit -c Tests/phpunit.xml.dist
64+
code-quality:
65+
name: Code Quality
66+
runs-on: ubuntu-latest
67+
steps:
68+
- name: Check out repository
69+
uses: actions/checkout@v5
70+
with:
71+
fetch-depth: 1
72+
- name: Setup PHP version
73+
uses: shivammathur/setup-php@v2
74+
with:
75+
php-version: 8.2
76+
extensions: mbstring
77+
- name: Get Composer Cache Directory
78+
id: composer-cache
79+
run: |
80+
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
81+
- name: Cache composer dependencies
82+
uses: actions/cache@v4
83+
with:
84+
path: ${{ steps.composer-cache.outputs.dir }}
85+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
86+
restore-keys: ${{ runner.os }}-composer-
87+
- name: Install composer dependencies
88+
run: |
89+
composer --version
90+
composer update --no-progress --prefer-dist --optimize-autoloader
91+
- name: Verify PSR-4 namespace correctness
92+
run: |
93+
composer dumpautoload --optimize --strict-psr

Classes/Command/DisableCommand.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Output\OutputInterface;
1111
use Symfony\Component\Console\Style\SymfonyStyle;
12-
use TYPO3\CMS\Core\Utility\GeneralUtility;
1312

14-
class DisableCommand extends Command
13+
final class DisableCommand extends Command
1514
{
15+
public function __construct(
16+
private readonly Setup $setup,
17+
) {
18+
parent::__construct();
19+
}
20+
1621
protected function execute(InputInterface $input, OutputInterface $output): int
1722
{
18-
GeneralUtility::makeInstance(Setup::class)->disable();
23+
$this->setup->disable();
1924
$io = new SymfonyStyle($input, $output);
2025
$io->success('Crowdin disabled');
26+
2127
return Command::SUCCESS;
2228
}
2329
}

Classes/Command/EnableCommand.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Output\OutputInterface;
1111
use Symfony\Component\Console\Style\SymfonyStyle;
12-
use TYPO3\CMS\Core\Utility\GeneralUtility;
1312

14-
class EnableCommand extends Command
13+
final class EnableCommand extends Command
1514
{
15+
public function __construct(
16+
private readonly Setup $setup,
17+
) {
18+
parent::__construct();
19+
}
20+
1621
protected function execute(InputInterface $input, OutputInterface $output): int
1722
{
18-
GeneralUtility::makeInstance(Setup::class)->enable();
23+
$this->setup->enable();
1924
$io = new SymfonyStyle($input, $output);
2025
$io->success('Crowdin enabled');
26+
2127
return Command::SUCCESS;
2228
}
2329
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace FriendsOfTYPO3\Crowdin\Tests\Unit\Command;
7+
8+
use FriendsOfTYPO3\Crowdin\Command\DisableCommand;
9+
use FriendsOfTYPO3\Crowdin\Setup;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
#[CoversClass(DisableCommand::class)]
16+
final class DisableCommandTest extends TestCase
17+
{
18+
#[Test]
19+
public function setUp(): void
20+
{
21+
$setupMock = self::createMock(Setup::class);
22+
$setupMock
23+
->expects(self::once())
24+
->method('disable');
25+
26+
$command = new DisableCommand($setupMock);
27+
$commandTester = new CommandTester($command);
28+
29+
$commandTester->execute([]);
30+
31+
self::assertSame(0, $commandTester->getStatusCode());
32+
self::assertStringContainsString('[OK] Crowdin disabled', $commandTester->getDisplay());
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace FriendsOfTYPO3\Crowdin\Tests\Unit\Command;
7+
8+
use FriendsOfTYPO3\Crowdin\Command\EnableCommand;
9+
use FriendsOfTYPO3\Crowdin\Setup;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
#[CoversClass(EnableCommand::class)]
16+
final class EnableCommandTest extends TestCase
17+
{
18+
#[Test]
19+
public function setUp(): void
20+
{
21+
$setupMock = self::createMock(Setup::class);
22+
$setupMock
23+
->expects(self::once())
24+
->method('enable');
25+
26+
$command = new EnableCommand($setupMock);
27+
$commandTester = new CommandTester($command);
28+
29+
$commandTester->execute([]);
30+
31+
self::assertSame(0, $commandTester->getStatusCode());
32+
self::assertStringContainsString('[OK] Crowdin enabled', $commandTester->getDisplay());
33+
}
34+
}

Tests/phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="../.Build/vendor/phpunit/phpunit/phpunit.xsd"
5+
bootstrap="../.Build/vendor/autoload.php"
6+
cacheResult="false"
7+
displayDetailsOnTestsThatTriggerDeprecations="true"
8+
displayDetailsOnTestsThatTriggerErrors="true"
9+
displayDetailsOnTestsThatTriggerNotices="true"
10+
displayDetailsOnTestsThatTriggerWarnings="true"
11+
executionOrder="random"
12+
>
13+
<coverage/>
14+
<testsuites>
15+
<testsuite name="Unit tests">
16+
<directory>Unit/</directory>
17+
</testsuite>
18+
</testsuites>
19+
<source>
20+
<include>
21+
<directory>../Classes/</directory>
22+
</include>
23+
</source>
24+
</phpunit>

composer.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@
3030
"php": ">= 8.1.0, <= 8.4.99",
3131
"typo3/cms-core": "^11 || ^12 || ^13"
3232
},
33+
"require-dev": {
34+
"phpunit/phpunit": "^10.5"
35+
},
3336
"autoload": {
3437
"psr-4": {
3538
"FriendsOfTYPO3\\Crowdin\\": "Classes/"
3639
}
3740
},
41+
"autoload-dev": {
42+
"psr-4": {
43+
"FriendsOfTYPO3\\Crowdin\\Tests\\": "Tests/"
44+
}
45+
},
3846
"extra": {
3947
"branch-alias": {
4048
"dev-main": "3.1.x-dev"
@@ -49,7 +57,10 @@
4957
"typo3/cms-composer-installers": true,
5058
"typo3/class-alias-loader": true
5159
},
52-
"bin-dir": ".Build/bin",
53-
"vendor-dir": ".Build/vendor"
60+
"bin-dir": ".Build/bin",
61+
"vendor-dir": ".Build/vendor"
62+
},
63+
"scripts": {
64+
"ci:tests:unit": "phpunit -c Tests/phpunit.xml.dist"
5465
}
5566
}

0 commit comments

Comments
 (0)