forked from slimphp/Slim
/
SlimHttpServerRequestCreatorTest.php
74 lines (59 loc) · 2.72 KB
/
SlimHttpServerRequestCreatorTest.php
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
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests\Factory\Psr17;
use Psr\Http\Message\ServerRequestInterface;
use ReflectionProperty;
use Slim\Factory\Psr17\SlimHttpServerRequestCreator;
use Slim\Http\ServerRequest;
use Slim\Interfaces\ServerRequestCreatorInterface;
use Slim\Tests\TestCase;
class SlimHttpServerRequestCreatorTest extends TestCase
{
/**
* We need to reset the static property of SlimHttpServerRequestCreator back to its original value
* Otherwise other tests will fail
*/
public function tearDown()
{
$serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class);
$slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal());
$serverRequestDecoratorClassProperty = new ReflectionProperty(
SlimHttpServerRequestCreator::class,
'serverRequestDecoratorClass'
);
$serverRequestDecoratorClassProperty->setAccessible(true);
$serverRequestDecoratorClassProperty->setValue($slimHttpServerRequestCreator, ServerRequest::class);
}
public function testCreateServerRequestFromGlobals()
{
$serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
$serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class);
$serverRequestCreatorProphecy
->createServerRequestFromGlobals()
->willReturn($serverRequestProphecy->reveal())
->shouldBeCalledOnce();
$slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal());
$this->assertInstanceOf(ServerRequest::class, $slimHttpServerRequestCreator->createServerRequestFromGlobals());
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage The Slim-Http ServerRequest decorator is not available.
*/
public function testCreateServerRequestFromGlobalsThrowsRuntimeException()
{
$serverRequestCreatorProphecy = $this->prophesize(ServerRequestCreatorInterface::class);
$slimHttpServerRequestCreator = new SlimHttpServerRequestCreator($serverRequestCreatorProphecy->reveal());
$serverRequestDecoratorClassProperty = new ReflectionProperty(
SlimHttpServerRequestCreator::class,
'serverRequestDecoratorClass'
);
$serverRequestDecoratorClassProperty->setAccessible(true);
$serverRequestDecoratorClassProperty->setValue($slimHttpServerRequestCreator, '');
$slimHttpServerRequestCreator->createServerRequestFromGlobals();
}
}