Skip to content

Commit

Permalink
Add integration tests (#3)
Browse files Browse the repository at this point in the history
Add integration tests
  • Loading branch information
dzava authored Sep 19, 2020
1 parent 5943c98 commit 0579a98
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
<testsuite name="Unit tests">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Integration tests">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
71 changes: 71 additions & 0 deletions tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Dzava\ResourceIterator\Tests\Integration;

use Dzava\ResourceIterator\PagedResourceIterator;
use Dzava\ResourceIterator\ResourceIterator;
use PHPUnit\Framework\TestCase;

class IntegrationTest extends TestCase
{
/** @var ResourceIterator $resource */
protected $resource;

private $firstPageResults = ['George', 'Janet', 'Emma', 'Eve', 'Charles', 'Tracey'];
private $secondPageResults = ['Michael', 'Lindsay', 'Tobias', 'Byron', 'George', 'Rachel'];
private $allResults;

protected function setUp()
{
parent::setUp();

$this->resource = $this->getPagedResourceIterator('https://reqres.in/api/users');

$this->allResults = array_merge($this->firstPageResults, $this->secondPageResults);
}

/** @test */
public function can_fetch_all_pages()
{
$items = $this->resource->toArray();

$this->assertEquals($this->allResults, $items);
}

/** @test */
public function can_limit_the_max_number_of_requests()
{
$this->resource->maxRequests(1);

$this->assertEquals($this->firstPageResults, $this->resource->toArray());
}

/** @test */
public function can_start_at_a_page_that_is_not_the_first_one()
{
$this->resource = $this->getPagedResourceIterator('https://reqres.in/api/users?page=2');

$this->assertEquals($this->secondPageResults, $this->resource->toArray());
}

/** @test */
public function does_not_fail_if_the_response_is_empty()
{
$this->resource = $this->getPagedResourceIterator('https://reqres.in/api/users?page=100');

$this->assertEmpty($this->resource->toArray());
}

protected function getPagedResourceIterator($url)
{
return new class($url) extends PagedResourceIterator {
public function toArray()
{
return array_map(function ($item) {
return $item->first_name;
}, parent::toArray());
}
};
}

}
4 changes: 2 additions & 2 deletions tests/PagedResourceTest.php → tests/Unit/UnitTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace Tests\Feature;
namespace Dzava\ResourceIterator\Tests\Unit;

use Dzava\ResourceIterator\PagedResourceIterator;
use Dzava\ResourceIterator\ResourceIterator;
use Illuminate\Support\Arr;
use PHPUnit\Framework\TestCase;
use stdClass;

class PagedResourceTest extends TestCase
class UnitTest extends TestCase
{
/** @var ResourceIterator $resource */
protected $resource;
Expand Down

0 comments on commit 0579a98

Please sign in to comment.