Browse Source

Add RequestFactory.

ADmad 3 years ago
parent
commit
ea4031145e

+ 3 - 2
src/Http/Client/Request.php

@@ -18,6 +18,7 @@ namespace Cake\Http\Client;
 use Laminas\Diactoros\RequestTrait;
 use Laminas\Diactoros\Stream;
 use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\UriInterface;
 
 /**
  * Implements methods for HTTP requests.
@@ -35,13 +36,13 @@ class Request extends Message implements RequestInterface
      * Provides backwards compatible defaults for some properties.
      *
      * @phpstan-param array<non-empty-string, non-empty-string> $headers
-     * @param string $url The request URL
+     * @param \Psr\Http\Message\UriInterface|string $url The request URL
      * @param string $method The HTTP method to use.
      * @param array $headers The HTTP headers to set.
      * @param array|string|null $data The request body to use.
      */
     public function __construct(
-        string $url = '',
+        UriInterface|string $url = '',
         string $method = self::METHOD_GET,
         array $headers = [],
         array|string|null $data = null

+ 40 - 0
src/Http/RequestFactory.php

@@ -0,0 +1,40 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         5.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Http;
+
+use Cake\Http\Client\Request;
+use Psr\Http\Message\RequestFactoryInterface;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Factory for creating request instances.
+ */
+class RequestFactory implements RequestFactoryInterface
+{
+    /**
+     * Create a new request.
+     *
+     * @param string $method The HTTP method associated with the request.
+     * @param \Psr\Http\Message\UriInterface|string $uri The URI associated with the request.
+     * @return \Psr\Http\Message\RequestInterface
+     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
+     */
+    public function createRequest(string $method, $uri): RequestInterface
+    {
+        return new Request($uri, $method);
+    }
+}

+ 43 - 0
tests/TestCase/Http/RequestFactoryTest.php

@@ -0,0 +1,43 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         5.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\Http;
+
+use Cake\Http\RequestFactory;
+use Cake\TestSuite\TestCase;
+use Laminas\Diactoros\Uri;
+
+/**
+ * Test case for the request factory.
+ */
+class RequestFactoryTest extends TestCase
+{
+    public function testCreateRequest(): void
+    {
+        $factory = new RequestFactory();
+
+        $request = $factory->createRequest('POST', 'http://example.com');
+
+        $this->assertSame('http://example.com', (string)$request->getUri());
+        $this->assertStringContainsString($request->getMethod(), 'POST');
+
+        $uri = new Uri('http://example.com');
+        $request = $factory->createRequest('GET', $uri);
+
+        $this->assertSame($uri, $request->getUri());
+        $this->assertStringContainsString($request->getMethod(), 'GET');
+    }
+}