Skip to content

Commit

Permalink
Merge pull request #67 from WendellAdriel/fix-model-conflicts
Browse files Browse the repository at this point in the history
Fix Model Conflicts
  • Loading branch information
WendellAdriel committed Oct 2, 2023
2 parents 0cca806 + f076f97 commit 0b10e4c
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"lint": "pint",
"test:lint": "pint --test",
"test:unit": "./vendor/bin/pest --order-by random",
"test:static": "phpstan analyse",
"test:static": "phpstan analyse --memory-limit 1G",
"test": [
"@test:lint",
"@test:unit",
Expand Down
38 changes: 17 additions & 21 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>

<php>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<php>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
31 changes: 31 additions & 0 deletions src/Lift.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,37 @@ public function toArray(): array
return $result;
}

public function setCreatedAt($value)
{
$createdAtColumn = $this->getCreatedAtColumn();

$this->{$createdAtColumn} = $value;
$this->setAttribute($createdAtColumn, $value);

return $this;
}

public function setUpdatedAt($value)
{
$updatedAtColumn = $this->getUpdatedAtColumn();

$this->{$updatedAtColumn} = $value;
$this->setAttribute($updatedAtColumn, $value);

return $this;
}

public function setUniqueIds()
{
foreach ($this->uniqueIds() as $column) {
if (empty($this->{$column})) {
$uniqueId = $this->newUniqueId();
$this->{$column} = $uniqueId;
$this->setAttribute($column, $uniqueId);
}
}
}

protected static function ignoredProperties(): array
{
return [
Expand Down
19 changes: 19 additions & 0 deletions tests/Datasets/Crew.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Datasets;

use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Model;
use WendellAdriel\Lift\Attributes\PrimaryKey;
use WendellAdriel\Lift\Lift;

final class Crew extends Model
{
use HasUlids;
use Lift;

#[PrimaryKey(type: 'string', incrementing: false)]
public string $id;
}
19 changes: 19 additions & 0 deletions tests/Datasets/Game.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Datasets;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use WendellAdriel\Lift\Attributes\PrimaryKey;
use WendellAdriel\Lift\Lift;

final class Game extends Model
{
use HasUuids;
use Lift;

#[PrimaryKey(type: 'string', incrementing: false)]
public string $id;
}
37 changes: 37 additions & 0 deletions tests/Datasets/PostTimestamps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tests\Datasets;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use WendellAdriel\Lift\Attributes\Cast;
use WendellAdriel\Lift\Attributes\DB;
use WendellAdriel\Lift\Attributes\Fillable;
use WendellAdriel\Lift\Attributes\PrimaryKey;
use WendellAdriel\Lift\Lift;

#[DB(table: 'posts')]
class PostTimestamps extends Model
{
use Lift;

#[PrimaryKey]
public int $id;

#[Fillable]
public string $title;

#[Fillable]
public string $content;

#[Fillable]
public ?int $user_id;

#[Cast('datetime')]
public Carbon $created_at;

#[Cast('datetime')]
public Carbon $updated_at;
}
16 changes: 16 additions & 0 deletions tests/Feature/PrimaryKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Tests\Datasets\Crew;
use Tests\Datasets\Game;
use Tests\Datasets\Movie;
use Tests\Datasets\User;
use Tests\Datasets\UserCustom;
Expand Down Expand Up @@ -181,3 +183,17 @@
expect($movie->id)->toBe('123e4567-e89b-12d3-a456-426614174000');
});
});

it('sets default ULID primary key column', function () {
$crew = new Crew();
$crew->save();

expect($crew->id)->not->toBeNull();
});

it('sets default UUID primary key column', function () {
$game = new Game();
$game->save();

expect($game->id)->not->toBeNull();
});
33 changes: 33 additions & 0 deletions tests/Feature/TimestampsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use Tests\Datasets\PostTimestamps;

it('should set timestamps on create', function () {
PostTimestamps::create([
'title' => 'Test',
'content' => 'Test',
]);

$post = PostTimestamps::first();
expect($post->created_at)->not->toBeNull();
expect($post->updated_at)->not->toBeNull();
});

it('should set timestamps on update', function () {
$post = PostTimestamps::create([
'title' => 'Test',
'content' => 'Test',
]);
sleep(2); // Wait 2 seconds to update
$post->title = 'Test 2';
$post->save();
$post = $post->fresh();

expect($post->created_at)->not->toBeNull();
expect($post->updated_at)->not->toBeNull();
expect($post->created_at)->not->toEqual($post->updated_at);
expect($post->updated_at)->toBeGreaterThan($post->created_at);
expect($post->title)->toBe('Test 2');
});
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ protected function setUp(): void
$table->softDeletes();
$table->timestamps();
});

Schema::create('crews', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->timestamps();
});

Schema::create('games', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->timestamps();
});
}

protected function getPackageProviders($app)
Expand Down

0 comments on commit 0b10e4c

Please sign in to comment.