Skip to content

Commit 86410c8

Browse files
committed
Add test for the NonEmptyStringValidator
And add phpunit.xml and add .phpunit.cache to .gitignore
1 parent ff72959 commit 86410c8

File tree

6 files changed

+96
-1
lines changed

6 files changed

+96
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.idea
2-
2+
.phpunit.cache
33
/vendor/

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/13.0/phpunit.xsd"
4+
cacheDirectory=".phpunit.cache"
5+
beStrictAboutOutputDuringTests="true"
6+
beStrictAboutTestsThatDoNotTestAnything="true"
7+
displayDetailsOnAllIssues="true"
8+
failOnPhpunitDeprecation="true"
9+
failOnRisky="true"
10+
failOnWarning="true"
11+
colors="true">
12+
<testsuites>
13+
<testsuite name="unit">
14+
<directory>tests/unit</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace thofman\KnowledgeBase\Domain\Question\Validation;
5+
6+
final readonly class NonEmptyStringValidator implements Validator
7+
{
8+
public function validate(string $value): ValidationResult
9+
{
10+
return ValidationResult::valid();
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace thofman\KnowledgeBase\Domain\Question\Validation;
5+
6+
final readonly class ValidationResult
7+
{
8+
public static function valid(): self
9+
{
10+
return new self(
11+
isValid: true,
12+
);
13+
}
14+
15+
public static function invalid(string $validationErrorMessage): self
16+
{
17+
return new self(
18+
isValid: false,
19+
validationErrorMessage: $validationErrorMessage,
20+
);
21+
}
22+
23+
private function __construct(
24+
public bool $isValid,
25+
public string $validationErrorMessage = '',
26+
) {
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace thofman\KnowledgeBase\Domain\Question\Validation;
5+
6+
use Symfony\Component\Console\Question\Question;
7+
8+
interface Validator
9+
{
10+
public function validate(string $value): ValidationResult;
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace thofman\KnowledgeBase\tests\unit\Domain\Question\Validation;
5+
6+
use Generator;
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use PHPUnit\Framework\TestCase;
10+
use thofman\KnowledgeBase\Domain\Question\Validation\NonEmptyStringValidator;
11+
use thofman\KnowledgeBase\Domain\Question\Validation\ValidationResult;
12+
13+
final class NonEmptyStringValidatorTest extends TestCase
14+
{
15+
#[Test]
16+
#[DataProvider('provideDataToTestTheNonEmptyStringValidator')]
17+
public function theNonEmptyStringValidator(string $value, ValidationResult $expectedResult): void
18+
{
19+
$validator = new NonEmptyStringValidator();
20+
self::assertEquals($expectedResult, $validator->validate($value));
21+
}
22+
23+
public static function provideDataToTestTheNonEmptyStringValidator(): Generator
24+
{
25+
yield ['test', ValidationResult::valid()];
26+
}
27+
}

0 commit comments

Comments
 (0)