Skip to content

Commit

Permalink
Merge pull request #2 from rcrosbourne/feature/add-hasuuid-trait-to-m…
Browse files Browse the repository at this point in the history
…odel

test: add tests for hasuuids trait
  • Loading branch information
rcrosbourne authored Dec 6, 2023
2 parents 73c8090 + 9ad1d23 commit 70ae6af
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/Feature/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,38 @@ public function output_generates_models_with_custom_pivot(): void
$this->assertEquals(['created' => ['app/Models/User.php', 'app/Models/Team.php', 'app/Models/Membership.php']], $this->subject->output($tree));
}

#[Test]
public function output_generates_models_with_hasuuids_trait_if_uuid_id_is_type_uuid(): void
{
$this->filesystem->expects('stub')
->with('model.class.stub')
->andReturn($this->stub('model.class.stub'));
$this->filesystem->expects('stub')
->times(1)
->with('model.casts.stub')
->andReturn($this->stub('model.casts.stub'));
$this->filesystem->expects('stub')
->times(1)
->with('model.fillable.stub')
->andReturn($this->stub('model.fillable.stub'));
$this->filesystem->expects('stub')
->times(1)
->with('model.method.stub')
->andReturn($this->stub('model.method.stub'));

$this->filesystem->expects('exists')
->times(1)
->with('app/Models')
->andReturnTrue();

$this->filesystem->expects('put')
->with('app/Models/User.php', $this->fixture('models/model-with-uuid-trait.php'));
$tokens = $this->blueprint->parse($this->fixture('drafts/model-with-uuid-id.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => ['app/Models/User.php']], $this->subject->output($tree));
}

public static function modelTreeDataProvider(): array
{
return [
Expand All @@ -610,6 +642,7 @@ public static function modelTreeDataProvider(): array
['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/Models/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'],
['drafts/model-with-meta.yaml', 'app/Models/Post.php', 'models/model-with-meta.php'],
['drafts/infer-belongsto.yaml', 'app/Models/Conference.php', 'models/infer-belongsto.php'],
['drafts/model-with-uuid-id.yaml', 'app/Models/User.php', 'models/model-with-uuid-trait.php'],
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/drafts/model-with-uuid-id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
models:
User:
id: uuid primary
name: string
base_pay: decimal:10,2
31 changes: 31 additions & 0 deletions tests/fixtures/models/model-with-uuid-trait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
use HasFactory, HasUuids;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'base_pay',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'base_pay' => 'decimal:2',
];
}

0 comments on commit 70ae6af

Please sign in to comment.