Skip to content

Commit 102bf2e

Browse files
authored
Add test environments (#416)
1 parent 650e7fd commit 102bf2e

17 files changed

Lines changed: 184 additions & 200 deletions

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ jobs:
3131
['ubuntu-latest', 'windows-latest']
3232
php: >-
3333
['8.0', '8.1']
34-
phpunit_without_extensions:
34+
phpunit-without-intl:
3535
uses: yiisoft/actions/.github/workflows/phpunit.yml@master
3636
with:
37+
phpunitConfig: phpunit-without-intl.xml
38+
extensions: :intl
3739
coverage: xdebug
3840
os: >-
3941
['ubuntu-latest', 'windows-latest']

config/common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
$formatter = extension_loaded('intl')
3232
? new IntlMessageFormatter()
33-
: new SimpleMessageFormatter(); // @codeCoverageIgnore
33+
: new SimpleMessageFormatter();
3434

3535
return new CategorySource($params['yiisoft/validator']['translation.category'], $reader, $formatter);
3636
},

phpunit-without-intl.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit colors="true"
4+
verbose="true"
5+
failOnRisky="true"
6+
failOnWarning="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
executionOrder="random"
12+
resolveDependencies="true">
13+
<php>
14+
<ini name="error_reporting" value="-1"/>
15+
</php>
16+
17+
<testsuites>
18+
<testsuite name="Validator">
19+
<directory>./tests/TestEnvironments/WithoutIntl</directory>
20+
</testsuite>
21+
</testsuites>
22+
<coverage>
23+
<include>
24+
<directory>./src</directory>
25+
<directory>./config</directory>
26+
</include>
27+
</coverage>
28+
</phpunit>

phpunit.xml.dist

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
<testsuites>
1818
<testsuite name="Validator">
1919
<directory>./tests</directory>
20-
<exclude>./tests/Php81</exclude>
20+
<exclude>
21+
<directory>./tests/TestEnvironments</directory>
22+
</exclude>
2123
</testsuite>
2224

2325
<testsuite name="Validator PHP 8.1">
24-
<directory phpVersion="8.1" phpVersionOperator=">=">./tests/Php81</directory>
26+
<directory phpVersion="8.1" phpVersionOperator=">=">./tests/TestEnvironments/Php81</directory>
2527
</testsuite>
2628
</testsuites>
2729
<coverage>

tests/BaseConfigTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Di\Container;
9+
use Yiisoft\Di\ContainerConfig;
10+
11+
use function dirname;
12+
13+
abstract class BaseConfigTest extends TestCase
14+
{
15+
final protected function createContainer(array|null $params = null): Container
16+
{
17+
$config = ContainerConfig::create()->withDefinitions($this->getCommonDefinitions($params));
18+
19+
return new Container($config);
20+
}
21+
22+
final protected function getCommonDefinitions(array|null $params): array
23+
{
24+
if ($params === null) {
25+
$params = $this->getParams();
26+
}
27+
28+
return require dirname(__DIR__) . '/config/common.php';
29+
}
30+
31+
final protected function getParams(): array
32+
{
33+
return require dirname(__DIR__) . '/config/params.php';
34+
}
35+
}

tests/ConfigTest.php

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@
44

55
namespace Yiisoft\Validator\Tests;
66

7-
use PHPUnit\Framework\TestCase;
8-
use Yiisoft\Di\Container;
9-
use Yiisoft\Di\ContainerConfig;
107
use Yiisoft\Translator\CategorySource;
118
use Yiisoft\Translator\SimpleMessageFormatter;
129
use Yiisoft\Validator\RuleHandlerResolverInterface;
1310
use Yiisoft\Validator\SimpleRuleHandlerContainer;
1411
use Yiisoft\Validator\Validator;
1512
use Yiisoft\Validator\ValidatorInterface;
1613

17-
use function dirname;
18-
use function extension_loaded;
19-
20-
final class ConfigTest extends TestCase
14+
final class ConfigTest extends BaseConfigTest
2115
{
2216
public function testBase(): void
2317
{
@@ -52,42 +46,6 @@ public function testCustomTranslationCategory(): void
5246
$this->assertSame('yii-validator-custom', $translationCategorySource->getName());
5347
}
5448

55-
public function testIntlMessageFormatter(): void
56-
{
57-
if (!extension_loaded('intl')) {
58-
$this->markTestSkipped('The intl extension must be available for this test.');
59-
}
60-
61-
$container = $this->createContainer();
62-
63-
/** @var CategorySource $translationCategorySource */
64-
$translationCategorySource = $container->get('[email protected]')[0];
65-
$message = '{n, selectordinal, one{#-one} two{#-two} few{#-few} other{#-other}}';
66-
// The default formatter argument is ignored in favor of formatter set in config.
67-
$this->assertSame(
68-
'1-one',
69-
$translationCategorySource->format($message, ['n' => 1], 'en', new SimpleMessageFormatter()),
70-
);
71-
}
72-
73-
public function testSimpleMessageFormatter(): void
74-
{
75-
if (extension_loaded('intl')) {
76-
$this->markTestSkipped('The intl extension must be unavailable for this test.');
77-
}
78-
79-
$container = $this->createContainer();
80-
81-
/** @var CategorySource $translationCategorySource */
82-
$translationCategorySource = $container->get('[email protected]')[0];
83-
$message = '{n, selectordinal, one{#-one} two{#-two} few{#-few} other{#-other}}';
84-
// The default formatter argument is ignored in favor of formatter set in config.
85-
$this->assertSame(
86-
'1',
87-
$translationCategorySource->format($message, ['n' => 1], 'en', new SimpleMessageFormatter()),
88-
);
89-
}
90-
9149
public function testTranslationCategorySource(): void
9250
{
9351
$container = $this->createContainer();
@@ -102,24 +60,17 @@ public function testTranslationCategorySource(): void
10260
$this->assertSame('Значение неверно.', $translationCategorySource->getMessage('This value is invalid.', 'ru'));
10361
}
10462

105-
private function createContainer(array|null $params = null): Container
106-
{
107-
$config = ContainerConfig::create()->withDefinitions($this->getCommonDefinitions($params));
108-
109-
return new Container($config);
110-
}
111-
112-
private function getCommonDefinitions(array|null $params): array
63+
public function testIntlMessageFormatter(): void
11364
{
114-
if ($params === null) {
115-
$params = $this->getParams();
116-
}
117-
118-
return require dirname(__DIR__) . '/config/common.php';
119-
}
65+
$container = $this->createContainer();
12066

121-
private function getParams(): array
122-
{
123-
return require dirname(__DIR__) . '/config/params.php';
67+
/** @var CategorySource $translationCategorySource */
68+
$translationCategorySource = $container->get('[email protected]')[0];
69+
$message = '{n, selectordinal, one{#-one} two{#-two} few{#-few} other{#-other}}';
70+
// The default formatter argument is ignored in favor of formatter set in config.
71+
$this->assertSame(
72+
'1-one',
73+
$translationCategorySource->format($message, ['n' => 1], 'en', new SimpleMessageFormatter()),
74+
);
12475
}
12576
}

tests/Rule/Base/RuleTestCase.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ abstract public function dataValidationPassed(): array;
1616
*/
1717
public function testValidationPassed(mixed $data, array $rules): void
1818
{
19-
$this->beforeTestValidationPassed();
20-
2119
$result = ValidatorFactory::make()->validate($data, $rules);
2220

2321
$this->assertTrue($result->isValid());
@@ -30,19 +28,9 @@ abstract public function dataValidationFailed(): array;
3028
*/
3129
public function testValidationFailed(mixed $data, array|null $rules, array $errorMessagesIndexedByPath): void
3230
{
33-
$this->beforeTestValidationFailed();
34-
3531
$result = ValidatorFactory::make()->validate($data, $rules);
3632

3733
$this->assertFalse($result->isValid());
3834
$this->assertSame($errorMessagesIndexedByPath, $result->getErrorMessagesIndexedByPath());
3935
}
40-
41-
protected function beforeTestValidationPassed(): void
42-
{
43-
}
44-
45-
protected function beforeTestValidationFailed(): void
46-
{
47-
}
4836
}

tests/Rule/Base/RuleWithOptionsTestTrait.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ abstract public function dataOptions(): array;
1515
*/
1616
public function testOptions(RuleWithOptionsInterface $rule, array $expectedOptions): void
1717
{
18-
$this->beforeTestOptions();
19-
2018
$options = $rule->getOptions();
2119
$this->assertSame($expectedOptions, $options);
2220
}
23-
24-
protected function beforeTestOptions(): void
25-
{
26-
}
2721
}

tests/Rule/EmailTest.php

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44

55
namespace Yiisoft\Validator\Tests\Rule;
66

7-
use RuntimeException;
87
use Yiisoft\Validator\Rule\Email;
98
use Yiisoft\Validator\Rule\EmailHandler;
109
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
1110
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
1211
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
13-
1412
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
1513
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
1614

17-
18-
use function extension_loaded;
19-
2015
final class EmailTest extends RuleTestCase
2116
{
2217
use DifferentRuleInHandlerTestTrait;
@@ -32,10 +27,6 @@ public function testGetName(): void
3227

3328
public function dataOptions(): array
3429
{
35-
if (!extension_loaded('intl')) {
36-
return [];
37-
}
38-
3930
return [
4031
[
4132
new Email(),
@@ -126,10 +117,6 @@ public function dataOptions(): array
126117

127118
public function dataValidationPassed(): array
128119
{
129-
if (!extension_loaded('intl')) {
130-
return [];
131-
}
132-
133120
$rule = new Email();
134121
$ruleAllowedName = new Email(allowName: true);
135122
$ruleEnabledIDN = new Email(enableIDN: true);
@@ -197,10 +184,6 @@ public function dataValidationPassed(): array
197184

198185
public function dataValidationFailed(): array
199186
{
200-
if (!extension_loaded('intl')) {
201-
return [];
202-
}
203-
204187
$rule = new Email();
205188
$ruleAllowedName = new Email(allowName: true);
206189
$ruleEnabledIDN = new Email(enableIDN: true);
@@ -335,16 +318,6 @@ public function dataValidationFailed(): array
335318
];
336319
}
337320

338-
public function testEnableIdnWithMissingIntlExtension(): void
339-
{
340-
if (extension_loaded('intl')) {
341-
$this->markTestSkipped('The intl extension must be unavailable for this test.');
342-
}
343-
344-
$this->expectException(RuntimeException::class);
345-
new Email(enableIDN: true);
346-
}
347-
348321
public function testSkipOnError(): void
349322
{
350323
$this->testSkipOnErrorInternal(new Email(), new Email(skipOnError: true));
@@ -356,27 +329,6 @@ public function testWhen(): void
356329
$this->testWhenInternal(new Email(), new Email(when: $when));
357330
}
358331

359-
protected function beforeTestOptions(): void
360-
{
361-
if (!extension_loaded('intl')) {
362-
$this->markTestSkipped('The intl extension must be available for this test.');
363-
}
364-
}
365-
366-
protected function beforeTestValidationPassed(): void
367-
{
368-
if (!extension_loaded('intl')) {
369-
$this->markTestSkipped('The intl extension must be available for this test.');
370-
}
371-
}
372-
373-
protected function beforeTestValidationFailed(): void
374-
{
375-
if (!extension_loaded('intl')) {
376-
$this->markTestSkipped('The intl extension must be available for this test.');
377-
}
378-
}
379-
380332
protected function getDifferentRuleInHandlerItems(): array
381333
{
382334
return [Email::class, EmailHandler::class];

0 commit comments

Comments
 (0)