|
| 1 | +<?php |
| 2 | + |
| 3 | +use DirectoryTree\Metrics\Commands\MakeMetricsModel; |
| 4 | +use Orchestra\Testbench\Concerns\InteractsWithPublishedFiles; |
| 5 | + |
| 6 | +use function Pest\Laravel\artisan; |
| 7 | + |
| 8 | +uses(InteractsWithPublishedFiles::class); |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + $this->files = [ |
| 12 | + 'app/CustomMetric.php', |
| 13 | + 'app/UserMetric.php', |
| 14 | + 'app/Models/CustomMetric.php', |
| 15 | + 'app/Models/UserMetric.php', |
| 16 | + 'app/Metrics/CustomMetric.php', |
| 17 | + ]; |
| 18 | +}); |
| 19 | + |
| 20 | +it('creates a model file with the correct name', function () { |
| 21 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 22 | + ->assertSuccessful(); |
| 23 | + |
| 24 | + $this->assertFilenameExists('app/CustomMetric.php'); |
| 25 | +}); |
| 26 | + |
| 27 | +it('displays success message with model name', function () { |
| 28 | + artisan(MakeMetricsModel::class, ['name' => 'UserMetric']) |
| 29 | + ->expectsOutputToContain('created successfully') |
| 30 | + ->assertSuccessful(); |
| 31 | +}); |
| 32 | + |
| 33 | +it('generates model with correct class name', function () { |
| 34 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 35 | + ->assertSuccessful(); |
| 36 | + |
| 37 | + $this->assertFileContains([ |
| 38 | + 'class CustomMetric extends Model', |
| 39 | + ], 'app/CustomMetric.php'); |
| 40 | +}); |
| 41 | + |
| 42 | +it('generates model with correct namespace', function () { |
| 43 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 44 | + ->assertSuccessful(); |
| 45 | + |
| 46 | + $this->assertFileContains([ |
| 47 | + 'namespace App;', |
| 48 | + ], 'app/CustomMetric.php'); |
| 49 | +}); |
| 50 | + |
| 51 | +it('generates model with all required imports', function () { |
| 52 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 53 | + ->assertSuccessful(); |
| 54 | + |
| 55 | + $this->assertFileContains([ |
| 56 | + 'use DirectoryTree\Metrics\MetricBuilder;', |
| 57 | + 'use DirectoryTree\Metrics\MetricFactory;', |
| 58 | + 'use Illuminate\Database\Eloquent\Factories\HasFactory;', |
| 59 | + 'use Illuminate\Database\Eloquent\Model;', |
| 60 | + ], 'app/CustomMetric.php'); |
| 61 | +}); |
| 62 | + |
| 63 | +it('generates model with HasFactory trait', function () { |
| 64 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 65 | + ->assertSuccessful(); |
| 66 | + |
| 67 | + $this->assertFileContains([ |
| 68 | + 'use HasFactory;', |
| 69 | + ], 'app/CustomMetric.php'); |
| 70 | +}); |
| 71 | + |
| 72 | +it('generates model with guarded property', function () { |
| 73 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 74 | + ->assertSuccessful(); |
| 75 | + |
| 76 | + $this->assertFileContains([ |
| 77 | + 'protected $guarded = [];', |
| 78 | + ], 'app/CustomMetric.php'); |
| 79 | +}); |
| 80 | + |
| 81 | +it('generates model with factory property', function () { |
| 82 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 83 | + ->assertSuccessful(); |
| 84 | + |
| 85 | + $this->assertFileContains([ |
| 86 | + 'protected static string $factory = MetricFactory::class;', |
| 87 | + ], 'app/CustomMetric.php'); |
| 88 | +}); |
| 89 | + |
| 90 | +it('generates model with casts method', function () { |
| 91 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 92 | + ->assertSuccessful(); |
| 93 | + |
| 94 | + $this->assertFileContains([ |
| 95 | + 'protected function casts(): array', |
| 96 | + '\'year\' => \'integer\',', |
| 97 | + '\'month\' => \'integer\',', |
| 98 | + '\'day\' => \'integer\',', |
| 99 | + '\'hour\' => \'integer\',', |
| 100 | + '\'value\' => \'integer\',', |
| 101 | + ], 'app/CustomMetric.php'); |
| 102 | +}); |
| 103 | + |
| 104 | +it('generates model with custom query builder method', function () { |
| 105 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 106 | + ->assertSuccessful(); |
| 107 | + |
| 108 | + $this->assertFileContains([ |
| 109 | + 'public function newEloquentBuilder($query): MetricBuilder', |
| 110 | + 'return new MetricBuilder($query);', |
| 111 | + ], 'app/CustomMetric.php'); |
| 112 | +}); |
| 113 | + |
| 114 | +it('generates valid php model file', function () { |
| 115 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 116 | + ->assertSuccessful(); |
| 117 | + |
| 118 | + $this->assertFileContains([ |
| 119 | + '<?php', |
| 120 | + ], 'app/CustomMetric.php'); |
| 121 | +}); |
| 122 | + |
| 123 | +it('supports namespaced model names', function () { |
| 124 | + artisan(MakeMetricsModel::class, ['name' => 'Models\CustomMetric']) |
| 125 | + ->assertSuccessful(); |
| 126 | + |
| 127 | + $this->assertFilenameExists('app/Models/CustomMetric.php'); |
| 128 | + $this->assertFileContains([ |
| 129 | + 'namespace App\Models;', |
| 130 | + 'class CustomMetric extends Model', |
| 131 | + ], 'app/Models/CustomMetric.php'); |
| 132 | +}); |
| 133 | + |
| 134 | +it('supports deeply namespaced model names', function () { |
| 135 | + artisan(MakeMetricsModel::class, ['name' => 'Metrics\CustomMetric']) |
| 136 | + ->assertSuccessful(); |
| 137 | + |
| 138 | + $this->assertFilenameExists('app/Metrics/CustomMetric.php'); |
| 139 | + $this->assertFileContains([ |
| 140 | + 'namespace App\Metrics;', |
| 141 | + 'class CustomMetric extends Model', |
| 142 | + ], 'app/Metrics/CustomMetric.php'); |
| 143 | +}); |
| 144 | + |
| 145 | +it('can generate multiple different metrics models', function () { |
| 146 | + artisan(MakeMetricsModel::class, ['name' => 'UserMetric']) |
| 147 | + ->assertSuccessful(); |
| 148 | + |
| 149 | + artisan(MakeMetricsModel::class, ['name' => 'CustomMetric']) |
| 150 | + ->assertSuccessful(); |
| 151 | + |
| 152 | + $this->assertFilenameExists('app/UserMetric.php'); |
| 153 | + $this->assertFilenameExists('app/CustomMetric.php'); |
| 154 | + |
| 155 | + $this->assertFileContains([ |
| 156 | + 'class UserMetric extends Model', |
| 157 | + ], 'app/UserMetric.php'); |
| 158 | + |
| 159 | + $this->assertFileContains([ |
| 160 | + 'class CustomMetric extends Model', |
| 161 | + ], 'app/CustomMetric.php'); |
| 162 | +}); |
0 commit comments