Task 3 - soft deletes

This commit is contained in:
PovilasKorop
2021-11-09 10:18:02 +02:00
parent 40044cbfa4
commit 7c86c01048
5 changed files with 86 additions and 0 deletions
+8
View File
@@ -44,3 +44,11 @@ Test method `test_column_added_to_the_table()`.
--- ---
## Task 3. Soft Deletes.
Folder `database/migrations/task3` contains a migration for projects table. You need to add a field there, for Soft Delete functionality.
Test method `test_soft_deletes()`.
---
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Project extends Model
{
use HasFactory, SoftDeletes;
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class ProjectFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->text(20),
];
}
}
@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
// TASK: Add soft deletes column here
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('projects');
}
}
+12
View File
@@ -2,6 +2,7 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Models\Project;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Tests\TestCase; use Tests\TestCase;
@@ -39,4 +40,15 @@ class MigrationsTest extends TestCase
$this->assertEquals(3, $fieldNumber); $this->assertEquals(3, $fieldNumber);
} }
public function test_soft_deletes()
{
// We just test if the test succeeds or throws an exception
$this->expectNotToPerformAssertions();
Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task3']);
$project = Project::factory()->create();
$project->delete();
}
} }