Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more syntax tests for type uuid / no increment #108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions tests/ComposhipsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Awobaz\Compoships\Tests\Models\TrackingTask;
use Awobaz\Compoships\Tests\Models\User;
use Awobaz\Compoships\Tests\TestCase\TestCase;
use Illuminate\Database\Eloquent\Model as Model;
use Illuminate\Database\Eloquent\Model;

/**
* @covers \Awobaz\Compoships\Compoships
Expand All @@ -36,8 +36,6 @@ public function testSave()

$this->assertNotNull($allocation->trackingTasks);
$this->assertInstanceOf(Allocation::class, $allocation->trackingTasks->first()->allocation);

Model::reguard();
}

/**
Expand All @@ -56,8 +54,6 @@ public function testSaveModelNotUsingCompoships_onHasOne()
->save(new Space());

$this->assertNotNull($allocation->space);

Model::reguard();
}

/**
Expand All @@ -78,8 +74,6 @@ public function testSaveWithANullValue()
$this->assertNotNull($allocation->trackingTasks);
$this->assertTrue($allocation->trackingTasks->isNotEmpty());
$this->assertInstanceOf(Allocation::class, $allocation->trackingTasks->first()->allocation);

Model::reguard();
}

/**
Expand All @@ -100,8 +94,6 @@ public function testARelationshipWithOnlyNullValuesIsNotSupported()
$this->assertNotNull($allocation->trackingTasks);
$this->assertTrue($allocation->trackingTasks->isEmpty());
$this->assertNull(TrackingTask::first()->allocation);

Model::reguard();
}

/**
Expand All @@ -119,8 +111,6 @@ public function testARelationshipWithAForeignKeyIsEmptyOnANewInstance()

$this->assertNotNull($user->allocations);
$this->assertTrue($user->allocations->isEmpty());

Model::reguard();
}

/**
Expand All @@ -140,8 +130,6 @@ public function testCreate()

$this->assertNotNull($allocation->trackingTasks);
$this->assertInstanceOf(Allocation::class, $allocation->trackingTasks->first()->allocation);

Model::reguard();
}

/**
Expand All @@ -161,8 +149,6 @@ public function testMake()

$this->assertNotNull($trackingTask);
$this->assertInstanceOf(Allocation::class, $trackingTask->allocation);

Model::reguard();
}

public function testHas()
Expand Down Expand Up @@ -220,8 +206,6 @@ public function testMixedTypeCompositeKey()
]);

$this->assertNotNull($pickupPoint->pickupTimes);

Model::reguard();
}

public function testHasForSelfRelation()
Expand Down Expand Up @@ -254,7 +238,5 @@ public function testAssociateOnbelongsTo()
$allocation->user()->associate($user);

$this->assertNotNull($allocation->user);

Model::reguard();
}
}
12 changes: 12 additions & 0 deletions tests/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ public function up()
// contains original single PK relations
Capsule::schema()->create('original_packages', function (Blueprint $table) {
$table->increments('id');
$table->string('pcid')->nullable();
$table->string('name')->nullable();
$table->integer('allocation_id');

$table->foreign('allocation_id')
->references('id')
->on('allocations')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreign('pcid')
->references('pcid')
->on('product_codes')
->onUpdate('cascade')
->onDelete('cascade');
});

Capsule::schema()->create('spaces', function (Blueprint $table) {
Expand Down Expand Up @@ -104,5 +111,10 @@ public function up()
->nullable();
$table->timestamps();
});

Capsule::schema()->create('product_codes', function (Blueprint $table) {
$table->uuid('pcid')->unique();
$table->string('code');
});
}
}
23 changes: 21 additions & 2 deletions tests/Models/OriginalPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property int $id
* @property string $name
* @property int $allocation_id
* @property int $allocation_id
* @property-read string $pcid
* @property-read Allocation $allocation
* @property-read ProductCode $productCode
* @property-read ProductCode $productCode2
*
* @mixin \Illuminate\Database\Eloquent\Builder
*/
Expand All @@ -26,4 +29,20 @@ public function allocation()
{
return $this->belongsTo(Allocation::class);
}

/**
* @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
*/
public function productCode()
{
return $this->belongsTo(ProductCode::class, 'pcid', 'pcid');
}

/**
* @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
*/
public function productCode2()
{
return $this->belongsTo(ProductCode::class, ['pcid'], ['pcid']);
}
}
22 changes: 22 additions & 0 deletions tests/Models/ProductCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Awobaz\Compoships\Tests\Models;

use Awobaz\Compoships\Compoships;
use Illuminate\Database\Eloquent\Model;

/**
* @property string $pcid
* @property string $code
*
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class ProductCode extends Model
{
use Compoships;

public $timestamps = false;
public $incrementing = false;
protected $keyType = 'string';
protected $table = 'product_codes';
}
2 changes: 2 additions & 0 deletions tests/TestCase/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Awobaz\Compoships\Tests\Migration;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase
{
protected function setupDatabase()
{
Model::reguard();
Carbon::setTestNow('2020-10-29 23:59:59');

$capsuleManager = new Capsule();
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/BelongsToTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Awobaz\Compoships\Tests\Unit;

use Awobaz\Compoships\Tests\Models\Allocation;
use Awobaz\Compoships\Tests\Models\OriginalPackage;
use Awobaz\Compoships\Tests\Models\ProductCode;
use Awobaz\Compoships\Tests\TestCase\TestCase;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Eloquent\Model;

class BelongsToTest extends TestCase
{
/**
* @covers \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
*/
public function test_uuid_no_inrecemnt_relation()
{
Model::unguard();

$pcid = uniqid();
$productCode = new ProductCode([
'pcid' => $pcid,
'code' => 'AAA-BBB-CCC',
]);
$productCode->save();

$allocation = new Allocation();
$allocation->save();

/** @var OriginalPackage $package */
$package = $allocation->originalPackages()->create([
'pcid' => $pcid,
]);

$dbPackage = Capsule::table('original_packages')->find($package->id);

$this->assertEquals(1, Capsule::table('original_packages')->count());
$this->assertNotNull($dbPackage);
$this->assertEquals($pcid, $package->pcid);
$this->assertEquals($pcid, $dbPackage->pcid);
$this->assertInstanceOf(ProductCode::class, $package->productCode);
$this->assertEquals($pcid, $package->productCode->pcid);

$this->assertEquals($package->productCode, $package->productCode2);
}
}
21 changes: 11 additions & 10 deletions tests/Unit/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ public function test_Compoships_hasOneOrMany_create__fail_set_unguarded()
public function test_Compoships_hasOneOrMany_create__change_relation_columns()
{
$allocation = $this->createAllocation();
$allocation::unguard(true);
$allocation::unguard();
$package = $allocation->trackingTasks()->create(['booking_id' => 123]);
$allocation::unguard(false);
$package->refresh();
$this->assertEquals([
'id' => (string) 1,
Expand All @@ -180,9 +179,8 @@ public function test_Compoships_hasOneOrMany_create__change_relation_columns()
public function test_Compoships_hasOneOrMany_create__normal()
{
$allocation = $this->createAllocation();
$allocation::unguard(true);
$allocation::unguard();
$trackingTask = $allocation->trackingTasks()->create(['created_at' => Carbon::now()->addDay()]);
$allocation::unguard(false);
$trackingTask->refresh();
$this->assertEquals([
'id' => (string) 1,
Expand Down Expand Up @@ -278,28 +276,28 @@ public function test_Illuminate_eagerLoading()
public function test_Illuminate_hasOneOrMany_create__normal()
{
$allocation = $this->createAllocation();
$allocation::unguard(true);
$allocation::unguard();
$package = $allocation->originalPackages()->create(['name' => 'some name']);
$allocation::unguard(false);
$package->refresh();
$this->assertEquals([
'id' => (string) 1,
'allocation_id' => (string) 1,
'name' => 'some name',
'pcid' => null,
], $package->toArray());
}

public function test_Illuminate_hasOneOrMany_create__change_relation_columns()
{
$allocation = $this->createAllocation();
$allocation::unguard(true);
$allocation::unguard();
$package = $allocation->originalPackages()->create(['allocation_id' => 123]);
$allocation::unguard(false);
$package->refresh();
$this->assertEquals([
'id' => (string) 1,
'allocation_id' => (string) 1, // correct, as it was not changed
'name' => null,
'pcid' => null,
], $package->toArray());
}

Expand All @@ -312,6 +310,7 @@ public function test_Illuminate_hasOneOrMany_create__empty()
'id' => (string) 1,
'allocation_id' => (string) 1,
'name' => null,
'pcid' => null,
], $package->toArray());
}

Expand All @@ -330,27 +329,29 @@ public function test_Illuminate_hasOneOrMany_saveMany()
'id' => (string) 1,
'allocation_id' => (string) 1,
'name' => 'name 1',
'pcid' => null,
],
[
'id' => (string) 2,
'allocation_id' => (string) 1,
'name' => 'name 2',
'pcid' => null,
],
[
'id' => (string) 3,
'allocation_id' => (string) 1,
'name' => 'name 3',
'pcid' => null,
],
];

$allocation = $this->createAllocation(1, 1);
$allocation::unguard(true);
$allocation::unguard();
$allocation->originalPackages()->saveMany([
new OriginalPackage(['name' => 'name 1']),
new OriginalPackage(['name' => 'name 2']),
new OriginalPackage(['name' => 'name 3']),
]);
$allocation::unguard(false);
$allocation->refresh();
$this->assertNotNull($allocation->originalPackages);
$this->assertEquals(count($expectedData), $allocation->originalPackages->count());
Expand Down