-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApiContextStepPatternTest.php
More file actions
79 lines (66 loc) · 2.81 KB
/
Copy pathApiContextStepPatternTest.php
File metadata and controls
79 lines (66 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace BehatApiContext\Tests\Unit\Context\Api;
use Behat\Behat\Definition\Pattern\PatternTransformer;
use Behat\Behat\Definition\Pattern\Policy\RegexPatternPolicy;
use Behat\Behat\Definition\Pattern\Policy\TurnipPatternPolicy;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
final class ApiContextStepPatternTest extends TestCase
{
private PatternTransformer $patternTransformer;
protected function setUp(): void
{
parent::setUp();
$this->patternTransformer = new PatternTransformer();
$this->patternTransformer->registerPatternPolicy(new RegexPatternPolicy());
$this->patternTransformer->registerPatternPolicy(new TurnipPatternPolicy());
}
public function testRequestHeaderStepMatchesContentTypeAndJsonMime(): void
{
$pattern = $this->givenPatternForMethod('theRequestHeaderContains');
$step = 'the "Content-Type" request header contains "application/json"';
$match = $this->matchStep($pattern, $step);
$this->assertIsArray($match);
$this->assertSame('Content-Type', $match['header']);
$this->assertSame('application/json', $match['value']);
}
public function testRequestHeaderStepDoesNotMatchUnquotedJsonMime(): void
{
$pattern = $this->givenPatternForMethod('theRequestHeaderContains');
$step = 'the Content-Type request header contains application/json';
$this->assertFalse($this->matchStep($pattern, $step));
}
public function testResponseHeaderStepMatchesQuotedNames(): void
{
$pattern = $this->givenPatternForMethod('theResponseHeadersContains');
$step = 'the "Content-Type" response headers contains "application/json; charset=UTF-8"';
$match = $this->matchStep($pattern, $step);
$this->assertIsArray($match);
$this->assertSame('Content-Type', $match['headerName']);
$this->assertSame('application/json; charset=UTF-8', $match['headerValue']);
}
/**
* @return array<int|string, string>|false
*/
private function matchStep(string $pattern, string $stepText): array|false
{
$regex = $this->patternTransformer->transformPatternToRegex($pattern);
if (preg_match($regex, $stepText, $matches) !== 1) {
return false;
}
return $matches;
}
private function givenPatternForMethod(string $methodName): string
{
$reflectionMethod = new ReflectionMethod(
\BehatApiContext\Context\ApiContext::class,
$methodName
);
$doc = (string) $reflectionMethod->getDocComment();
if (preg_match('/@Given\\s+(.+)/i', $doc, $m) || preg_match('/@Then\\s+(.+)/i', $doc, $m)) {
return trim($m[1]);
}
$this->fail('No @Given/@Then pattern in docblock for ' . $methodName);
}
}