Skip to content

Commit 582470e

Browse files
committed
Refactored test methods
2 parents bbf9425 + 3b45c98 commit 582470e

File tree

6 files changed

+47
-73
lines changed

6 files changed

+47
-73
lines changed

tests/Doubles/DummyResponse.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getProtocolVersion(): string
2424
return '1.0';
2525
}
2626

27-
public function withProtocolVersion($version): ResponseInterface
27+
public function withProtocolVersion($version): self
2828
{
2929
return $this;
3030
}
@@ -49,17 +49,17 @@ public function getHeaderLine($name): string
4949
return '';
5050
}
5151

52-
public function withHeader($name, $value): ResponseInterface
52+
public function withHeader($name, $value): self
5353
{
5454
return $this;
5555
}
5656

57-
public function withAddedHeader($name, $value): ResponseInterface
57+
public function withAddedHeader($name, $value): self
5858
{
5959
return $this;
6060
}
6161

62-
public function withoutHeader($name): ResponseInterface
62+
public function withoutHeader($name): self
6363
{
6464
return $this;
6565
}
@@ -69,7 +69,7 @@ public function getBody(): StreamInterface
6969
return $this->body;
7070
}
7171

72-
public function withBody(StreamInterface $body): ResponseInterface
72+
public function withBody(StreamInterface $body): self
7373
{
7474
return $this;
7575
}
@@ -79,7 +79,7 @@ public function getStatusCode(): int
7979
return 200;
8080
}
8181

82-
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
82+
public function withStatus($code, $reasonPhrase = ''): self
8383
{
8484
return $this;
8585
}

tests/Doubles/DummyServerRequest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getProtocolVersion(): string
4141
return '1.0';
4242
}
4343

44-
public function withProtocolVersion($version): ServerRequestInterface
44+
public function withProtocolVersion($version): self
4545
{
4646
return $this;
4747
}
@@ -66,17 +66,17 @@ public function getHeaderLine($name): string
6666
return '';
6767
}
6868

69-
public function withHeader($name, $value): ServerRequestInterface
69+
public function withHeader($name, $value): self
7070
{
7171
return $this;
7272
}
7373

74-
public function withAddedHeader($name, $value): ServerRequestInterface
74+
public function withAddedHeader($name, $value): self
7575
{
7676
return $this;
7777
}
7878

79-
public function withoutHeader($name): ServerRequestInterface
79+
public function withoutHeader($name): self
8080
{
8181
return $this;
8282
}
@@ -86,22 +86,22 @@ public function getBody(): StreamInterface
8686
return $this->body;
8787
}
8888

89-
public function withBody(StreamInterface $body): ServerRequestInterface
89+
public function withBody(StreamInterface $body): self
9090
{
9191
return $this;
9292
}
9393

94-
public function withRequestTarget($requestTarget): ServerRequestInterface
94+
public function withRequestTarget($requestTarget): self
9595
{
9696
return $this;
9797
}
9898

99-
public function withMethod($method): ServerRequestInterface
99+
public function withMethod($method): self
100100
{
101101
return $this;
102102
}
103103

104-
public function withUri(UriInterface $uri, $preserveHost = false): ServerRequestInterface
104+
public function withUri(UriInterface $uri, $preserveHost = false): self
105105
{
106106
return $this;
107107
}
@@ -116,7 +116,7 @@ public function getCookieParams(): array
116116
return [];
117117
}
118118

119-
public function withCookieParams(array $cookies): ServerRequestInterface
119+
public function withCookieParams(array $cookies): self
120120
{
121121
return $this;
122122
}
@@ -126,7 +126,7 @@ public function getQueryParams(): array
126126
return [];
127127
}
128128

129-
public function withQueryParams(array $query): ServerRequestInterface
129+
public function withQueryParams(array $query): self
130130
{
131131
return $this;
132132
}
@@ -136,7 +136,7 @@ public function getUploadedFiles(): array
136136
return [];
137137
}
138138

139-
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
139+
public function withUploadedFiles(array $uploadedFiles): self
140140
{
141141
return $this;
142142
}
@@ -146,7 +146,7 @@ public function getParsedBody(): array
146146
return [];
147147
}
148148

149-
public function withParsedBody($data): ServerRequestInterface
149+
public function withParsedBody($data): self
150150
{
151151
return $this;
152152
}
@@ -161,12 +161,12 @@ public function getAttribute($name, $default = null)
161161
return $default;
162162
}
163163

164-
public function withAttribute($name, $value): ServerRequestInterface
164+
public function withAttribute($name, $value): self
165165
{
166166
return $this;
167167
}
168168

169-
public function withoutAttribute($name): ServerRequestInterface
169+
public function withoutAttribute($name): self
170170
{
171171
return $this;
172172
}

tests/Doubles/MockedMiddleware.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
use Psr\Http\Server\RequestHandlerInterface;
1616
use Psr\Http\Message\ServerRequestInterface;
1717
use Psr\Http\Message\ResponseInterface;
18-
use Polymorphine\Middleware\Tests\Fixtures\ExecutionOrder;
1918

2019

2120
class MockedMiddleware implements MiddlewareInterface
2221
{
23-
public static bool $instance = false;
22+
public static bool $instance = false;
23+
public static array $processedInstances = [];
2424

2525
private string $id;
2626

@@ -30,9 +30,15 @@ public function __construct(string $id)
3030
self::$instance = true;
3131
}
3232

33+
public static function reset(): void
34+
{
35+
self::$instance = false;
36+
self::$processedInstances = [];
37+
}
38+
3339
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
3440
{
35-
ExecutionOrder::add($this->id);
41+
self::$processedInstances[] = $this->id;
3642
return $handler->handle($request);
3743
}
3844
}

tests/Fixtures/ExecutionOrder.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/LazyMiddlewareTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
class LazyMiddlewareTest extends TestCase
2020
{
21-
public function testInstantiation()
21+
public function test_Instantiation()
2222
{
2323
$this->assertInstanceOf(MiddlewareInterface::class, $this->middleware());
2424
}
2525

26-
public function testInvokingMiddleware()
26+
public function test_InvokingMiddleware()
2727
{
2828
$middleware = $this->middleware();
2929
$this->assertFalse(Doubles\MockedMiddleware::$instance);
@@ -33,9 +33,7 @@ public function testInvokingMiddleware()
3333

3434
private function middleware(): MiddlewareInterface
3535
{
36-
Doubles\MockedMiddleware::$instance = false;
37-
return new LazyMiddleware(function () {
38-
return new Doubles\MockedMiddleware('lazy');
39-
});
36+
Doubles\MockedMiddleware::reset();
37+
return new LazyMiddleware(fn () => new Doubles\MockedMiddleware('lazy'));
4038
}
4139
}

tests/MiddlewareChainTest.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,40 @@
1919

2020
class MiddlewareChainTest extends TestCase
2121
{
22-
public function testInstantiation()
22+
public function test_Instantiation()
2323
{
2424
$this->assertInstanceOf(MiddlewareChain::class, $this->middleware());
2525
}
2626

27-
public function testEmptyChainIsProcessed()
27+
public function test_EmptyChain_IsProcessed()
2828
{
2929
$this->assertInstanceOf(ResponseInterface::class, $this->process());
3030
}
3131

32-
public function testSingleMiddlewareIsProcessed()
32+
public function test_SingleMiddleware_IsProcessed()
3333
{
34-
$this->assertInstanceOf(ResponseInterface::class, $this->process(new Doubles\MockedMiddleware('single')));
35-
$this->assertSame(['single'], Fixtures\ExecutionOrder::$processIdList);
34+
$this->assertInstanceOf(ResponseInterface::class, $this->process('single'));
35+
$this->assertSame(['single'], Doubles\MockedMiddleware::$processedInstances);
3636
}
3737

38-
public function testChainIsProcessedInCorrectOrder()
38+
public function test_Chain_IsProcessedInCorrectOrder()
3939
{
40-
$response = $this->process(
41-
new Doubles\MockedMiddleware('first'),
42-
new Doubles\MockedMiddleware('second'),
43-
new Doubles\MockedMiddleware('third')
44-
);
40+
$response = $this->process('first', 'second', 'third');
4541
$this->assertInstanceOf(ResponseInterface::class, $response);
46-
$this->assertSame(['first', 'second', 'third'], Fixtures\ExecutionOrder::$processIdList);
42+
$this->assertSame(['first', 'second', 'third'], Doubles\MockedMiddleware::$processedInstances);
4743
}
4844

49-
private function process(MiddlewareInterface ...$middlewares): ResponseInterface
45+
private function process(string ...$middlewareIds): ResponseInterface
5046
{
51-
$middleware = $this->middleware(...$middlewares);
47+
$middleware = $this->middleware(...$middlewareIds);
5248
return $middleware->process(new Doubles\DummyServerRequest(), new Doubles\FakeRequestHandler());
5349
}
5450

55-
private function middleware(MiddlewareInterface ...$middlewares): MiddlewareInterface
51+
private function middleware(string ...$middlewareIds): MiddlewareInterface
5652
{
57-
Fixtures\ExecutionOrder::reset();
53+
Doubles\MockedMiddleware::reset();
54+
$middlewares = array_map(fn (string $id) => new Doubles\MockedMiddleware($id), $middlewareIds);
55+
5856
return new MiddlewareChain(...$middlewares);
5957
}
6058
}

0 commit comments

Comments
 (0)