From 02b58395998ec5f88dbd599178731db3550fbe7c Mon Sep 17 00:00:00 2001 From: Mehdi Fathi Date: Tue, 13 Jun 2017 19:15:59 -0700 Subject: [PATCH] Improve tests for Client\Request (#10762) * Fix array input in method * Make mehtod for test 4 main method request * Fix error lint * Fix test version * Make dataprovider RequestTest || Fix url * Make comment method * Fix request test --- src/Http/Client/Request.php | 4 +- tests/TestCase/Http/Client/RequestTest.php | 44 ++++++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Http/Client/Request.php b/src/Http/Client/Request.php index da71a071101..67e894aac2d 100644 --- a/src/Http/Client/Request.php +++ b/src/Http/Client/Request.php @@ -153,7 +153,7 @@ public function header($name = null, $value = null) * @param array $headers The headers to add. * @return void */ - protected function addHeaders($headers) + protected function addHeaders(array $headers) { foreach ($headers as $key => $val) { $normalized = strtolower($key); @@ -253,5 +253,5 @@ public function body($body = null) } } -// @deprecated Add backwards compat alias. +// @deprecated Add backwards compact alias. class_alias('Cake\Http\Client\Request', 'Cake\Network\Http\Request'); diff --git a/tests/TestCase/Http/Client/RequestTest.php b/tests/TestCase/Http/Client/RequestTest.php index 7ad5c6de9bd..c395e54bbd0 100644 --- a/tests/TestCase/Http/Client/RequestTest.php +++ b/tests/TestCase/Http/Client/RequestTest.php @@ -22,6 +22,7 @@ */ class RequestTest extends TestCase { + /** * test string ata, header and constructor * @@ -37,10 +38,44 @@ public function testConstructorStringData() $request = new Request('http://example.com', 'POST', $headers, json_encode($data)); $this->assertEquals('http://example.com', $request->url()); - $this->assertEquals('POST', $request->getMethod()); + $this->assertContains($request->getMethod(), 'POST'); $this->assertEquals('application/json', $request->getHeaderLine('Content-Type')); $this->assertEquals(json_encode($data), $request->body()); } + /** + * @param array $headers The HTTP headers to set. + * @param array|string|null $data The request body to use. + * @param string $method The HTTP method to use. + * + * @dataProvider additionProvider + */ + public function testMethods(array $headers, $data, $method) + { + $request = new Request('http://example.com', $method, $headers, json_encode($data)); + + $this->assertEquals($request->getMethod(), $method); + $this->assertEquals('http://example.com', $request->url()); + $this->assertEquals('application/json', $request->getHeaderLine('Content-Type')); + $this->assertEquals(json_encode($data), $request->body()); + } + /** + * @dataProvider additionProvider + */ + public function additionProvider() + { + $headers = [ + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer valid-token', + ]; + $data = ['a' => 'b', 'c' => 'd']; + + return [ + [$headers, $data, Request::METHOD_POST], + [$headers, $data, Request::METHOD_GET], + [$headers, $data, Request::METHOD_PUT], + [$headers, $data, Request::METHOD_DELETE], + ]; + } /** * test array data, header and constructor @@ -128,7 +163,7 @@ public function testMethod() } /** - * test method interop. + * test method interoperability. * * @return void */ @@ -136,6 +171,7 @@ public function testMethodInteroperability() { $request = new Request(); $this->assertSame($request, $request->method(Request::METHOD_GET)); + $this->assertEquals(Request::METHOD_GET, $request->method()); $this->assertEquals(Request::METHOD_GET, $request->getMethod()); @@ -293,13 +329,13 @@ public function testVersion() { $request = new Request(); $result = $request->version('1.0'); - $this->assertSame($request, $request, 'Should return self'); + $this->assertSame($request, $result, 'Should return self'); $this->assertSame('1.0', $request->version()); } /** - * test version interop. + * test version Interoperable. * * @return void */