2 Commits 745983a926 ... 7e9a2f314e

Author SHA1 Message Date
  Felix Freeman 7e9a2f314e Complete Controller tests 4 years ago
  Felix Freeman e9bc1cbc7c Minor comment 4 years ago

+ 6 - 1
src/Http/Controllers/Controller.php

@@ -12,7 +12,12 @@ use Laravel\Lumen\Routing\Controller as BaseController;
 
 class Controller extends BaseController
 {
-    // Right now I don't find any reason to not return the plain object
+    /**
+     * Right now I don't find any reason to not return the plain object
+     * This is probably the dumbest function ever.
+     * Kept for historical reasons, deprecated.
+     * @deprecated
+     */
     protected function singleResponse(object $object): object
     {
         return $object;

+ 1 - 1
src/Http/Controllers/Paginator.php

@@ -48,7 +48,7 @@ class Paginator
         }
         return $this->current_page - 1;
     }
-    
+
     private function nextPage()
     {
         if ($this->current_page === $this->totalPages()) {

+ 138 - 0
tests/ControllerTest.php

@@ -0,0 +1,138 @@
+<?php
+
+// Copyright 2019 Hackware SpA <human@hackware.cl>
+// "Hackware Web Services Core" is released under the MIT License terms.
+
+namespace Hawese\Tests;
+
+use Illuminate\Http\Request;
+use Laravel\Lumen\Testing\DatabaseTransactions;
+
+class ControllerTest extends TestCase
+{
+    use DatabaseTransactions;
+
+    public function setUp(): void
+    {
+        parent::setUp();
+
+        $this->dumbController = new DumbController();
+        $this->request = new Request();
+    }
+
+    public function testSingleResponse()
+    {
+        $obj = (object) ['any' => 'thing'];
+        $this->assertEqualsCanonicalizing(
+            $obj,
+            $this->dumbController->singleResponse($obj)
+        );
+    }
+
+    public function testCollectionResponsePage()
+    {
+        for ($i = 0; $i < 30; $i++) {
+            (new DumbTableModel([
+                'attr1' => bin2hex(random_bytes(12))
+            ]))->insert();
+        }
+
+        $this->request->query->set('page', 2);
+
+        $response = $this->dumbController->collectionResponse(
+            $this->request,
+            DumbTableModel::class
+        );
+
+        $this->assertSame(3, $response['meta']['total_pages']);
+        $this->assertSame(10, count($response['data']));
+    }
+
+    public function testCollectionResponseLimit()
+    {
+        for ($i = 0; $i < 30; $i++) {
+            (new DumbTableModel([
+                'attr1' => bin2hex(random_bytes(12))
+            ]))->insert();
+        }
+
+        $this->request->query->set('limit', 5);
+
+        $response = $this->dumbController->collectionResponse(
+            $this->request,
+            DumbTableModel::class
+        );
+
+        $this->assertSame(6, $response['meta']['total_pages']);
+        $this->assertSame(5, count($response['data']));
+    }
+
+    public function testCollectionResponseSort()
+    {
+        for ($i = 0; $i < 15; $i++) {
+            (new DumbTableModel([
+                'attr1' => bin2hex(random_bytes(12)),
+                'attr2' => chr(65 + $i)
+            ]))->insert();
+        }
+
+        $this->request->query->set('sort', 'attr2');
+        $response = $this->dumbController->collectionResponse(
+            $this->request,
+            DumbTableModel::class
+        );
+        $this->assertSame('A', $response['data'][0]['attr2']);
+
+        $this->request->query->set('sort', '-attr2');
+        $response = $this->dumbController->collectionResponse(
+            $this->request,
+            DumbTableModel::class
+        );
+        $this->assertSame('O', $response['data'][0]['attr2']);
+    }
+
+    public function testCollectionResponseInclude()
+    {
+        (new DumbTableModel([
+            'attr1' => bin2hex(random_bytes(12)),
+            'foreign_id' => (new ForeignTableModel())->insert(),
+            'other_foreign_id' => (new ForeignTableModel())->insert()
+        ]))->insert();
+
+        $this->request->query->set('include', 'foreign');
+        $response = $this->dumbController->collectionResponse(
+            $this->request,
+            DumbTableModel::class
+        );
+        $this->assertArrayHasKey('foreign', $response['data'][0]);
+        $this->assertArrayNotHasKey('other_foreign', $response['data'][0]);
+
+        $this->request->query->set('include', 'foreign,other_foreign');
+        $response = $this->dumbController->collectionResponse(
+            $this->request,
+            DumbTableModel::class
+        );
+        $this->assertArrayHasKey('other_foreign', $response['data'][0]);
+    }
+
+    public function testCollectionResponseFiltersCallback()
+    {
+        for ($i = 0; $i < 5; $i++) {
+            (new DumbTableModel([
+                'attr1' => bin2hex(random_bytes(12))
+            ]))->insert();
+        }
+
+        $attr1 = DumbTableModel::select()->inRandomOrder()->first()->attr1;
+
+        $response = $this->dumbController->collectionResponse(
+            $this->request,
+            DumbTableModel::class,
+            function ($query) use ($attr1) {
+                return $query->where('attr1', $attr1);
+            }
+        );
+
+        $this->assertSame($attr1, $response['data'][0]['attr1']);
+    }
+}

+ 29 - 0
tests/DumbController.php

@@ -0,0 +1,29 @@
+<?php
+
+// Copyright 2019 Hackware SpA <human@hackware.cl>
+// "Hackware Web Services Core" is released under the MIT License terms.
+
+namespace Hawese\Tests;
+
+use Illuminate\Http\Request;
+use Hawese\Core\Http\Controllers\Controller;
+
+/**
+ * Just a dumb class, exposes private methods of Controller.
+ * I want this methods to be private, but also need to test them.
+ */
+class DumbController extends Controller
+{
+    public function singleResponse($obj): object
+    {
+        return parent::singleResponse($obj);
+    }
+
+    public function collectionResponse(
+        Request $request,
+        $model,
+        $filters_cb = null
+    ): array {
+        return parent::collectionResponse($request, $model, $filters_cb);
+    }
+}

+ 5 - 1
tests/DumbTableModel.php

@@ -21,9 +21,13 @@ class DumbTableModel extends TableModel
         'deleted_at' => [],
         'else_at' => [],
         'foreign_id' => [],
+        'other_foreign_id' => [],
     ];
 
-    public static $foreign_keys = ['foreign_id' => ForeignTableModel::class];
+    public static $foreign_keys = [
+        'foreign_id' => ForeignTableModel::class,
+        'other_foreign_id' => ForeignTableModel::class,
+    ];
 
     protected function getCustomGetter()
     {

+ 4 - 0
tests/db_migrations/2019_12_02_124500_create_tablemodel_test_tables.php

@@ -32,6 +32,10 @@ class CreateTablemodelTestTables extends Migration
             $table->timestamp('else_at')->nullable();
             $table->integer('foreign_id')->unsigned()->nullable();
             $table->foreign('foreign_id')->references('id')->on('foreigns');
+            $table->integer('other_foreign_id')->unsigned()->nullable();
+            $table->foreign('other_foreign_id')
+                  ->references('id')
+                  ->on('foreigns');
         });
     }