Browse Source

Merge pull request #17856 from cakephp/5.next-test-replace-request

5.next: add method in IntegrationTestTrait to replace configured request
Mark Story 1 year ago
parent
commit
b4f387f8ef

+ 12 - 1
src/TestSuite/IntegrationTestTrait.php

@@ -271,7 +271,7 @@ trait IntegrationTestTrait
     }
 
     /**
-     * Configures the data for the *next* request.
+     * Configures the data for the *next* request merging with existing state.
      *
      * This data is cleared in the tearDown() method.
      *
@@ -288,6 +288,17 @@ trait IntegrationTestTrait
     }
 
     /**
+     * Configures the data for the *next* request replacing existing state.
+     *
+     * @param array $data The request data to use.
+     * @return void
+     */
+    public function replaceRequest(array $data): void
+    {
+        $this->_request = $data;
+    }
+
+    /**
      * Sets HTTP headers for the *next* request to be identified as JSON request.
      *
      * @return void

+ 8 - 0
tests/TestCase/TestSuite/IntegrationTestTraitTest.php

@@ -182,6 +182,14 @@ class IntegrationTestTraitTest extends TestCase
         $this->assertSame(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
         $this->assertSame('foo', $request['environment']['PHP_AUTH_USER']);
         $this->assertSame('bar', $request['environment']['PHP_AUTH_PW']);
+
+        $this->replaceRequest([
+            'headers' => [
+                'X-CSRF-Token' => 'test321',
+            ],
+        ]);
+        $this->assertSame('test321', $this->_request['headers']['X-CSRF-Token']);
+        $this->assertEmpty($this->_request['webroot']);
     }
 
     /**