Skip to content

Commit 37449d3

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.4
2 parents 0d15487 + 73d3997 commit 37449d3

7 files changed

Lines changed: 304 additions & 33 deletions

File tree

‎system/Router/RouteCollection.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,11 @@ protected function buildReverseRoute(string $routeKey, array $params): string
13261326
// Build our resulting string, inserting the $params in
13271327
// the appropriate places.
13281328
foreach ($matches[0] as $index => $pattern) {
1329+
if (! isset($params[$index])) {
1330+
throw new InvalidArgumentException(
1331+
'Missing argument for "' . $pattern . '" in route "' . $from . '".'
1332+
);
1333+
}
13291334
if (! preg_match('#^' . $pattern . '$#u', $params[$index])) {
13301335
throw RouterException::forInvalidParameterType();
13311336
}

‎system/Test/FeatureTestTrait.php‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function withBodyFormat(string $format)
109109
/**
110110
* Set the raw body for the request
111111
*
112-
* @param mixed $body
112+
* @param string $body
113113
*
114114
* @return $this
115115
*/
@@ -136,6 +136,8 @@ public function skipEvents()
136136
* Calls a single URI, executes it, and returns a TestResponse
137137
* instance that can be used to run many assertions against.
138138
*
139+
* @param string $method HTTP verb
140+
*
139141
* @return TestResponse
140142
*/
141143
public function call(string $method, string $path, ?array $params = null)
@@ -147,7 +149,7 @@ public function call(string $method, string $path, ?array $params = null)
147149
$request = $this->setupRequest($method, $path);
148150
$request = $this->setupHeaders($request);
149151
$request = $this->populateGlobals($method, $request, $params);
150-
$request = $this->setRequestBody($request);
152+
$request = $this->setRequestBody($request, $params);
151153

152154
// Initialize the RouteCollection
153155
if (! $routes = $this->routes) {
@@ -258,6 +260,8 @@ public function options(string $path, ?array $params = null)
258260
/**
259261
* Setup a Request object to use so that CodeIgniter
260262
* won't try to auto-populate some of the items.
263+
*
264+
* @param string $method HTTP verb
261265
*/
262266
protected function setupRequest(string $method, ?string $path = null): IncomingRequest
263267
{
@@ -302,6 +306,8 @@ protected function setupHeaders(IncomingRequest $request)
302306
*
303307
* Always populate the GET vars based on the URI.
304308
*
309+
* @param string $method HTTP verb
310+
*
305311
* @return Request
306312
*
307313
* @throws ReflectionException
@@ -310,16 +316,23 @@ protected function populateGlobals(string $method, Request $request, ?array $par
310316
{
311317
// $params should set the query vars if present,
312318
// otherwise set it from the URL.
313-
$get = ! empty($params) && $method === 'get'
319+
$get = (! empty($params) && $method === 'get')
314320
? $params
315321
: $this->getPrivateProperty($request->getUri(), 'query');
316322

317323
$request->setGlobal('get', $get);
318-
if ($method !== 'get') {
319-
$request->setGlobal($method, $params);
324+
325+
if ($method === 'get') {
326+
$request->setGlobal('request', $request->fetchGlobal('get'));
320327
}
321328

322-
$request->setGlobal('request', $params);
329+
if ($method === 'post') {
330+
$request->setGlobal($method, $params);
331+
$request->setGlobal(
332+
'request',
333+
$request->fetchGlobal('post') + $request->fetchGlobal('get')
334+
);
335+
}
323336

324337
$_SESSION = $this->session ?? [];
325338

@@ -331,31 +344,30 @@ protected function populateGlobals(string $method, Request $request, ?array $par
331344
* This allows the body to be formatted in a way that the controller is going to
332345
* expect as in the case of testing a JSON or XML API.
333346
*
334-
* @param array|null $params The parameters to be formatted and put in the body. If this is empty, it will get the
335-
* what has been loaded into the request global of the request class.
347+
* @param array|null $params The parameters to be formatted and put in the body.
336348
*/
337349
protected function setRequestBody(Request $request, ?array $params = null): Request
338350
{
339-
if (isset($this->requestBody) && $this->requestBody !== '') {
351+
if ($this->requestBody !== '') {
340352
$request->setBody($this->requestBody);
341-
342-
return $request;
343353
}
344354

345-
if (isset($this->bodyFormat) && $this->bodyFormat !== '') {
346-
if (empty($params)) {
347-
$params = $request->fetchGlobal('request');
348-
}
355+
if ($this->bodyFormat !== '') {
349356
$formatMime = '';
350357
if ($this->bodyFormat === 'json') {
351358
$formatMime = 'application/json';
352359
} elseif ($this->bodyFormat === 'xml') {
353360
$formatMime = 'application/xml';
354361
}
355-
if (! empty($formatMime) && ! empty($params)) {
362+
363+
if ($formatMime !== '') {
364+
$request->setHeader('Content-Type', $formatMime);
365+
}
366+
367+
if ($params !== null && $formatMime !== '') {
356368
$formatted = Services::format()->getFormatter($formatMime)->format($params);
369+
// "withBodyFormat() and $params of call()" has higher priority than withBody().
357370
$request->setBody($formatted);
358-
$request->setHeader('Content-Type', $formatMime);
359371
}
360372
}
361373

‎tests/system/Helpers/URLHelper/MiscUrlTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Router\Exceptions\RouterException;
1818
use CodeIgniter\Test\CIUnitTestCase;
1919
use Config\App;
20+
use InvalidArgumentException;
2021

2122
/**
2223
* @backupGlobals enabled
@@ -900,4 +901,20 @@ public function testUrlToWithSupportedLocaleInRoute()
900901
url_to('path-to', 'string', 13, 'en')
901902
);
902903
}
904+
905+
/**
906+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/7651
907+
*/
908+
public function testUrlToMissingArgument()
909+
{
910+
$this->expectException(InvalidArgumentException::class);
911+
$this->expectExceptionMessage('Missing argument for "([a-zA-Z]+)" in route "([a-zA-Z]+)/login".');
912+
913+
$routes = Services::routes();
914+
$routes->group('(:alpha)', static function ($routes) {
915+
$routes->match(['get'], 'login', 'Common\LoginController::loginView', ['as' => 'loginURL']);
916+
});
917+
918+
url_to('loginURL');
919+
}
903920
}

‎tests/system/Test/FeatureTestTraitTest.php‎

Lines changed: 195 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,162 @@ public function testIsOkWithRedirects()
376376
$this->assertTrue($response->isOK());
377377
}
378378

379+
public function testCallGetWithParams()
380+
{
381+
$this->withRoutes([
382+
[
383+
'get',
384+
'home',
385+
static fn () => json_encode(Services::request()->getGet()),
386+
],
387+
]);
388+
$data = [
389+
'true' => true,
390+
'false' => false,
391+
'int' => 2,
392+
'null' => null,
393+
'float' => 1.23,
394+
'string' => 'foo',
395+
];
396+
$response = $this->get('home', $data);
397+
398+
$response->assertOK();
399+
$this->assertStringContainsString(
400+
// All GET values will be strings.
401+
'{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"}',
402+
$response->getBody()
403+
);
404+
}
405+
406+
public function testCallGetWithParamsAndREQUEST()
407+
{
408+
$this->withRoutes([
409+
[
410+
'get',
411+
'home',
412+
static fn () => json_encode(Services::request()->fetchGlobal('request')),
413+
],
414+
]);
415+
$data = [
416+
'true' => true,
417+
'false' => false,
418+
'int' => 2,
419+
'null' => null,
420+
'float' => 1.23,
421+
'string' => 'foo',
422+
];
423+
$response = $this->get('home', $data);
424+
425+
$response->assertOK();
426+
$this->assertStringContainsString(
427+
// All GET values will be strings.
428+
'{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"}',
429+
$response->getBody()
430+
);
431+
}
432+
433+
public function testCallPostWithParams()
434+
{
435+
$this->withRoutes([
436+
[
437+
'post',
438+
'home',
439+
static fn () => json_encode(Services::request()->getPost()),
440+
],
441+
]);
442+
$data = [
443+
'true' => true,
444+
'false' => false,
445+
'int' => 2,
446+
'null' => null,
447+
'float' => 1.23,
448+
'string' => 'foo',
449+
];
450+
$response = $this->post('home', $data);
451+
452+
$response->assertOK();
453+
$this->assertStringContainsString(
454+
// All POST values will be strings.
455+
'{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"}',
456+
$response->getBody()
457+
);
458+
}
459+
460+
public function testCallPostWithParamsAndREQUEST()
461+
{
462+
$this->withRoutes([
463+
[
464+
'post',
465+
'home',
466+
static fn () => json_encode(Services::request()->fetchGlobal('request')),
467+
],
468+
]);
469+
$data = [
470+
'true' => true,
471+
'false' => false,
472+
'int' => 2,
473+
'null' => null,
474+
'float' => 1.23,
475+
'string' => 'foo',
476+
];
477+
$response = $this->post('home', $data);
478+
479+
$response->assertOK();
480+
$this->assertStringContainsString(
481+
// All POST values will be strings.
482+
'{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"}',
483+
$response->getBody()
484+
);
485+
}
486+
487+
public function testCallPutWithJsonRequest()
488+
{
489+
$this->withRoutes([
490+
[
491+
'put',
492+
'home',
493+
'\Tests\Support\Controllers\Popcorn::echoJson',
494+
],
495+
]);
496+
$data = [
497+
'true' => true,
498+
'false' => false,
499+
'int' => 2,
500+
'null' => null,
501+
'float' => 1.23,
502+
'string' => 'foo',
503+
];
504+
$response = $this->withBodyFormat('json')
505+
->call('put', 'home', $data);
506+
507+
$response->assertOK();
508+
$response->assertJSONExact($data);
509+
}
510+
511+
public function testCallPutWithJsonRequestAndREQUEST()
512+
{
513+
$this->withRoutes([
514+
[
515+
'put',
516+
'home',
517+
static fn () => json_encode(Services::request()->fetchGlobal('request')),
518+
],
519+
]);
520+
$data = [
521+
'true' => true,
522+
'false' => false,
523+
'int' => 2,
524+
'null' => null,
525+
'float' => 1.23,
526+
'string' => 'foo',
527+
];
528+
$response = $this->withBodyFormat('json')
529+
->call('put', 'home', $data);
530+
531+
$response->assertOK();
532+
$this->assertStringContainsString('[]', $response->getBody());
533+
}
534+
379535
public function testCallWithJsonRequest()
380536
{
381537
$this->withRoutes([
@@ -385,9 +541,19 @@ public function testCallWithJsonRequest()
385541
'\Tests\Support\Controllers\Popcorn::echoJson',
386542
],
387543
]);
388-
$response = $this->withBodyFormat('json')->call('post', 'home', ['foo' => 'bar']);
544+
$data = [
545+
'true' => true,
546+
'false' => false,
547+
'int' => 2,
548+
'null' => null,
549+
'float' => 1.23,
550+
'string' => 'foo',
551+
];
552+
$response = $this->withBodyFormat('json')
553+
->call('post', 'home', $data);
554+
389555
$response->assertOK();
390-
$response->assertJSONExact(['foo' => 'bar']);
556+
$response->assertJSONExact($data);
391557
}
392558

393559
public function testSetupRequestBodyWithParams()
@@ -400,14 +566,39 @@ public function testSetupRequestBodyWithParams()
400566
$this->assertSame('application/json', $request->header('Content-Type')->getValue());
401567
}
402568

569+
public function testSetupJSONRequestBodyWithBody()
570+
{
571+
$request = $this->setupRequest('post', 'home');
572+
$request = $this->withBodyFormat('json')
573+
->withBody(json_encode(['foo1' => 'bar1']))
574+
->setRequestBody($request);
575+
576+
$this->assertJsonStringEqualsJsonString(
577+
json_encode(['foo1' => 'bar1']),
578+
$request->getBody()
579+
);
580+
$this->assertSame(
581+
'application/json',
582+
$request->header('Content-Type')->getValue()
583+
);
584+
}
585+
403586
public function testSetupRequestBodyWithXml()
404587
{
405588
$request = $this->setupRequest('post', 'home');
406589

407-
$request = $this->withBodyFormat('xml')->setRequestBody($request, ['foo' => 'bar']);
590+
$data = [
591+
'true' => true,
592+
'false' => false,
593+
'int' => 2,
594+
'null' => null,
595+
'float' => 1.23,
596+
'string' => 'foo',
597+
];
598+
$request = $this->withBodyFormat('xml')->setRequestBody($request, $data);
408599

409600
$expectedXml = '<?xml version="1.0"?>
410-
<response><foo>bar</foo></response>
601+
<response><true>1</true><false/><int>2</int><null/><float>1.23</float><string>foo</string></response>
411602
';
412603

413604
$this->assertSame($expectedXml, $request->getBody());

‎user_guide_src/source/changelogs/v4.3.7.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Release Date: Unreleased
1212
BREAKING
1313
********
1414

15+
- **FeatureTestTrait:** When using :ref:`withBodyFormat() <feature-formatting-the-request>`,
16+
the priority of the request body has been changed.
17+
See :ref:`Upgrading Guide <upgrade-437-feature-testing>` for details.
18+
1519
Message Changes
1620
***************
1721

0 commit comments

Comments
 (0)