diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index dab937b..6f4dde1 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -2,7 +2,6 @@ namespace rjapitest\unit; -use Faker\Factory; use Illuminate\Foundation\Testing\TestCase as TestCaseLaravel; use rjapi\ApiGenerator; use rjapi\types\ConfigInterface; @@ -10,8 +9,15 @@ 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/'; @@ -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(), - ], - ]], - ]; - } } \ No newline at end of file diff --git a/tests/unit/extension/BaseControllerTest.php b/tests/unit/extension/BaseControllerTest.php index ad682c9..665677b 100644 --- a/tests/unit/extension/BaseControllerTest.php +++ b/tests/unit/extension/BaseControllerTest.php @@ -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; @@ -50,28 +51,63 @@ 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() { @@ -79,13 +115,16 @@ public function it_updates_bulk() $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, @@ -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, @@ -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() { @@ -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() @@ -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 } \ No newline at end of file