Skip to content

Commit 1c506dd

Browse files
authored
Merge pull request #3 from m-shule/dev
Fix Param matching
2 parents 96f1257 + bf1cbbc commit 1c506dd

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

src/Matching/CueValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function matches(Pipe $pipe, Request $request)
2121
// if cue has no static params we have to let the request
2222
// pass and trust in the PatternValidator to detect
2323
// mistakes with the parameters.
24-
if (Str::startsWith($pipe->cue(), '{')) {
24+
if ($pipe->cueStartsWithPlaceholder()) {
2525
return true;
2626
}
2727

src/Pipe.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static function getValidators()
172172
public function path(Request $request)
173173
{
174174
$replacements = collect($this->parameterNames())->map(function ($param) use ($request) {
175-
return $request->{$param};
175+
return $request->{$this->paramKey($param)};
176176
})->toArray();
177177

178178
$path = preg_replace_array('/\\{[a-zA-Z]+\\}/', $replacements, $this->uri());
@@ -234,4 +234,40 @@ public function getAlias()
234234
{
235235
return $this->alias;
236236
}
237+
238+
/**
239+
* Checks if cue of this pipe starts with a placeholder.
240+
*
241+
* @return bool
242+
*/
243+
public function cueStartsWithPlaceholder()
244+
{
245+
return Str::startsWith($this->cue(), '{');
246+
}
247+
248+
/**
249+
* Checks if cue only contains one placeholder.
250+
*
251+
* @return bool
252+
*/
253+
public function cueIsPlaceholder()
254+
{
255+
return preg_match('/^\\{[A-Za-z]+\\}$/', $this->cue());
256+
}
257+
258+
/**
259+
* Get param key for the path matching of a pipe.
260+
*
261+
* @return string
262+
*/
263+
public function paramKey($param)
264+
{
265+
if ($this->key() === resolve('pipe_any')) {
266+
return $param;
267+
}
268+
269+
return $this->cueIsPlaceholder()
270+
? $this->key()
271+
: $param ?? $this->key();
272+
}
237273
}

src/Request.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public static function destruct(HttpRequest $request)
4141
*
4242
* @return self
4343
*/
44-
public static function reconstruct($data)
44+
public static function reconstruct($data, $request = self::class)
4545
{
46-
return new self(...$data);
46+
return tap(resolve($request), function ($request) use ($data) {
47+
$request->initialize(...$data);
48+
});
4749
}
4850

4951
/**

tests/PipeRequestTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Mshule\LaravelPipes\Tests;
44

5+
use Illuminate\Support\Facades\Event;
56
use Mshule\LaravelPipes\Facades\Pipe;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Mshule\LaravelPipes\Testing\MakesPipeRequests;
89
use Mshule\LaravelPipes\Tests\Fixtures\Models\Todo;
10+
use Mshule\LaravelPipes\Events\IncomingPipeResponse;
911
use Mshule\LaravelPipes\Exceptions\NotFoundPipeException;
1012

1113
class PipeRequestTest extends TestCase
@@ -191,6 +193,7 @@ public function it_can_add_fallback_pipes_to_handle_any_request_which_could_not_
191193
/** @test */
192194
public function it_can_match_dynamic_parameters()
193195
{
196+
$this->withoutExceptionHandling();
194197
Pipe::fake();
195198

196199
Pipe::match('trigger:name {text}', function ($text) {
@@ -205,6 +208,32 @@ public function it_can_match_dynamic_parameters()
205208
});
206209
}
207210

211+
/** @test */
212+
public function its_dynamic_pattern_match_checks_first_for_placeholder_name_in_request_then_for_default_cue_value()
213+
{
214+
Pipe::fake();
215+
216+
Pipe::match('trigger:{text}', function ($text) {
217+
return "you said {$text}";
218+
});
219+
220+
$this->pipe(['trigger' => 'something']);
221+
222+
Pipe::assertResponded(function ($response) {
223+
$response->assertOk()
224+
->assertSee('you said something');
225+
});
226+
227+
Event::listen(IncomingPipeResponse::class, function () {
228+
$this->pipe(['trigger' => 'something', 'text' => 'another']);
229+
230+
Pipe::assertResponded(function ($response) {
231+
$response->assertOk()
232+
->assertSee('you said another');
233+
});
234+
});
235+
}
236+
208237
/** @test */
209238
public function it_can_match_multiple_dynamic_parameters()
210239
{

0 commit comments

Comments
 (0)