Skip to content

Commit

Permalink
Fix + Test Sprunje with Relation Query
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Dec 27, 2023
1 parent e2970d6 commit a429064
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
7 changes: 5 additions & 2 deletions app/src/Sprunje/Sprunje.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ public function __construct(array $options = [])
$this->setOptions($options);

// Start a new query on any Model instances
if (is_a($query = $this->baseQuery(), Model::class)) {
$query = $this->baseQuery();
if (is_a($query, Model::class)) {
$this->query = $query->newQuery();
} elseif (is_a($query, Relation::class)) {
$this->query = $query->getQuery();
} else {
$this->query = $query;
}
Expand Down Expand Up @@ -633,7 +636,7 @@ protected function applyTransformations(Collection $collection): Collection
/**
* Set the initial query used by your Sprunje.
*
* @return EloquentBuilder|QueryBuilder|Model
* @return EloquentBuilder|QueryBuilder|Model|Relation
*/
abstract protected function baseQuery();

Expand Down
79 changes: 77 additions & 2 deletions app/tests/Integration/Sprunje/SprunjeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace UserFrosting\Sprinkle\Core\Tests\Integration\Sprunje;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Schema\Blueprint;
Expand Down Expand Up @@ -53,6 +54,8 @@ public function setUp(): void
$builder = $this->ci->get(Builder::class);
$migration = new TestTableMigration($builder);
$migration->up();
$migration = new TestRelationTableMigration($builder);
$migration->up();

// Insert some data
$this->createData();
Expand All @@ -65,6 +68,8 @@ public function tearDown(): void
$builder = $this->ci->get(Builder::class);
$migration = new TestTableMigration($builder);
$migration->down();
$migration = new TestRelationTableMigration($builder);
$migration->down();

parent::tearDown();
}
Expand Down Expand Up @@ -92,6 +97,12 @@ protected function createData(): void
'type' => 1,
'active' => true
]))->save();

(new TestSprunjeRelationModel([
'id' => 1,
'name' => 'Relation with 2',
'test_sprunje_id' => 2,
]))->save();
}

public function testBaseSprunje(): void
Expand Down Expand Up @@ -451,6 +462,21 @@ public function testToResponseWithJson(): void
], $response);
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
}

/** Sprunje were the base query is a relation */
public function testRelationSprunje(): void
{
$sprunje = new RelationTestSprunje();

$this->assertEquals([
'count' => 1,
'count_filtered' => 1,
'rows' => [
['id' => 2, 'name' => 'bar', 'description' => 'Le Bar', 'type' => 2, 'active' => false],
],
'listable' => [],
], $sprunje->getArray());
}
}

class TestSprunje extends Sprunje
Expand Down Expand Up @@ -495,12 +521,12 @@ protected function applyTransformations(Collection $collection): Collection
return $collection;
}

protected function sortName(EloquentBuilder|QueryBuilder|Relation $query, string $direction): static
protected function sortName(EloquentBuilder|QueryBuilder $query, string $direction): static
{
return $this;
}

protected function filterName(EloquentBuilder|QueryBuilder|Relation $query, string $value): static
protected function filterName(EloquentBuilder|QueryBuilder $query, string $value): static
{
return $this;
}
Expand Down Expand Up @@ -535,6 +561,19 @@ protected function applyTransformations(Collection $collection): Collection
}
}

/** Sprunje were the base query is a relation. Will return the same as TestSprunje */
class RelationTestSprunje extends Sprunje
{
protected function baseQuery()
{
/** @var TestSprunjeRelationModel */
$query = TestSprunjeRelationModel::findOrFail(1);
$query = $query->testSprunje();

return $query;
}
}

class TestSprunjeModel extends UfModel
{
protected $table = 'test_sprunje';
Expand All @@ -557,6 +596,25 @@ class TestSprunjeModel extends UfModel
];
}

class TestSprunjeRelationModel extends UfModel
{
protected $table = 'test_sprunje_relation';

protected $fillable = [
'id',
'test_sprunje_id',
'name',
];

/** @var bool */
public $timestamps = false;

public function testSprunje(): BelongsTo
{
return $this->belongsTo(TestSprunjeModel::class);
}
}

/**
* Custom migration for testing.
*/
Expand All @@ -578,3 +636,20 @@ public function down(): void
$this->schema->drop('test_sprunje');
}
}

class TestRelationTableMigration extends Migration
{
public function up(): void
{
$this->schema->create('test_sprunje_relation', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('test_sprunje_id')->unsigned();
});
}

public function down(): void
{
$this->schema->drop('test_sprunje_relation');
}
}

0 comments on commit a429064

Please sign in to comment.