Skip to content

Commit 95bb312

Browse files
authored
Added tests for server request (#12)
* Added tests for server request * cs
1 parent eac33b8 commit 95bb312

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

src/BaseTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Http\Psr7Test;
44

55
use GuzzleHttp\Psr7\Stream as GuzzleStream;
6+
use GuzzleHttp\Psr7\UploadedFile as GuzzleUploadedFile;
67
use GuzzleHttp\Psr7\Uri as GuzzleUri;
78
use Slim\Http\Uri as SlimUri;
89
use Zend\Diactoros\Stream as ZendStream;
910
use Zend\Diactoros\Uri as ZendUri;
11+
use Zend\Diactoros\UploadedFile as ZendUploadedFile;
1012

1113
/**
1214
* @author Tobias Nyholm <[email protected]>
@@ -67,4 +69,27 @@ protected function buildStream($data)
6769

6870
throw new \RuntimeException('Could not create Stream. Check your config');
6971
}
72+
73+
protected function buildUploadableFile($data)
74+
{
75+
if (defined('UPLOADED_FILE_FACTORY')) {
76+
$factoryClass = UPLOADED_FILE_FACTORY;
77+
$factory = new $factoryClass();
78+
if (!$factory instanceof \Interop\Http\Factory\UploadedFileFactoryInterface) {
79+
throw new \RuntimeException('Constant "UPLOADED_FILE_FACTORY" must be a reference to a Interop\Http\Factory\UploadedFileFactoryInterface');
80+
}
81+
82+
return $factory->createUploadedFile($data);
83+
}
84+
85+
if (class_exists(GuzzleUploadedFile::class)) {
86+
return new GuzzleUploadedFile($data, count($data), 0);
87+
}
88+
89+
if (class_exists(ZendUploadedFile::class)) {
90+
return new ZendUploadedFile($data, count($data), 0);
91+
}
92+
93+
throw new \RuntimeException('Could not create Stream. Check your config');
94+
}
7095
}

src/ServerRequestIntegrationTest.php

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public function testGetQueryParams()
6969
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
7070
}
7171

72-
// TODO write me
72+
$new = $this->serverRequest->withQueryParams(['foo' => 'bar']);
73+
$this->assertEmpty($this->serverRequest->getQueryParams(), 'withQueryParams MUST be immutable');
74+
75+
$this->assertArrayHasKey('foo', $new->getQueryParams());
7376
}
7477

7578
public function testGetUploadedFiles()
@@ -78,7 +81,13 @@ public function testGetUploadedFiles()
7881
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
7982
}
8083

81-
// TODO write me
84+
$file = $this->buildUploadableFile('foo');
85+
$new = $this->serverRequest->withUploadedFiles([$file]);
86+
$this->assertEmpty($this->serverRequest->getUploadedFiles(), 'withUploadedFiles MUST be immutable');
87+
88+
$files = $new->getUploadedFiles();
89+
$this->assertEquals(1, count($files));
90+
$this->assertEquals($file, $files[0]);
8291
}
8392

8493
public function testGetParsedBody()
@@ -87,7 +96,18 @@ public function testGetParsedBody()
8796
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
8897
}
8998

90-
// TODO write me
99+
$data = [
100+
4711,
101+
null,
102+
new \stdClass(),
103+
['foo' => 'bar', 'baz'],
104+
];
105+
106+
foreach ($data as $item) {
107+
$new = $this->serverRequest->withParsedBody($item);
108+
$this->assertNull($this->serverRequest->getParsedBody(), 'withParsedBody MUST be immutable');
109+
$this->assertEquals($item, $new->getParsedBody());
110+
}
91111
}
92112

93113
public function testGetAttributes()
@@ -96,6 +116,36 @@ public function testGetAttributes()
96116
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
97117
}
98118

99-
// TODO write me
119+
$new = $this->serverRequest->withAttribute('foo', 'bar');
120+
$this->assertNull($this->serverRequest->getAttributes(), 'withAttribute MUST be immutable');
121+
$this->assertEquals(['foo' => 'bar'], $new->getAttributes());
122+
123+
$new = $new->withAttribute('baz', 'biz');
124+
$this->assertEquals(['foo' => 'bar', 'baz' => 'biz'], $new->getAttributes());
125+
}
126+
127+
public function testGetAttribute()
128+
{
129+
if (isset($this->skippedTests[__FUNCTION__])) {
130+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
131+
}
132+
133+
$new = $this->serverRequest->withAttribute('foo', 'bar');
134+
$this->assertEquals('bar', $new->getAttribute('foo'));
135+
$this->assertEquals('baz', $new->getAttribute('not found', 'baz'));
136+
$this->assertEquals(null, $new->getAttribute('not found'));
137+
}
138+
139+
public function testWithoutAttribute()
140+
{
141+
if (isset($this->skippedTests[__FUNCTION__])) {
142+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
143+
}
144+
145+
$with = $this->serverRequest->withAttribute('foo', 'bar');
146+
$without = $with->withoutAttribute('foo');
147+
148+
$this->assertEquals('bar', $with->getAttribute('foo'), 'withoutAttribute MUST be immutable');
149+
$this->assertEquals(null, $without->getAttribute('foo'));
100150
}
101151
}

0 commit comments

Comments
 (0)