Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Paginator/Adapters/ModelPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
57 changes: 30 additions & 27 deletions tests/Unit/Libraries/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand All @@ -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',
Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -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()
Expand Down Expand Up @@ -267,27 +270,27 @@ public function testHttpClientMultiRequestGetError()
public function testHttpClientCurlInfo()
{
$this->httpClient
->createRequest($this->restServer . '/users')
->createRequest($this->restServer . '/breeds')
->setHeader('x-api-key', $this->restServerApiKey)
->start();

$this->assertIsArray($this->httpClient->info());

$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());
}
}
41 changes: 38 additions & 3 deletions tests/Unit/Paginator/Adapters/ModelPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
Expand Down