From 744404d7da2bebbe06ac3dd62773ff9f43654dc2 Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Wed, 1 Oct 2025 20:47:31 +0400 Subject: [PATCH 1/2] Making sure to not wrap the orm instance into model when it is an anonymous model --- src/Paginator/Adapters/ModelPaginator.php | 10 +++-- .../Paginator/Adapters/ModelPaginatorTest.php | 41 +++++++++++++++++-- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/Paginator/Adapters/ModelPaginator.php b/src/Paginator/Adapters/ModelPaginator.php index 50ad2672..9aaac628 100644 --- a/src/Paginator/Adapters/ModelPaginator.php +++ b/src/Paginator/Adapters/ModelPaginator.php @@ -62,11 +62,13 @@ public function data(): ModelCollection ->offset($this->perPage * ($this->page - 1)) ->get(); - $models = array_map(function ($item) { - return wrapToModel($item->getOrmInstance(), $this->modelClass); - }, iterator_to_array($result)); + if($this->modelClass != '@anonymous') { + $result = array_map(function ($item) { + return wrapToModel($item->getOrmInstance(), $this->modelClass); + }, iterator_to_array($result)); + } - return new ModelCollection($models); + return new ModelCollection($result); } /** diff --git a/tests/Unit/Paginator/Adapters/ModelPaginatorTest.php b/tests/Unit/Paginator/Adapters/ModelPaginatorTest.php index 13ae2813..1eae8f49 100644 --- a/tests/Unit/Paginator/Adapters/ModelPaginatorTest.php +++ b/tests/Unit/Paginator/Adapters/ModelPaginatorTest.php @@ -6,6 +6,8 @@ use Quantum\Tests\Unit\Paginator\PaginatorTestCase; use Quantum\Paginator\Adapters\ModelPaginator; use Quantum\Model\Factories\ModelFactory; +use Quantum\Model\ModelCollection; +use Quantum\Model\QtModel; class ModelPaginatorTest extends PaginatorTestCase { @@ -33,13 +35,46 @@ public function testModelPaginatorData() $this->assertIsIterable($data); + $this->assertInstanceOf(ModelCollection::class, $data); + + $this->assertInstanceOf(ModelCollection::class, $data); + + $this->assertEquals(2, $data->count()); + + $record = $data->first(); + + $this->assertInstanceOf(QtModel::class, $record); + + $this->assertInstanceOf(TestPostModel::class, $record); + + $this->assertEquals('Hi', $record->title); + + $this->assertEquals('First post!', $record->content); + } + + public function testModelPaginatorDataWithAnonymousModel() + { + $dynamicModel = ModelFactory::createDynamicModel('posts'); + + $this->paginator = new ModelPaginator($dynamicModel, 2, 1); + + $data = $this->paginator->data(); + + $this->assertIsIterable($data); + + $this->assertInstanceOf(ModelCollection::class, $data); + $this->assertEquals(2, $data->count()); - $this->assertInstanceOf(TestPostModel::class, $data->first()); + $record = $data->first(); + + $this->assertInstanceOf(QtModel::class, $record); + + $this->assertStringContainsString('@anonymous', get_class($record)); - $this->assertEquals('Hi', $data->first()->title); + $this->assertEquals('Hi', $record->title); - $this->assertEquals('First post!', $data->first()->content); + $this->assertEquals('First post!', $record->content); } public function testModelPaginatorFirstItem() From ca14ac75bbc9f230ddc3b16f4bbac16c13539a4a Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Sun, 5 Oct 2025 19:15:53 +0400 Subject: [PATCH 2/2] Updating data source for curl unit tests --- .../Libraries/HttpClient/HttpClientTest.php | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/tests/Unit/Libraries/HttpClient/HttpClientTest.php b/tests/Unit/Libraries/HttpClient/HttpClientTest.php index 0c831920..280a8028 100644 --- a/tests/Unit/Libraries/HttpClient/HttpClientTest.php +++ b/tests/Unit/Libraries/HttpClient/HttpClientTest.php @@ -11,9 +11,9 @@ class HttpClientTest extends AppTestCase private $httpClient; - private $restServer = 'https://reqres.in/api'; + private $restServer = 'https://api.thedogapi.com/v1'; - private $restServerApiKey = 'reqres-free-v1'; + private $restServerApiKey = 'live_U3JuUI7JmyksBMiYM5xsBH3ZkA8zNFwhIusK1Mwz80nrokn6UtC3XUM5D7T5fSkx'; public function setUp(): void { @@ -83,8 +83,8 @@ public function testHttpClientCreateMultiRequestFlow() $this->httpClient ->createMultiRequest() ->setHeader('x-api-key', $this->restServerApiKey) - ->addGet($this->restServer . '/users') - ->addPost($this->restServer . '/users') + ->addGet($this->restServer . '/breeds') + ->addPost($this->restServer . '/breeds') ->start(); $multiResponse = $this->httpClient->getResponse(); @@ -105,24 +105,24 @@ public function testHttpClientCreateAsyncMultiRequestFlow() function ($instance) { $this->assertFalse($instance->isError()); - $this->assertEquals($this->restServer . '/users', $instance->getUrl()); + $this->assertEquals($this->restServer . '/breeds', $instance->getUrl()); }, function ($instance) { $this->assertTrue($instance->isError()); - $this->assertEquals(404, $instance->getErrorCode()); + $this->assertEquals(405, $instance->getErrorCode()); } ) ->setHeader('x-api-key', $this->restServerApiKey) - ->addGet($this->restServer . '/users') - ->addPost($this->restServer . '/users') + ->addGet($this->restServer . '/breeds') + ->addPost($this->restServer . '/breeds') ->start(); } public function testHttpClientGetRequestHeaders() { $this->httpClient - ->createRequest($this->restServer . '/users') + ->createRequest($this->restServer . '/breeds/?limit=10&page=0') ->setHeaders([ 'x-api-key' => $this->restServerApiKey, 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', @@ -145,8 +145,9 @@ public function testHttpClientGetRequestHeaders() public function testHttpClientGetResponseHeaders() { $this->httpClient - ->createRequest($this->restServer . '/users') + ->createRequest($this->restServer . '/breeds/?limit=10&page=0') ->setHeader('x-api-key', $this->restServerApiKey) + ->setOpt(CURLOPT_FOLLOWLOCATION, true) ->start(); $this->assertIsArray($this->httpClient->getResponseHeaders()); @@ -186,39 +187,39 @@ public function testHttpClientGetTextResponseBody() public function testHttpClientGetObjectResponseBody() { $this->httpClient - ->createRequest($this->restServer . '/users') + ->createRequest($this->restServer . '/breeds/?limit=10&page=0') ->setHeader('x-api-key', $this->restServerApiKey) + ->setOpt(CURLOPT_FOLLOWLOCATION, true) ->start(); $responseBody = $this->httpClient->getResponseBody(); $this->assertNotNull($responseBody); - $this->assertIsObject($responseBody); + $this->assertIsArray($responseBody); } public function testHttpClientSendPostRequestAndGetResponseBody() { $this->httpClient - ->createRequest($this->restServer . '/users') + ->createRequest($this->restServer . '/images/search?format=json&limit=10') ->setHeader('x-api-key', $this->restServerApiKey) - ->setMethod('POST') + ->setOpt(CURLOPT_FOLLOWLOCATION, true) ->start(); $responseBody = $this->httpClient->getResponseBody(); - $this->assertNotNull($responseBody); + $imageId = $responseBody[0]->id; - $this->assertIsObject($responseBody); - } - - public function testHttpClientSendPostRequestWithDataAndGetResponseBody() - { $this->httpClient - ->createRequest($this->restServer . '/users') + ->createRequest($this->restServer . '/votes') ->setHeader('x-api-key', $this->restServerApiKey) + ->setHeader('Content-Type', 'application/json') ->setMethod('POST') - ->setData(['custom' => 'Custom value']) + ->setData([ + "image_id" => $imageId, + "value"=> 1, + ]) ->start(); $responseBody = $this->httpClient->getResponseBody(); @@ -227,7 +228,9 @@ public function testHttpClientSendPostRequestWithDataAndGetResponseBody() $this->assertIsObject($responseBody); - $this->assertEquals('Custom value', $responseBody->custom); + $this->assertObjectHasProperty('message', $responseBody); + + $this->assertEquals('SUCCESS', $responseBody->message); } public function testHttpClientGetError() @@ -267,7 +270,7 @@ public function testHttpClientMultiRequestGetError() public function testHttpClientCurlInfo() { $this->httpClient - ->createRequest($this->restServer . '/users') + ->createRequest($this->restServer . '/breeds') ->setHeader('x-api-key', $this->restServerApiKey) ->start(); @@ -275,19 +278,19 @@ public function testHttpClientCurlInfo() $this->assertEquals(200, $this->httpClient->info(CURLINFO_HTTP_CODE)); - $this->assertEquals('https://reqres.in/api/users', $this->httpClient->info(CURLINFO_EFFECTIVE_URL)); + $this->assertEquals('https://api.thedogapi.com/v1/breeds', $this->httpClient->info(CURLINFO_EFFECTIVE_URL)); } public function testHttpClientUrl() { $this->httpClient - ->createRequest($this->restServer . '/users') + ->createRequest($this->restServer . '/breeds') ->setHeader('x-api-key', $this->restServerApiKey) ->start(); $this->assertIsString($this->httpClient->url()); - $this->assertEquals('https://reqres.in/api/users', $this->httpClient->url()); + $this->assertEquals('https://api.thedogapi.com/v1/breeds', $this->httpClient->url()); } }