|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | | -/* |
6 | | - * This file is part of the JsonSchema package. |
7 | | - * |
8 | | - * For the full copyright and license information, please view the LICENSE |
9 | | - * file that was distributed with this source code. |
10 | | - */ |
11 | | - |
12 | 5 | namespace JsonSchema\Tests\Constraints; |
13 | 6 |
|
| 7 | +use JsonSchema\UriRetrieverInterface; |
14 | 8 | use PHPUnit\Framework\TestCase; |
15 | 9 | use Prophecy\Argument; |
| 10 | +use stdClass; |
16 | 11 |
|
17 | | -/** |
18 | | - * @package JsonSchema\Tests\Constraints |
19 | | - */ |
20 | 12 | abstract class VeryBaseTestCase extends TestCase |
21 | 13 | { |
22 | | - /** @var object */ |
23 | | - private $jsonSchemaDraft03; |
| 14 | + private const DRAFT_SCHEMA_DIR = __DIR__ . '/../../dist/schema/'; |
| 15 | + private const TEST_SUITE_REMOTES = __DIR__ . '/../../vendor/json-schema/json-schema-test-suite/remotes'; |
24 | 16 |
|
25 | | - /** @var object */ |
26 | | - private $jsonSchemaDraft04; |
| 17 | + /** @var array<string, stdClass> */ |
| 18 | + private $draftSchemas = []; |
27 | 19 |
|
28 | 20 | protected function getUriRetrieverMock(?object $schema): object |
29 | 21 | { |
30 | | - $relativeTestsRoot = realpath(__DIR__ . '/../../vendor/json-schema/json-schema-test-suite/remotes'); |
31 | | - |
32 | | - $jsonSchemaDraft03 = $this->getJsonSchemaDraft03(); |
33 | | - $jsonSchemaDraft04 = $this->getJsonSchemaDraft04(); |
34 | | - |
35 | | - $uriRetriever = $this->prophesize('JsonSchema\UriRetrieverInterface'); |
| 22 | + $uriRetriever = $this->prophesize(UriRetrieverInterface::class); |
36 | 23 | $uriRetriever->retrieve('http://www.my-domain.com/schema.json') |
37 | 24 | ->willReturn($schema) |
38 | 25 | ->shouldBeCalled(); |
39 | 26 |
|
| 27 | + $that = $this; |
40 | 28 | $uriRetriever->retrieve(Argument::any()) |
41 | | - ->will(function ($args) use ($jsonSchemaDraft03, $jsonSchemaDraft04, $relativeTestsRoot) { |
42 | | - if ('http://json-schema.org/draft-03/schema' === $args[0]) { |
43 | | - return $jsonSchemaDraft03; |
44 | | - } elseif ('http://json-schema.org/draft-04/schema' === $args[0]) { |
45 | | - return $jsonSchemaDraft04; |
46 | | - } elseif (0 === strpos($args[0], 'http://localhost:1234')) { |
47 | | - $urlParts = parse_url($args[0]); |
48 | | - |
49 | | - return json_decode(file_get_contents($relativeTestsRoot . $urlParts['path'])); |
50 | | - } elseif (0 === strpos($args[0], 'http://www.my-domain.com')) { |
51 | | - $urlParts = parse_url($args[0]); |
52 | | - |
53 | | - return json_decode(file_get_contents($relativeTestsRoot . '/folder' . $urlParts['path'])); |
| 29 | + ->will(function ($args) use ($that): stdClass { |
| 30 | + if (strpos($args[0], 'http://json-schema.org/draft-03/schema') === 0) { |
| 31 | + return $that->getDraftSchema('json-schema-draft-03.json'); |
| 32 | + } |
| 33 | + |
| 34 | + if (strpos($args[0], 'http://json-schema.org/draft-04/schema') === 0) { |
| 35 | + return $that->getDraftSchema('json-schema-draft-04.json'); |
| 36 | + } |
| 37 | + if (strpos($args[0], 'http://json-schema.org/draft-06/schema') === 0) { |
| 38 | + return $that->getDraftSchema('json-schema-draft-06.json'); |
54 | 39 | } |
| 40 | + |
| 41 | + $urlParts = parse_url($args[0]); |
| 42 | + |
| 43 | + if (0 === strpos($args[0], 'http://localhost:1234')) { |
| 44 | + return $that->readAndJsonDecodeFile(self::TEST_SUITE_REMOTES . $urlParts['path']); |
| 45 | + } |
| 46 | + |
| 47 | + if (0 === strpos($args[0], 'http://www.my-domain.com')) { |
| 48 | + return $that->readAndJsonDecodeFile(self::TEST_SUITE_REMOTES . '/folder' . $urlParts['path']); |
| 49 | + } |
| 50 | + |
| 51 | + throw new \InvalidArgumentException(sprintf('No handling for %s has been setup', $args[0])); |
55 | 52 | }); |
56 | 53 |
|
57 | 54 | return $uriRetriever->reveal(); |
58 | 55 | } |
59 | 56 |
|
60 | | - private function getJsonSchemaDraft03(): object |
| 57 | + private function getDraftSchema(string $draft): stdClass |
61 | 58 | { |
62 | | - if (!$this->jsonSchemaDraft03) { |
63 | | - $this->jsonSchemaDraft03 = json_decode( |
64 | | - file_get_contents(__DIR__ . '/../../dist/schema/json-schema-draft-03.json') |
65 | | - ); |
| 59 | + if (!array_key_exists($draft, $this->draftSchemas)) { |
| 60 | + $this->draftSchemas[$draft] = $this->readAndJsonDecodeFile(self::DRAFT_SCHEMA_DIR . '/' . $draft); |
66 | 61 | } |
67 | 62 |
|
68 | | - return $this->jsonSchemaDraft03; |
| 63 | + return $this->draftSchemas[$draft]; |
69 | 64 | } |
70 | 65 |
|
71 | | - private function getJsonSchemaDraft04(): object |
| 66 | + private function readAndJsonDecodeFile(string $file): stdClass |
72 | 67 | { |
73 | | - if (!$this->jsonSchemaDraft04) { |
74 | | - $this->jsonSchemaDraft04 = json_decode( |
75 | | - file_get_contents(__DIR__ . '/../../dist/schema/json-schema-draft-04.json') |
76 | | - ); |
| 68 | + if (!file_exists($file)) { |
| 69 | + throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $file)); |
77 | 70 | } |
78 | 71 |
|
79 | | - return $this->jsonSchemaDraft04; |
| 72 | + return json_decode(file_get_contents($file), false); |
80 | 73 | } |
81 | 74 | } |
0 commit comments