Skip to content

Commit

Permalink
#111. Change existing tests to support Illuminate\Http\Request
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Feb 2, 2019
1 parent fbb39ef commit 22ad01e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 77 deletions.
47 changes: 7 additions & 40 deletions tests/unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace rjapitest\unit;

use Faker\Factory;
use Illuminate\Foundation\Testing\TestCase as TestCaseLaravel;
use rjapi\ApiGenerator;
use rjapi\types\ConfigInterface;
use rjapi\types\DirsInterface;
use rjapi\types\JwtInterface;
use rjapi\types\PhpInterface;

/**
* Class TestCase
* @inheritdoc
* @package rjapitest\unit
*/
abstract class TestCase extends TestCaseLaravel
{
public const API_VERSION = 'v1';

public const CONFIG_KEY = 'v2';
public const MODULE_NAME = 'V2';
public const DIR_OUTPUT = './tests/_output/';
Expand Down Expand Up @@ -64,43 +70,4 @@ public function createConfig()
fwrite($fp, $str);
fclose($fp);
}

/**
* Fake generated provider for articles
*/
public function articleProvider()
{
$faker = Factory::create();

return [
[[
[
'id' => uniqid(),
'title' => $faker->title,
'fake_attr' => 'attr',
'description' => $faker->name,
'url' => $faker->url . uniqid('', true),
'topic_id' => $faker->randomNumber(),
'rate' => $faker->randomFloat(),
'status' => 'draft',
'show_in_top' => random_int(0, 1),
'date_posted' => $faker->date(),
'time_to_live' => $faker->time(),
],
[
'id' => uniqid(),
'title' => $faker->title,
'fake_attr' => 'attr',
'description' => $faker->name,
'url' => $faker->url . uniqid('', true),
'topic_id' => $faker->randomNumber(),
'rate' => $faker->randomFloat(),
'status' => 'draft',
'show_in_top' => random_int(0, 1),
'date_posted' => $faker->date(),
'time_to_live' => $faker->time(),
],
]],
];
}
}
117 changes: 80 additions & 37 deletions tests/unit/extension/BaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace rjapitest\unit\extensions;


use Faker\Factory;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Modules\V2\Entities\Article;
use Modules\V2\Http\Controllers\ArticleController;
use ReflectionException;
use rjapi\exceptions\AttributesException;
use rjapi\extension\BaseController;
use rjapi\extension\JSONApiInterface;
use rjapitest\_data\ArticleFixture;
use rjapitest\unit\TestCase;

Expand Down Expand Up @@ -50,42 +51,80 @@ public function setUp()
$this->baseController = new ArticleController($router);
}

/**
* Fake generated provider for articles
* @throws \Exception
*/
public function articleBulkProvider()
{
$faker = Factory::create();

return ['data' => [
[
'id' => uniqid(),
'title' => $faker->title,
'fake_attr' => 'attr',
'description' => $faker->name,
'url' => $faker->url . uniqid('', true),
'topic_id' => random_int(1, 127),
'rate' => random_int(1, 10),
'status' => 'draft',
'show_in_top' => random_int(0, 1),
'date_posted' => date('Y-m-d'),
'time_to_live' => $faker->time(),
],
[
'id' => uniqid(),
'title' => $faker->title,
'fake_attr' => 'attr',
'description' => $faker->name,
'url' => $faker->url . uniqid('', true),
'topic_id' => random_int(1, 127),
'rate' => random_int(1, 10),
'status' => 'draft',
'show_in_top' => random_int(0, 1),
'date_posted' => date('Y-m-d'),
'time_to_live' => $faker->time(),
],
],
];
}

/**
* @test
* @dataProvider articleProvider
* @runInSeparateProcess
* @preserveGlobalState disabled
* @param $data
* @throws \Exception
*/
public function it_creates_bulk($data)
public function it_creates_bulk()
{
try {
$this->baseController->createBulk(\rjapitest\unit\extensions\request($data));
} catch (AttributesException $e) {
echo $e->getTraceAsString();
}
$data = $this->articleBulkProvider();
$resp = $this->baseController->createBulk(\rjapitest\unit\extensions\request($data));

$this->assertInstanceOf(BaseController::class, $this->baseController);
$respJson = json_decode($resp->getContent(), true)['data'];
foreach ($data['data'] as $k => $v) {
$this->assertEquals($v['id'], $respJson[$k]['id']);
}
}

/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
* @throws AttributesException
*/
public function it_updates_bulk()
{
/** @var Article $firstItem */
$firstItem = ArticleFixture::createAndGet();
$secondItem = ArticleFixture::createAndGet();

// change values
$faker = Factory::create();

$data = [
'data' => [
[
'type' => 'article',
'id' => $firstItem->id,
'title' => $firstItem->title,
'description' => $firstItem->description,
'title' => $faker->title,
'description' => $faker->name,
'fake_attr' => 'attr',
'url' => $firstItem->url,
'show_in_top' => $firstItem->show_in_top,
Expand All @@ -97,8 +136,8 @@ public function it_updates_bulk()
[
'type' => 'article',
'id' => $secondItem->id,
'title' => $secondItem->title,
'description' => $secondItem->description,
'title' => $faker->title,
'description' => $faker->name,
'fake_attr' => 'attr',
'url' => $secondItem->url,
'show_in_top' => $secondItem->show_in_top,
Expand All @@ -110,19 +149,16 @@ public function it_updates_bulk()
],
];

try {
$this->baseController->updateBulk(\rjapitest\unit\extensions\request($data));
} catch (AttributesException $e) {
echo $e->getTraceAsString();
}
$resp = $this->baseController->updateBulk(\rjapitest\unit\extensions\request($data));

$this->assertInstanceOf(BaseController::class, $this->baseController);
$respJson = json_decode($resp->getContent(), true)['data'];
foreach ($data['data'] as $k => $v) {
$this->assertEquals($v['title'], $respJson[$k]['attributes']['title']);
}
}

/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function it_deletes_bulk()
{
Expand All @@ -146,31 +182,33 @@ public function it_deletes_bulk()
],
];

$this->baseController->deleteBulk(\rjapitest\unit\extensions\request($data));
$this->assertInstanceOf(BaseController::class, $this->baseController);
$resp = $this->baseController->deleteBulk(\rjapitest\unit\extensions\request($data));
$this->assertEquals($resp->getStatusCode(), JSONApiInterface::HTTP_RESPONSE_CODE_NO_CONTENT);
}

/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
* @throws AttributesException
*/
public function it_runs_index()
{
$router = new Route(['GET'], '/v2/article?include=tag&data=["title", "description"]', function () {
$router = new Route(['GET'], '/' . self::API_VERSION . '/article?include=tag&data=["title", "description"]', function () {
});
$router->setAction(['controller' => 'ArticleController@index']);
$this->baseController = new ArticleController($router);

$this->baseController->index(\rjapitest\unit\extensions\request());
$this->assertInstanceOf(BaseController::class, $this->baseController);
$req = new Request();
$req->initialize([
'include' => 'tag',
]);
$resp = $this->baseController->index($req);

// @todo: Change simple 200 OK check to more complex tests
$this->assertEquals($resp->getStatusCode(), JSONApiInterface::HTTP_RESPONSE_CODE_OK);
}

/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
* @throws AttributesException
*/
public function it_runs_view()
Expand All @@ -184,7 +222,12 @@ public function it_runs_view()

$this->baseController = new ArticleController($router);

$this->baseController->view(\rjapitest\unit\extensions\request(), $item->id);
$this->assertInstanceOf(BaseController::class, $this->baseController);
$resp = $this->baseController->view(\rjapitest\unit\extensions\request(), $item->id);

// @todo: Change simple 200 OK check to more complex tests
$this->assertEquals($resp->getStatusCode(), JSONApiInterface::HTTP_RESPONSE_CODE_OK);
}

// @todo: create/update/delete
// @todo: relations/updateRelations/deleteRelations
}

0 comments on commit 22ad01e

Please sign in to comment.