Passed
Push — master ( 21cf99...2fc355 )
by William
10:56
created

classes/Http/Factory/ServerRequestFactoryTest.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Tests\Http\Factory;
6
7
use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory;
8
use Laminas\Diactoros\ServerRequestFactory as LaminasServerRequestFactory;
9
use Nyholm\Psr7\Factory\Psr17Factory as NyholmPsr17Factory;
10
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
11
use PhpMyAdmin\Http\ServerRequest;
12
use PhpMyAdmin\Tests\AbstractTestCase;
13
use Psr\Http\Message\ServerRequestFactoryInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Slim\Psr7\Factory\ServerRequestFactory as SlimServerRequestFactory;
0 ignored issues
show
The type Slim\Psr7\Factory\ServerRequestFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
use function class_exists;
18
19
/** @covers \PhpMyAdmin\Http\Factory\ServerRequestFactory */
20
class ServerRequestFactoryTest extends AbstractTestCase
21
{
22
    private const IMPLEMENTATION_CLASSES = [
23
        'slim/psr7' => [SlimServerRequestFactory::class, 'Slim PSR-7'],
24
        'guzzlehttp/psr7' => [GuzzleHttpFactory::class, 'Guzzle PSR-7'],
25
        'nyholm/psr7' => [NyholmPsr17Factory::class, 'Nyholm PSR-7'],
26
        'laminas/laminas-diactoros' => [LaminasServerRequestFactory::class, 'Laminas diactoros PSR-7'],
27
    ];
28
29
    /** @return mixed[][] */
30
    public static function dataProviderPsr7Implementations(): array
31
    {
32
        return self::IMPLEMENTATION_CLASSES;
33
    }
34
35
    /** @phpstan-param class-string $className */
36
    private function testOrSkip(string $className, string $humanName): void
37
    {
38
        if (! class_exists($className)) {
39
            $this->markTestSkipped($humanName . ' is missing');
40
        }
41
42
        foreach (self::IMPLEMENTATION_CLASSES as $libName => $details) {
43
            /** @phpstan-var class-string */
44
            $classImpl = $details[0];
45
            if ($classImpl === $className) {
46
                continue;
47
            }
48
49
            if (! class_exists($classImpl)) {
50
                continue;
51
            }
52
53
            $this->markTestSkipped($libName . ' exists and will conflict with the test results');
54
        }
55
    }
56
57
    /**
58
     * @phpstan-param class-string $className
59
     *
60
     * @dataProvider dataProviderPsr7Implementations
61
     */
62
    public function testPsr7ImplementationGet(string $className, string $humanName): void
63
    {
64
        $this->testOrSkip($className, $humanName);
65
66
        $_GET['foo'] = 'bar';
67
        $_GET['blob'] = 'baz';
68
        $_SERVER['QUERY_STRING'] = 'foo=bar&blob=baz';
69
        $_SERVER['REQUEST_URI'] = '/test-page.php';
70
        $_SERVER['REQUEST_METHOD'] = 'GET';
71
        $_SERVER['HTTP_HOST'] = 'phpmyadmin.local';
72
73
        $request = ServerRequestFactory::createFromGlobals();
74
        $this->assertSame(
75
            'GET',
76
            $request->getMethod(),
77
        );
78
        $this->assertSame(
79
            'http://phpmyadmin.local/test-page.php?foo=bar&blob=baz',
80
            $request->getUri()->__toString(),
81
        );
82
        $this->assertFalse(
83
            $request->isPost(),
84
        );
85
        $this->assertSame(
86
            'default',
87
            $request->getParam('not-exists', 'default'),
88
        );
89
        $this->assertSame(
90
            'bar',
91
            $request->getParam('foo'),
92
        );
93
        $this->assertSame(
94
            'baz',
95
            $request->getParam('blob'),
96
        );
97
        $this->assertSame(['foo' => 'bar', 'blob' => 'baz'], $request->getQueryParams());
98
    }
99
100
    public function testCreateServerRequestFromGlobals(): void
101
    {
102
        $_GET['foo'] = 'bar';
103
        $_GET['blob'] = 'baz';
104
        $_POST['input1'] = 'value1';
105
        $_POST['input2'] = 'value2';
106
        $_POST['input3'] = '';
107
        $_SERVER['QUERY_STRING'] = 'foo=bar&blob=baz';
108
        $_SERVER['REQUEST_URI'] = '/test-page.php';
109
        $_SERVER['REQUEST_METHOD'] = 'POST';
110
        $_SERVER['HTTP_HOST'] = 'phpmyadmin.local';
111
112
        $creator = $this->getMockBuilder(ServerRequestFactory::class)
113
            ->onlyMethods(['getallheaders'])
114
            ->getMock();
115
116
        $creator
117
            ->method('getallheaders')
118
            ->willReturn(['Content-Type' => 'application/x-www-form-urlencoded']);
119
120
        /** @var ServerRequestInterface $serverRequest */
121
        $serverRequest = $this->callFunction(
122
            $creator,
123
            ServerRequestFactory::class,
124
            'createServerRequestFromGlobals',
125
            [$creator],
126
        );
127
128
        $request = new ServerRequest($serverRequest);
129
130
        $this->assertSame(
131
            ['application/x-www-form-urlencoded'],
132
            $request->getHeader('Content-Type'),
133
        );
134
        $this->assertSame(
135
            'POST',
136
            $request->getMethod(),
137
        );
138
        $this->assertSame(
139
            'http://phpmyadmin.local/test-page.php?foo=bar&blob=baz',
140
            $request->getUri()->__toString(),
141
        );
142
        $this->assertTrue(
143
            $request->isPost(),
144
        );
145
        $this->assertSame(
146
            'default',
147
            $request->getParam('not-exists', 'default'),
148
        );
149
        $this->assertSame(
150
            'bar',
151
            $request->getParam('foo'),
152
        );
153
        $this->assertSame(
154
            'baz',
155
            $request->getParam('blob'),
156
        );
157
        $this->assertSame(['foo' => 'bar', 'blob' => 'baz'], $request->getQueryParams());
158
159
        $this->assertSame(['input1' => 'value1', 'input2' => 'value2', 'input3' => ''], $request->getParsedBody());
160
161
        $this->assertNull($request->getParsedBodyParam('foo'));
162
        $this->assertSame('value1', $request->getParsedBodyParam('input1'));
163
        $this->assertSame('value2', $request->getParsedBodyParam('input2'));
164
        $this->assertSame('', $request->getParsedBodyParam('input3', 'default'));
165
    }
166
167
    /**
168
     * @phpstan-param class-string $className
169
     *
170
     * @dataProvider dataProviderPsr7Implementations
171
     */
172
    public function testPsr7ImplementationCreateServerRequestFactory(string $className, string $humanName): void
173
    {
174
        $this->testOrSkip($className, $humanName);
175
176
        $serverRequestFactory = new $className();
177
        $this->assertInstanceOf(ServerRequestFactoryInterface::class, $serverRequestFactory);
178
179
        $factory = new ServerRequestFactory($serverRequestFactory);
180
        $this->assertInstanceOf(ServerRequestFactory::class, $factory);
181
    }
182
}
183