|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Slim Framework (https://slimframework.com) |
| 5 | + * |
| 6 | + * @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License) |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespaceSlim\Tests\Handlers\Strategies; |
| 12 | + |
| 13 | +usePsr\Http\Message\ResponseInterface; |
| 14 | +usePsr\Http\Message\ServerRequestInterface; |
| 15 | +useRuntimeException; |
| 16 | +useSlim\Handlers\Strategies\RequestResponseNamedArgs; |
| 17 | +useSlim\Tests\TestCase; |
| 18 | + |
| 19 | +class RequestResponseNamedArgsTestextends TestCase |
| 20 | +{ |
| 21 | +privateServerRequestInterface$request; |
| 22 | +privateResponseInterface$response; |
| 23 | + |
| 24 | +publicfunctionsetUp():void |
| 25 | + { |
| 26 | +$this->request =$this->createMock(ServerRequestInterface::class); |
| 27 | +$this->response =$this->createMock(ResponseInterface::class); |
| 28 | + } |
| 29 | + |
| 30 | +publicfunctiontestCreatingRequestResponseNamedArgsThrowsRuntimeExceptionForPHPOlderThan80() |
| 31 | + { |
| 32 | +if (PHP_VERSION_ID >=80000) { |
| 33 | +$this->markTestSkipped('Test only valid for PHP versions older than 8.0'); |
| 34 | + } |
| 35 | + |
| 36 | +$this->expectException(RuntimeException::class); |
| 37 | +newRequestResponseNamedArgs(); |
| 38 | + } |
| 39 | + |
| 40 | +publicfunctiontestCallingWithEmptyArguments() |
| 41 | + { |
| 42 | +if (PHP_VERSION_ID <80000) { |
| 43 | +$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); |
| 44 | + } |
| 45 | + |
| 46 | +$args = []; |
| 47 | +$invocationStrategy =newRequestResponseNamedArgs(); |
| 48 | + |
| 49 | +$callback =function ($request,$response) { |
| 50 | +$this->assertSame($this->request,$request); |
| 51 | +$this->assertSame($this->response,$response); |
| 52 | + |
| 53 | +return$response; |
| 54 | + }; |
| 55 | + |
| 56 | +$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args)); |
| 57 | + } |
| 58 | + |
| 59 | +publicfunctiontestCallingWithKnownArguments() |
| 60 | + { |
| 61 | +if (PHP_VERSION_ID <80000) { |
| 62 | +$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); |
| 63 | + } |
| 64 | + |
| 65 | +$args = [ |
| 66 | +'name' =>'world', |
| 67 | +'greeting' =>'hello', |
| 68 | + ]; |
| 69 | + |
| 70 | +$invocationStrategy =newRequestResponseNamedArgs(); |
| 71 | + |
| 72 | +$callback =function ($request,$response,$greeting,$name)use ($args) { |
| 73 | +$this->assertSame($this->request,$request); |
| 74 | +$this->assertSame($this->response,$response); |
| 75 | +$this->assertSame($greeting,$args['greeting']); |
| 76 | +$this->assertSame($name,$args['name']); |
| 77 | + |
| 78 | +return$response; |
| 79 | + }; |
| 80 | + |
| 81 | +$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args)); |
| 82 | + } |
| 83 | + |
| 84 | +publicfunctiontestCallingWithOptionalArguments() |
| 85 | + { |
| 86 | +if (PHP_VERSION_ID <80000) { |
| 87 | +$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); |
| 88 | + } |
| 89 | + |
| 90 | +$args = [ |
| 91 | +'name' =>'world', |
| 92 | + ]; |
| 93 | + |
| 94 | +$invocationStrategy =newRequestResponseNamedArgs(); |
| 95 | + |
| 96 | +$callback =function ($request,$response,$greeting ='Hello',$name ='Rob')use ($args) { |
| 97 | +$this->assertSame($this->request,$request); |
| 98 | +$this->assertSame($this->response,$response); |
| 99 | +$this->assertSame($greeting,'Hello'); |
| 100 | +$this->assertSame($name,$args['name']); |
| 101 | + |
| 102 | +return$response; |
| 103 | + }; |
| 104 | + |
| 105 | +$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args)); |
| 106 | + } |
| 107 | + |
| 108 | +publicfunctiontestCallingWithUnknownAndVariadic() |
| 109 | + { |
| 110 | +if (PHP_VERSION_ID <80000) { |
| 111 | +$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); |
| 112 | + } |
| 113 | + |
| 114 | +$args = [ |
| 115 | +'name' =>'world', |
| 116 | +'greeting' =>'hello', |
| 117 | + ]; |
| 118 | + |
| 119 | +$invocationStrategy =newRequestResponseNamedArgs(); |
| 120 | + |
| 121 | +$callback =function ($request,$response, ...$arguments)use ($args) { |
| 122 | +$this->assertSame($this->request,$request); |
| 123 | +$this->assertSame($this->response,$response); |
| 124 | +$this->assertSame($args,$arguments); |
| 125 | + |
| 126 | +return$response; |
| 127 | + }; |
| 128 | + |
| 129 | +$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args)); |
| 130 | + } |
| 131 | + |
| 132 | +publicfunctiontestCallingWithMixedKnownAndUnknownParametersAndVariadic() |
| 133 | + { |
| 134 | +if (PHP_VERSION_ID <80000) { |
| 135 | +$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0'); |
| 136 | + } |
| 137 | + |
| 138 | +$known = [ |
| 139 | +'name' =>'world', |
| 140 | +'greeting' =>'hello', |
| 141 | + ]; |
| 142 | +$unknown = [ |
| 143 | +'foo' =>'foo', |
| 144 | +'bar' =>'bar', |
| 145 | + ]; |
| 146 | +$args =array_merge($known,$unknown); |
| 147 | +$invocationStrategy =newRequestResponseNamedArgs(); |
| 148 | + |
| 149 | +$callback =function ($request,$response,$name,$greeting, ...$arguments)use ($known,$unknown) { |
| 150 | +$this->assertSame($this->request,$request); |
| 151 | +$this->assertSame($this->response,$response); |
| 152 | +$this->assertSame($name,$known['name']); |
| 153 | +$this->assertSame($greeting,$known['greeting']); |
| 154 | +$this->assertSame($unknown,$arguments); |
| 155 | + |
| 156 | +return$response; |
| 157 | + }; |
| 158 | + |
| 159 | +$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args)); |
| 160 | + } |
| 161 | +} |