Skip to content

Commit

Permalink
Use Pest instead of PHPT files
Browse files Browse the repository at this point in the history
Although I love PHPT files, and I've done my fair share of making it
easier to write them in this library, they're very slow, and running
them has become a hindrance.

I've been fidgeting with the idea of using Pest for a while, and I think
it's the right tool for the job. I had to create a couple of functions
to make it easier to run those tests, and now they're working really
alright.

I migrated all the PHPT files into Pest files -- I automated most of the
work with a little script using "nikic/php-parser"; this commit should
contain all the previous PHPT tests as Pest tests.

The previous integration tests would take sixteen seconds, and the Pest
tests take less than a second.
  • Loading branch information
henriquemoody committed Dec 16, 2024
1 parent ea29c2c commit 01d41c7
Show file tree
Hide file tree
Showing 411 changed files with 7,906 additions and 6,345 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ jobs:
run: composer install --prefer-dist ${{ matrix.composer-extra-arguments }}

- name: Run Unit Tests
run: ./vendor/phpunit/phpunit/phpunit --testsuite=unit
run: ./vendor/bin/phpunit --testsuite=unit

- name: Run Integration Tests
run: ./vendor/phpunit/phpunit/phpunit --testsuite=integration
run: ./vendor/bin/pest --testsuite=feature --compact

code-coverage:
name: Code coverage
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
run: composer install --prefer-dist ${{ matrix.composer-extra-arguments }}

- name: Generating Code Coverage Report
run: ./vendor/phpunit/phpunit/phpunit --coverage-clover=coverage.xml
run: ./vendor/bin/pest --compact --coverage-clover=coverage.xml

- name: Send Code Coverage Report to Codecov.io
uses: codecov/codecov-action@v5
Expand Down
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
Expand All @@ -29,6 +30,7 @@
"malukenho/docheader": "^1.0",
"mikey179/vfsstream": "^1.6",
"nette/php-generator": "^4.1",
"pestphp/pest": "^2.36",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
Expand Down Expand Up @@ -60,23 +62,20 @@
"Respect\\Validation\\": "tests/unit/",
"Respect\\Validation\\Test\\": "tests/library/"
},
"files": [
"tests/integration/lib/aliases.php",
"tests/integration/lib/helpers.php"
]
"files": ["tests/aliases.php"]
},
"scripts": {
"docheader": "vendor/bin/docheader check library/ tests/",
"phpcs": "vendor/bin/phpcs",
"phpstan": "vendor/bin/phpstan analyze",
"phpunit": "vendor/bin/phpunit",
"phpunit-integration": "vendor/bin/phpunit --testsuite=integration",
"phpunit-unit": "vendor/bin/phpunit --testsuite=unit",
"phpunit": "vendor/bin/phpunit --testsuite=unit",
"pest": "vendor/bin/pest --testsuite=feature --compact",
"qa": [
"@docheader",
"@phpcs",
"@phpstan",
"@phpunit"
"@phpunit",
"@pest"
]
}
}
2 changes: 1 addition & 1 deletion library/Transformers/DeprecatedAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function transform(RuleSpec $ruleSpec): RuleSpec

if ($name === 'property') {
trigger_error(
$firstMessage . ' Use property() without it.',
$firstMessage . ' Use property() instead.',
E_USER_DEPRECATED
);
}
Expand Down
2 changes: 1 addition & 1 deletion library/Transformers/DeprecatedKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function transform(RuleSpec $ruleSpec): RuleSpec
if ($name === 'key') {
trigger_error(
'Calling key() with a third parameter has been deprecated, ' .
'and will be not be allowed in the next major version. Use key() without it the third parameter.',
'and will be not be allowed in the next major version. Use key() without the third parameter.',
E_USER_DEPRECATED
);
}
Expand Down
9 changes: 5 additions & 4 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

<rule ref="Respect" />
<rule ref="Generic.Files.LineLength.TooLong">
<exclude-pattern>tests/integration/</exclude-pattern>
<exclude-pattern>tests/feature/</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.Functions.StaticClosure.ClosureNotStatic">
<exclude-pattern>tests/Pest.php</exclude-pattern>
<exclude-pattern>tests/feature</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.Attributes.AttributesOrder">
<properties>
Expand All @@ -26,7 +30,4 @@
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing">
<exclude-pattern>tests/integration/</exclude-pattern>
</rule>
</ruleset>
26 changes: 15 additions & 11 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@ parameters:
# Why: SimpleXMLElement is weird and doesn't implement anything ArrayAccess-like
message: '/Instanceof between mixed and SimpleXMLElement will always evaluate to false\./'
path: library/Rules/ArrayVal.php
- message: '/Call to an undefined method .+::skip\(\)/'
path: tests/feature
- message: '/Call to an undefined method .+::expectException\(\)/'
path: tests/Pest.php
- message: '/Call to deprecated method optional\(\).+/'
path: tests/integration/transformers/aliases.phpt
path: tests/feature/Transformers/AliasesTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::(min|max)Age\(\)./'
path: tests/integration/transformers/deprecated_age.phpt
path: tests/feature/Transformers/DeprecatedAgeTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::attribute\(\)./'
path: tests/integration/transformers/deprecated_attribute.phpt
path: tests/feature/Transformers/DeprecatedAttributeTest.php
- message: '/Static method Respect\\Validation\\Mixins\\StaticValidator::key\(\) invoked with \d parameters?, 2 required/'
path: tests/integration/transformers/deprecated_key.phpt
path: tests/feature/Transformers/DeprecatedKeyTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::keyNested\(\)./'
path: tests/integration/transformers/deprecated_keyNested.phpt
path: tests/feature/Transformers/DeprecatedKeyNestedTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::keyValue\(\)./'
path: tests/integration/transformers/deprecated_keyValue.phpt
path: tests/feature/Transformers/DeprecatedKeyValueTest.php
- message: '/Parameter #1 \$rule of static method Respect\\Validation\\Mixins\\StaticValidator::length\(\) expects Respect\\Validation\\Rule.+/'
path: tests/integration/transformers/deprecated_length.phpt
path: tests/feature/Transformers/DeprecatedLengthTest.php
- message: '/Static method Respect\\Validation\\Mixins\\StaticValidator::length\(\) invoked with \d parameters, 1 required/'
path: tests/integration/transformers/deprecated_length.phpt
path: tests/feature/Transformers/DeprecatedLengthTest.php
- message: '/Parameter #1 \$rule of static method Respect\\Validation\\Mixins\\StaticValidator::max\(\) expects Respect\\Validation\\Rule.+/'
path: tests/integration/transformers/deprecated_max.phpt
path: tests/feature/Transformers/DeprecatedMaxTest.php
- message: '/Parameter #1 \$rule of static method Respect\\Validation\\Mixins\\StaticValidator::min\(\) expects Respect\\Validation\\Rule.+/'
path: tests/integration/transformers/deprecated_min.phpt
path: tests/feature/Transformers/DeprecatedMinTest.php
- message: '/Call to an undefined static method Respect\\Validation\\Validator::type\(\)./'
path: tests/integration/transformers/deprecated_type.phpt
path: tests/feature/Transformers/DeprecatedTypeTest.php
- message: '/Method .+\\TestingStringifier::stringify\(\) never returns null so it can be removed from the return type./'
path: tests/library/Message/TestingStringifier.php
- message: '/Parameter #1 \$messages of class .+\\ArrayTranslator constructor expects array<string, string>, array<string, int> given./'
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<testsuite name="unit">
<directory suffix="Test.php">tests/unit/</directory>
</testsuite>
<testsuite name="integration">
<directory suffix=".phpt">tests/integration/</directory>
<testsuite name="feature">
<directory suffix="Test.php">./tests/feature</directory>
</testsuite>
</testsuites>
<source>
Expand Down
104 changes: 104 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

use Respect\Validation\Exceptions\ValidationException;
use Symfony\Component\VarExporter\VarExporter;

use function PHPUnit\Framework\assertStringMatchesFormat;

/** @param array<string, mixed> $messages */
function expectAll(callable $callback, string $message, string $fullMessage, array $messages): Closure
{
return function () use ($callback, $message, $fullMessage, $messages): void {
try {
$callback();
test()->expectException(ValidationException::class);
} catch (ValidationException $e) {
expect($e->getMessage())->toBe($message)
->and($e->getFullMessage())->toBe($fullMessage)
->and($e->getMessages())->toBe($messages);
}
};
}

/** @param array<string, mixed> $messages */
function expectAllToMatch(callable $callback, string $message, string $fullMessage, array $messages): Closure
{
return function () use ($callback, $message, $fullMessage, $messages): void {
try {
$callback();
test()->expectException(ValidationException::class);
} catch (ValidationException $e) {
assertStringMatchesFormat($message, $e->getMessage(), 'Validation message does not match');
assertStringMatchesFormat($fullMessage, $e->getFullMessage(), 'Validation full message does not match');
assertStringMatchesFormat(
VarExporter::export($messages),
VarExporter::export($e->getMessages()),
'Validation messages do not match'
);
}
};
}

function expectMessage(callable $callback, string $message): Closure
{
return function () use ($callback, $message): void {
try {
$callback();
test()->expectException(ValidationException::class);
} catch (ValidationException $e) {
expect($e->getMessage())->toBe($message, 'Validation message does not match');
}
};
}

function expectFullMessage(callable $callback, string $fullMessage): Closure
{
return function () use ($callback, $fullMessage): void {
try {
$callback();
test()->expectException(ValidationException::class);
} catch (ValidationException $exception) {
expect($exception->getFullMessage())->toBe($fullMessage, 'Validation full message does not match');
}
};
}

/** @param array<string, mixed> $messages */
function expectMessages(callable $callback, array $messages): Closure
{
return function () use ($callback, $messages): void {
try {
$callback();
test()->expectException(ValidationException::class);
} catch (ValidationException $exception) {
expect($exception->getMessages())->toBe($messages, 'Validation messages do not match');
}
};
}

function expectMessageAndError(Closure $callback, string $message, string $error): Closure
{
return function () use ($callback, $message, $error): void {
$lastError = null;
set_error_handler(static function (int $errno, string $errstr) use (&$lastError): bool {
$lastError = $errstr;

return true;
});
try {
$callback();
test()->expectException(ValidationException::class);
} catch (ValidationException $e) {
expect($e->getMessage())->toBe($message, 'Validation message does not match');
}
restore_error_handler();
expect($lastError)->toBe($error);
};
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
--FILE--
<?php

require 'vendor/autoload.php';
/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

exceptionFullMessage(static function (): void {
v::create()
declare(strict_types=1);

test('Scenario #1', expectFullMessage(
fn() => v::create()
->key(
'mysql',
v::create()
Expand All @@ -31,18 +35,18 @@
'user' => 42,
'password' => 42,
],
]);
});
?>
--EXPECT--
- All of the required rules must pass for the given data
- All of the required rules must pass for mysql
- host must be a string
- user must be present
- password must be present
- schema must be a string
- All of the required rules must pass for postgresql
- host must be present
- user must be a string
- password must be a string
- schema must be present
]),
<<<'FULL_MESSAGE'
- All of the required rules must pass for the given data
- All of the required rules must pass for mysql
- host must be a string
- user must be present
- password must be present
- schema must be a string
- All of the required rules must pass for postgresql
- host must be present
- user must be a string
- password must be a string
- schema must be present
FULL_MESSAGE,
));
54 changes: 54 additions & 0 deletions tests/feature/AssertWithPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

test('Scenario #1', expectFullMessage(
function (): void {
$array = [
'mysql' => [
'host' => 42,
'user' => 'user',
'password' => 'password',
'schema' => 'schema',
],
'postgresql' => [
'host' => 'host',
'user' => 42,
'password' => 'password',
'schema' => 'schema',
],
];
$object = json_decode((string) json_encode($array));
v::create()
->property(
'mysql',
v::create()
->property('host', v::stringType())
->property('user', v::stringType())
->property('password', v::stringType())
->property('schema', v::stringType())
)
->property(
'postgresql',
v::create()
->property('host', v::stringType())
->property('user', v::stringType())
->property('password', v::stringType())
->property('schema', v::stringType())
)
->setName('the given data')
->assert($object);
},
<<<'FULL_MESSAGE'
- All of the required rules must pass for the given data
- These rules must pass for mysql
- host must be a string
- These rules must pass for postgresql
- user must be a string
FULL_MESSAGE,
));
Loading

0 comments on commit 01d41c7

Please sign in to comment.