Skip to content

Commit 4a455c3

Browse files
authored
Merge pull request #1 from membrane-php/add-infection
Add infection/infection
2 parents 9abd287 + c0add5f commit 4a455c3

7 files changed

Lines changed: 90 additions & 33 deletions

File tree

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"phpunit/phpunit": "^10.1",
1818
"phpstan/phpstan": "^1.10.19",
1919
"squizlabs/php_codesniffer": "^3.7",
20-
"mikey179/vfsstream": "^1.6.7"
20+
"mikey179/vfsstream": "^1.6.7",
21+
"infection/infection": "^0.27.0"
22+
},
23+
"config": {
24+
"allow-plugins": {
25+
"infection/extension-installer": true
26+
}
2127
}
2228
}

infection.json5

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "vendor/infection/infection/resources/schema.json",
3+
"source": {
4+
"directories": [
5+
"src"
6+
]
7+
},
8+
minCoveredMsi: 90,
9+
minMsi: 80,
10+
"mutators": {
11+
"@default": true,
12+
},
13+
}

src/Exception/CannotSupport.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ final class CannotSupport extends RuntimeException
1717

1818
public static function unsupportedMethod(string $pathUrl, string $method): self
1919
{
20-
$message = sprintf(
21-
"Membrane does not currently support the method: '%s'\n" .
22-
'Found on Path: "%s"',
23-
$method,
24-
$pathUrl
25-
);
20+
$message = <<<TEXT
21+
Membrane does not currently support the method: '$method'.
22+
Found on Path: '$pathUrl'
23+
TEXT;
2624
return new self($message, self::UNSUPPORTED_METHOD);
2725
}
2826

@@ -40,14 +38,12 @@ public static function unsupportedVersion(string $version): self
4038

4139
public static function missingOperationId(string $pathUrl, string $method): self
4240
{
43-
$message = sprintf(
44-
"Membrane requires an operationId on all operations.\n" .
45-
"operationId is missing for the following operation:\n" .
46-
"\tPath: '%s'\n" .
47-
"\tMethod: '%s'",
48-
$pathUrl,
49-
$method
50-
);
41+
$message = <<<TEXT
42+
Membrane requires an operationId on all operations.
43+
operationId is missing for the following operation:
44+
Path: '$pathUrl'
45+
Method: '$method'
46+
TEXT;
5147
return new self($message, self::MISSING_OPERATION_ID);
5248
}
5349
}

src/Exception/InvalidOpenAPI.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ public static function duplicateOperationIds(
2020
string $secondPath,
2121
string $secondMethod
2222
): self {
23-
$message = sprintf(
24-
"A valid OpenAPI must contain unique operationIds across the API.\n" .
25-
"The operationId: '%s' is duplicated between the following operations:\n" .
26-
"\tPath: '%s', Method: '%s'\n" .
27-
"\tPath: '%s', Method: '%s'\n",
28-
$operationId,
29-
$firstPath,
30-
$firstMethod,
31-
$secondPath,
32-
$secondMethod
33-
);
23+
$message = <<<TEXT
24+
A valid OpenAPI must contain unique operationIds across the API.
25+
The operationId: '$operationId' is duplicated between the following operations:
26+
Path: '$firstPath', Method: '$firstMethod'.
27+
Path: '$secondPath', Method: '$secondMethod'.
28+
TEXT;
3429

3530
return new self($message, self::INVALID_OPEN_API);
3631
}

src/Reader.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@ public function readFromString(string $openAPI, FileFormat $fileFormat): CebeSpe
5959
throw CannotRead::invalidFormatting($e);
6060
}
6161

62-
$this->validate($openAPI);
63-
64-
6562
$openAPI->resolveReferences(new Cebe\ReferenceContext($openAPI, '/tmp'));
6663

64+
$this->validate($openAPI);
6765

6866
return $openAPI;
6967
}

tests/FileFormatTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ class FileFormatTest extends TestCase
1818
public static function provideSupportedFileExtensions(): Generator
1919
{
2020
yield 'json' => [FileFormat::Json, 'json'];
21+
yield 'JSON' => [FileFormat::Json, 'JSON'];
2122
yield 'yaml' => [FileFormat::Yaml, 'yaml'];
23+
yield 'YAML' => [FileFormat::Yaml, 'YAML'];
2224
yield 'yml' => [FileFormat::Yaml, 'yml'];
25+
yield 'YML' => [FileFormat::Yaml, 'YML'];
2326
}
2427

2528
#[Test, TestDox('it supports file extensions used for OpenAPI')]

tests/ReaderTest.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function itMustHaveAnArrayContainingOnlyOpenAPIVersions(): void
4040
#[Test]
4141
public function itCannotReadFilesItCannotFind(): void
4242
{
43-
$filePath = vfsStream::setup()->url() . '/open-api';
43+
$filePath = vfsStream::setup()->url() . '/openapi';
4444

4545
self::assertFalse(file_exists($filePath));
4646

@@ -72,7 +72,21 @@ public static function provideMinimalOpenAPIAsArray(): Generator
7272

7373
#[Test]
7474
#[DataProvider('provideMinimalOpenAPIAsArray')]
75-
public function itCannotSupportUnspecifiedOpenAPIVersions(array $openAPIArray): void
75+
public function itCannotSupportUnspecifiedOpenAPIVersionsFromAbsoluteFilePath(array $openAPIArray): void
76+
{
77+
78+
$filePath = vfsStream::setup()->url() . '/openapi';
79+
file_put_contents($filePath, json_encode($openAPIArray));
80+
81+
self::expectExceptionObject(CannotSupport::unsupportedVersion($openAPIArray['openapi']));
82+
83+
(new Reader([OpenAPIVersion::Version_3_1]))
84+
->readFromAbsoluteFilePath($filePath, FileFormat::Json);
85+
}
86+
87+
#[Test]
88+
#[DataProvider('provideMinimalOpenAPIAsArray')]
89+
public function itCannotSupportUnspecifiedOpenAPIVersionsFromString(array $openAPIArray): void
7690
{
7791
self::expectExceptionObject(CannotSupport::unsupportedVersion($openAPIArray['openapi']));
7892

@@ -82,7 +96,20 @@ public function itCannotSupportUnspecifiedOpenAPIVersions(array $openAPIArray):
8296

8397
#[Test]
8498
#[DataProvider('provideMinimalOpenAPIAsArray')]
85-
public function itCanSupportSpecifiedOpenAPIVersions(array $openAPIArray): void
99+
public function itCanSupportSpecifiedOpenAPIVersionsFromAbsoluteFilePath(array $openAPIArray): void
100+
{
101+
$filePath = vfsStream::setup()->url() . '/openapi';
102+
file_put_contents($filePath, json_encode($openAPIArray));
103+
104+
$openAPIObject = (new Reader([OpenAPIVersion::Version_3_0]))
105+
->readFromAbsoluteFilePath($filePath, FileFormat::Json);
106+
107+
self::assertInstanceOf(CebeSpec\OpenApi::class, $openAPIObject);
108+
}
109+
110+
#[Test]
111+
#[DataProvider('provideMinimalOpenAPIAsArray')]
112+
public function itCanSupportSpecifiedOpenAPIVersionsFromString(array $openAPIArray): void
86113
{
87114
$openAPIObject = (new Reader([OpenAPIVersion::Version_3_0]))
88115
->readFromString(json_encode($openAPIArray), FileFormat::Json);
@@ -107,6 +134,10 @@ public static function provideInvalidFormatting(): Generator
107134
yield 'Empty string to be interpreted as yaml' => ['', FileFormat::Yaml];
108135
yield 'Invalid json format' => ['{openapi: ",', FileFormat::Json];
109136
yield 'Invalid yaml format' => ['---openapi: ",- title: "invalid"', FileFormat::Yaml];
137+
yield 'info is a string rather than an array' => [
138+
json_encode(['openapi' => '3.0.0', 'info' => 'hold on what is this?', 'paths' => []]),
139+
FileFormat::Json
140+
];
110141
}
111142

112143
#[Test]
@@ -179,7 +210,22 @@ public static function provideInvalidOpenAPIs(): Generator
179210

180211
#[Test]
181212
#[DataProvider('provideInvalidOpenAPIs')]
182-
public function itWillNotProcessInvalidOpenAPI(string $openAPIString, InvalidOpenAPI $expectedException): void
213+
public function itWillNotProcessInvalidOpenAPIFromAbsoluteFilePath(
214+
string $openAPIString,
215+
InvalidOpenAPI $expectedException
216+
): void {
217+
$filePath = vfsStream::setup()->url() . '/openapi.json';
218+
file_put_contents($filePath, $openAPIString);
219+
220+
self::expectExceptionObject($expectedException);
221+
222+
(new Reader([OpenAPIVersion::Version_3_0]))
223+
->readFromAbsoluteFilePath($filePath, FileFormat::Json);
224+
}
225+
226+
#[Test]
227+
#[DataProvider('provideInvalidOpenAPIs')]
228+
public function itWillNotProcessInvalidOpenAPIFromString(string $openAPIString, InvalidOpenAPI $expectedException): void
183229
{
184230
self::expectExceptionObject($expectedException);
185231

0 commit comments

Comments
 (0)