File: CurlTest.php

package info (click to toggle)
php-json-schema 6.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,216 kB
  • sloc: php: 9,403; makefile: 153; python: 28; sh: 13
file content (61 lines) | stat: -rw-r--r-- 1,909 bytes parent folder | download | duplicates (2)
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
<?php

namespace JsonSchema\Tests\Uri\Retrievers
{
    use JsonSchema\Uri\Retrievers\Curl;
    use PHPUnit\Framework\TestCase;

    class CurlTest extends TestCase
    {
        public function testRetrieveFile(): void
        {
            $c = new Curl();
            $result = $c->retrieve(realpath(__DIR__ . '/../../fixtures/foobar.json'));

            self::assertStringEqualsFileCanonicalizing(realpath(__DIR__ . '/../../fixtures/foobar.json'), $result);
        }

        public function testRetrieveNonexistantFile(): void
        {
            $c = new Curl();

            $this->expectException('\JsonSchema\Exception\ResourceNotFoundException');
            $this->expectExceptionMessage('JSON schema not found');

            $c->retrieve(__DIR__ . '/notARealFile');
        }

        public function testNoContentType(): void
        {
            $c = new Curl();
            $result = $c->retrieve(realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json');

            self::assertStringEqualsFileCanonicalizing(realpath(__DIR__ . '/../../fixtures/foobar.json'), $result);
        }
    }
}

namespace JsonSchema\Uri\Retrievers
{
    function curl_exec($curl)
    {
        $uri = curl_getinfo($curl, \CURLINFO_EFFECTIVE_URL);

        if ($uri === realpath(__DIR__ . '/../../fixtures/foobar.json')) {
            // return file with headers
            $headers = implode("\n", [
                'Content-Type: application/json'
            ]);

            return sprintf("%s\r\n\r\n%s", $headers, file_get_contents($uri));
        } elseif ($uri === realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json') {
            // return file without headers
            $uri = realpath(__DIR__ . '/../../fixtures/foobar.json');

            return "\r\n\r\n" . file_get_contents($uri);
        }

        // fallback to real curl_exec
        return \curl_exec($curl);
    }
}