Skip to content

Commit

Permalink
[#187] Updated to PHPUnit 11 and moved to attributes in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed May 16, 2024
1 parent 1d6c291 commit 6a8b6c7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 43 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"opis/closure": "^3.6",
"phpmd/phpmd": "^2.13",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10",
"phpunit/phpunit": "^11",
"rector/rector": "^1.0.0"
},
"minimum-stability": "stable",
Expand Down Expand Up @@ -67,14 +67,14 @@
"box compile"
],
"lint": [
"cp php-script php-script.php && phpcs && rm php-script.php",
"cp php-script php-script.php && phpcs; rm php-script.php",
"phpmd --exclude vendor,vendor-bin,node_modules . text phpmd.xml",
"phpstan",
"rector --clear-cache --dry-run"
],
"lint-fix": [
"rector --clear-cache",
"cp php-script php-script.php && phpcbf && rm php-script.php"
"cp php-script php-script.php && phpcbf; rm php-script.php"
],
"test": "phpunit --no-coverage",
"test-coverage": "phpunit"
Expand Down
10 changes: 5 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
Expand All @@ -11,15 +11,16 @@
failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
>
displayDetailsOnTestsThatTriggerNotices="true">
<testsuites>
<testsuite name="default">
<directory>tests/phpunit</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<source restrictNotices="true"
restrictWarnings="true"
ignoreIndirectDeprecations="true">
<include>
<!-- #;< PHP_COMMAND -->
<directory>src</directory>
Expand All @@ -28,7 +29,6 @@
<file>php-script</file>
<!-- #;> !PHP_COMMAND -->
</include>

<exclude>
<directory>tests</directory>
</exclude>
Expand Down
46 changes: 46 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
>
<testsuites>
<testsuite name="default">
<directory>tests/phpunit</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<!-- #;< PHP_COMMAND -->
<directory>src</directory>
<!-- #;> PHP_COMMAND -->
<!-- #;< !PHP_COMMAND -->
<file>php-script</file>
<!-- #;> !PHP_COMMAND -->
</include>

<exclude>
<directory>tests</directory>
</exclude>
</source>

<coverage includeUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="false">
<report>
<html outputDirectory=".coverage-html" lowUpperBound="50" highLowerBound="90"/>
<cobertura outputFile="cobertura.xml"/>
</report>
</coverage>
</phpunit>
15 changes: 9 additions & 6 deletions tests/phpunit/Functional/ExampleScriptFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

namespace YourNamespace\App\Tests\Functional;

use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;

/**
* Class ExampleScriptFunctionalTest.
*
* Functional tests for php-script.
*/
#[CoversFunction('main')]
#[Group('scripts')]
class ExampleScriptFunctionalTest extends ScriptFunctionalTestCase {

/**
* @covers ::main
* @dataProvider dataProviderMain
* @runInSeparateProcess
* @group script
*/
#[DataProvider('dataProviderMain')]
#[RunInSeparateProcess]
public function testMain(array|string $args, int $expected_code, string $expected_output): void {
$result = $this->runScript($args);
$this->assertEquals($expected_code, $result['code']);
Expand Down
19 changes: 8 additions & 11 deletions tests/phpunit/Unit/Command/JokeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@

namespace YourNamespace\App\Tests\Unit\Command;

use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Console\Command\Command;
use YourNamespace\App\Command\JokeCommand;

/**
* Class JokeCommandTest.
*
* This is a unit test for the JokeCommand class.
*
* @coversDefaultClass \YourNamespace\App\Command\JokeCommand
*/
#[CoversMethod(JokeCommand::class, 'execute')]
#[CoversMethod(JokeCommand::class, 'configure')]
#[CoversMethod(JokeCommand::class, 'getJoke')]
#[Group('command')]
class JokeCommandTest extends CommandTestCase {

/**
* Test the execute method.
*
* @covers ::execute
* @covers ::configure
* @covers ::getJoke
* @dataProvider dataProviderExecute
* @group command
*/
#[DataProvider('dataProviderExecute')]
public function testExecute(string $content, int $expected_code, array|string $expected_output = []): void {
/** @var \YourNamespace\App\Command\JokeCommand $mock */
$mock = $this->prepareMock(JokeCommand::class, [
Expand Down
14 changes: 5 additions & 9 deletions tests/phpunit/Unit/Command/SayHelloCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@

namespace YourNamespace\App\Tests\Unit\Command;

use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;
use YourNamespace\App\Command\SayHelloCommand;

/**
* Class SayHelloCommandTest.
*
* This is a unit test for the SayHelloCommand class.
*
* @coversDefaultClass \YourNamespace\App\Command\SayHelloCommand
*/
#[CoversMethod(SayHelloCommand::class, 'execute')]
#[CoversMethod(SayHelloCommand::class, 'configure')]
#[Group('command')]
class SayHelloCommandTest extends CommandTestCase {

/**
* Test the execute method.
*
* @covers ::execute
* @covers ::configure
* @group command
*/
public function testExecute(): void {
$output = $this->runExecute(SayHelloCommand::class);
$this->assertArrayContainsString('Hello, Symfony console!', $output);
Expand Down
18 changes: 9 additions & 9 deletions tests/phpunit/Unit/ExampleScriptUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

namespace YourNamespace\App\Tests\Unit;

use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
* Class ExampleScriptUnitTest.
*
* Unit tests for php-script.
*
* @group scripts
*/
#[CoversFunction('main')]
#[CoversFunction('print_help')]
#[CoversFunction('verbose')]
#[Group('scripts')]
class ExampleScriptUnitTest extends ScriptUnitTestCase {

/**
* @covers ::main
* @covers ::print_help
* @covers ::verbose
* @dataProvider dataProviderMain
* @group script
*/
#[DataProvider('dataProviderMain')]
public function testMain(string|array $args = [], array|string $expected_output = [], string|null $expected_exception_message = NULL): void {
if ($expected_exception_message) {
$this->expectException(\Exception::class);
Expand Down

0 comments on commit 6a8b6c7

Please sign in to comment.