Skip to content

Commit 3535817

Browse files
author
Kirill Nesmeyanov
committed
Add more feature tests and remove unit type tests
1 parent c3248d8 commit 3535817

25 files changed

+444
-1164
lines changed

behat.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ default:
55
- '%paths.base%/tests/Feature'
66
contexts:
77
# Executors
8-
- TypeLang\Mapper\Tests\Context\Execution\MapperContext
8+
- TypeLang\Mapper\Tests\Context\Execution\MapTypeContext
99
# Providers
1010
- TypeLang\Mapper\Tests\Context\Provider\ConfigurationContext
11+
- TypeLang\Mapper\Tests\Context\Provider\MappingContext
1112
- TypeLang\Mapper\Tests\Context\Provider\MetadataContext
1213
- TypeLang\Mapper\Tests\Context\Provider\PlatformContext
1314
- TypeLang\Mapper\Tests\Context\Provider\TypeContext
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Tests\Context\Execution;
6+
7+
use Behat\Gherkin\Node\TableNode;
8+
use Behat\Step\When;
9+
use PHPUnit\Framework\Assert;
10+
use PHPUnit\Framework\ExpectationFailedException;
11+
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
12+
use TypeLang\Mapper\Tests\Context\Context;
13+
use TypeLang\Mapper\Tests\Context\Provider\MappingContext;
14+
use TypeLang\Mapper\Tests\Context\Provider\TypeContext;
15+
16+
/**
17+
* @api
18+
* @see http://behat.org/en/latest/quick_start.html
19+
*/
20+
final class MapTypeContext extends Context
21+
{
22+
private static function assertCompareWithCode(string $expectedCode, mixed $actualValue, string $message = ''): void
23+
{
24+
if (\str_starts_with($expectedCode, '<error:') && \str_ends_with($expectedCode, '>')) {
25+
$expectedCode = \trim(\substr($expectedCode, 7, -1));
26+
27+
Assert::assertInstanceOf(InvalidValueException::class, $actualValue);
28+
Assert::assertStringContainsString($expectedCode, $actualValue->getMessage());
29+
30+
return;
31+
}
32+
33+
if ($actualValue instanceof \Throwable) {
34+
throw $actualValue;
35+
}
36+
37+
$expectedValue = eval(\sprintf('return %s;', $expectedCode));
38+
39+
if (\is_float($expectedValue) && \is_nan($expectedValue)) {
40+
Assert::assertNan($actualValue, $message);
41+
} elseif (\is_object($expectedValue)) {
42+
Assert::assertEquals($expectedValue, $actualValue, $message);
43+
} else {
44+
Assert::assertSame($expectedValue, $actualValue, $message);
45+
}
46+
}
47+
48+
#[When('matching returns the following values:')]
49+
public function whenMatching(TableNode $table): void
50+
{
51+
$type = $this->from(TypeContext::class)
52+
->getCurrent();
53+
54+
foreach ($table->getRows() as [$inputCode, $expectedCode]) {
55+
$inputValue = eval('return ' . $inputCode . ';');
56+
$context = $this->from(MappingContext::class)
57+
->setContextByValue($inputValue);
58+
59+
$actualValue = $type->match($inputValue, $context);
60+
61+
self::assertCompareWithCode(
62+
expectedCode: $expectedCode,
63+
actualValue: $actualValue,
64+
message: \vsprintf('Type %s expects %s to be %s, got %s', [
65+
$type::class,
66+
$inputCode,
67+
$expectedCode,
68+
$actualValue ? 'true' : 'false',
69+
]),
70+
);
71+
}
72+
}
73+
74+
#[When('casting returns the following values:')]
75+
public function whenCasting(TableNode $table): void
76+
{
77+
$type = $this->from(TypeContext::class)
78+
->getCurrent();
79+
80+
foreach ($table->getRows() as [$inputCode, $expectedCode]) {
81+
$inputValue = eval('return ' . $inputCode . ';');
82+
$context = $this->from(MappingContext::class)
83+
->setContextByValue($inputValue);
84+
85+
try {
86+
$actualValue = $type->cast($inputValue, $context);
87+
} catch (\Throwable $e) {
88+
self::assertCompareWithCode($expectedCode, $e);
89+
90+
continue;
91+
}
92+
93+
self::assertCompareWithCode(
94+
expectedCode: $expectedCode,
95+
actualValue: $actualValue,
96+
message: \vsprintf('Type %s expects %s to be %s, got %s', [
97+
$type::class,
98+
$inputCode,
99+
$expectedCode,
100+
\get_debug_type($actualValue),
101+
]),
102+
);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)