Skip to content

Commit 813a1c9

Browse files
committed
ADDED TEST: new exceptions, other bridge methods.
1 parent 68c35fb commit 813a1c9

File tree

3 files changed

+90
-23
lines changed

3 files changed

+90
-23
lines changed

Src/PipelineBridge.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class PipelineBridge {
2929

3030
/**
3131
* @param string|Pipe|Closure(mixed $subject, Closure $next, mixed ...$use): mixed $from
32-
* @throws InvalidPipeError When invalid pipe given.
32+
* @throws InvalidPipeError When invalid pipe given.
33+
* @throws UnexpectedPipelineException When could not determine thrown exception.
3334
*/
3435
public static function toPipe( string|Closure|Pipe $from ): Pipe {
3536
$pipe = Pipeline::resolve( pipe: $from );
36-
3737
return new class( $pipe ) implements Pipe {
3838
public function __construct( private readonly Closure $pipe ) {}
3939

@@ -61,7 +61,6 @@ public static function toMiddleware( mixed $middleware ): object {
6161
: $default
6262
);
6363

64-
$provided = $middleware;
6564
$isClassName = is_string( $middleware ) && class_exists( $middleware );
6665

6766
try {
@@ -76,9 +75,8 @@ public static function toMiddleware( mixed $middleware ): object {
7675
if ( null === $middleware ) {
7776
throw new InvalidMiddlewareForPipeError(
7877
sprintf(
79-
'Invalid or Non-existing class "%1$s". Middleware must be a Closure, an'
80-
. ' instance of %2$s or a concrete\'s classname that implements %2$s.',
81-
$isClassName ? $provided : $provided::class,
78+
'Invalid middleware type. Middleware must be a Closure, an'
79+
. ' instance of "%1$s" or a concrete\'s classname that implements "%1$s".',
8280
$interface
8381
)
8482
);

Tests/BridgeTest.php

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace TheWebSolver\Codegarage;
1111

12+
use Closure;
1213
use MiddlewareAdapter;
1314
use Psr7Adapter\Request;
1415
use Psr7Adapter\Response;
@@ -19,23 +20,90 @@
1920
use Psr7Adapter\ServerRequestInterface;
2021
use Psr15Adapter\RequestHandlerInterface;
2122
use TheWebSolver\Codegarage\Lib\Pipeline;
23+
use TheWebSolver\Codegarage\Stub\PipeStub;
24+
use TheWebSolver\Codegarage\Lib\PipeInterface;
2225
use TheWebSolver\Codegarage\Lib\PipelineBridge;
26+
use TheWebSolver\Codegarage\Lib\InvalidMiddlewareForPipeError;
2327
use TheWebSolver\Codegarage\Lib\MiddlewarePsrNotFoundException;
2428

2529
class BridgeTest extends TestCase {
26-
private bool $PSRPackageInstalled;
27-
protected function setUp(): void {
28-
$this->PSRPackageInstalled = interface_exists( '\\Psr\\Http\\Server\\MiddlewareInterface' );
30+
private function addPsrPackageFixtures(): void {
31+
PipelineBridge::setMiddlewareAdapter(
32+
interface: MiddlewareInterface::class,
33+
className: MiddlewareAdapter::class
34+
);
35+
}
2936

30-
require_once __DIR__ . '/Stub/PsrStubs.php';
37+
private function removePsrPackageFixtures(): void {
38+
PipelineBridge::setMiddlewareAdapter( interface: '', className: '' );
39+
}
3140

41+
/** @dataProvider provideVariousPipes */
42+
public function testPipeConversion( string|Closure|PipeInterface $from ): void {
43+
$this->assertInstanceOf( PipeInterface::class, PipelineBridge::toPipe( $from ) );
3244
}
3345

34-
public function testPSRBridge() {
35-
PipelineBridge::setMiddlewareAdapter(
36-
interface: MiddlewareInterface::class,
37-
className: MiddlewareAdapter::class
46+
/** @return array<mixed[]> */
47+
public function provideVariousPipes(): array {
48+
return array(
49+
array( fn( $subject, $next ) => $next( $subject ) ),
50+
array( PipeStub::class ),
51+
array(
52+
new class implements PipeInterface {
53+
public function handle( mixed $subject, Closure $next, mixed ...$use ): mixed {
54+
return $next( $subject );
55+
}
56+
}
57+
),
3858
);
59+
}
60+
61+
/** @dataProvider provideMiddlewares */
62+
public function testMiddlewareConversion( mixed $middleware, ?string $thrown ): void {
63+
$this->addPsrPackageFixtures();
64+
65+
if ( $thrown ) {
66+
$this->expectException( $thrown );
67+
}
68+
69+
$this->assertInstanceOf( MiddlewareInterface::class, PipelineBridge::toMiddleware( $middleware ) );
70+
}
71+
72+
/** @dataProvider provideMiddlewares */
73+
public function testMiddlewareToPipeConversion( mixed $middleware, ?string $thrown ): void {
74+
// The middleware gets converted to pipe irrespective of middleware being invalid.
75+
// The exception is only thrown when converted pipe handles the subject.
76+
$this->assertInstanceOf( PipeInterface::class, PipelineBridge::middlewareToPipe( $middleware ) );
77+
}
78+
79+
/** @return array<mixed[]>*/
80+
public function provideMiddlewares(): array {
81+
return array(
82+
array( Middleware::class, null ),
83+
array( $this->createMock( MiddlewareInterface::class ), null ),
84+
array( '\\Invalid\\Middleware', InvalidMiddlewareForPipeError::class ),
85+
array( true, InvalidMiddlewareForPipeError::class ),
86+
array( static::class, InvalidMiddlewareForPipeError::class ),
87+
array(
88+
static fn( ServerRequestInterface $r, RequestHandlerInterface $h ) => new Response(),
89+
null,
90+
),
91+
array(
92+
new class implements MiddlewareInterface {
93+
public function process(
94+
ServerRequestInterface $request,
95+
RequestHandlerInterface $handler
96+
): ResponseInterface {
97+
return new Response();
98+
}
99+
},
100+
null,
101+
),
102+
);
103+
}
104+
105+
public function testPSRBridge() {
106+
$this->addPsrPackageFixtures();
39107

40108
$request = new Request();
41109
$handler = new class() implements RequestHandlerInterface {
@@ -70,10 +138,10 @@ public function process(
70138

71139
$this->assertSame( expected: 350, actual: $handler->handle( $request )->getStatusCode() );
72140

73-
PipelineBridge::resetMiddlewareAdapter();
141+
$this->removePsrPackageFixtures();
74142

75143
// Must always throw exception if core PSR-15 implementation not used.
76-
if ( ! $this->PSRPackageInstalled ) {
144+
if ( ! CODEGARAGE_PSR_PACKAGE_INSTALLED ) {
77145
$this->expectException( MiddlewarePsrNotFoundException::class );
78146
$this->expectExceptionMessage( 'Cannot find PSR15 HTTP Server Middleware.' );
79147

Tests/PipelineTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
class PipelineTest extends TestCase {
2323
/** @dataProvider provideVariousPipeTypes */
24-
public function testPipeResolver( mixed $pipe, bool $throws ): void {
25-
if ( $throws ) {
26-
$this->expectException( InvalidPipeError::class );
24+
public function testPipeResolver( mixed $pipe, ?string $thrown ): void {
25+
if ( $thrown ) {
26+
$this->expectException( $thrown );
2727
}
2828

2929
$this->assertSame(
@@ -38,16 +38,17 @@ public function testPipeResolver( mixed $pipe, bool $throws ): void {
3838
/** @return mixed[] */
3939
public static function provideVariousPipeTypes(): array {
4040
return array(
41-
array( PipeStub::class, false ),
42-
array( static fn ( string $subject, Closure $next ): string => $next( $subject ), false ),
43-
array( '\\Undefined\\ClassName', true ),
41+
array( PipeStub::class, null ),
42+
array( static fn ( string $subject, Closure $next ): string => $next( $subject ), null ),
43+
array( '\\Undefined\\ClassName', InvalidPipeError::class ),
44+
array( static::class, UnexpectedPipelineException::class ),
4445
array(
4546
new class() implements PipeInterface {
4647
public function handle( mixed $subject, Closure $next, mixed ...$use ): mixed {
4748
return $subject;
4849
}
4950
},
50-
false,
51+
null,
5152
),
5253
);
5354
}

0 commit comments

Comments
 (0)