diff --git a/.env.testing b/.env.testing new file mode 100644 index 0000000..8da6be8 --- /dev/null +++ b/.env.testing @@ -0,0 +1,51 @@ +APP_NAME=Laravel +APP_ENV=local +APP_KEY=base64:wO+axlK/1o9pzGQo9UdvqGI1h7sCI03CBHSxiXkNmYk= +APP_DEBUG=true +APP_URL=http://localhost + +LOG_CHANNEL=stack + +DB_CONNECTION=testing + +DB_HOST=postgres +DB_PORT=5432 +DB_DATABASE=homestead +DB_USERNAME=homestead +DB_PASSWORD=secret + +DB_HOST_MYSQL=127.0.0.1 +DB_PORT_MYSQL=33061 +DB_DATABASE_MYSQL=byte5_test +DB_USERNAME_MYSQL=homestead +DB_PASSWORD_MYSQL=secret + +BROADCAST_DRIVER=log +CACHE_DRIVER=file +QUEUE_CONNECTION=sync +SESSION_DRIVER=file +SESSION_LIFETIME=120 + +REDIS_HOST=127.0.0.1 +REDIS_PASSWORD=null +REDIS_PORT=6379 + +MAIL_DRIVER=smtp +MAIL_HOST=smtp.mailtrap.io +MAIL_PORT=2525 +MAIL_USERNAME=null +MAIL_PASSWORD=null +MAIL_ENCRYPTION=null + +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_DEFAULT_REGION=us-east-1 +AWS_BUCKET= + +PUSHER_APP_ID= +PUSHER_APP_KEY= +PUSHER_APP_SECRET= +PUSHER_APP_CLUSTER=mt1 + +MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" +MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml new file mode 100644 index 0000000..96618a1 --- /dev/null +++ b/.idea/php-test-framework.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/Project.php b/app/Project.php index e4cdfb2..23b8cca 100644 --- a/app/Project.php +++ b/app/Project.php @@ -27,7 +27,6 @@ class Project extends Model { - public $connection = 'mysql'; protected $fillable = ['name']; diff --git a/config/database.php b/config/database.php index 1e1637e..c2da0b6 100644 --- a/config/database.php +++ b/config/database.php @@ -43,6 +43,12 @@ 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), ], + 'testing' => [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ], + 'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), diff --git a/database/factories/ProjectFactory.php b/database/factories/ProjectFactory.php index 19765ee..ee52545 100644 --- a/database/factories/ProjectFactory.php +++ b/database/factories/ProjectFactory.php @@ -7,6 +7,6 @@ $factory->define(Project::class, function (Faker $faker) { return [ - // + 'name' => $faker->name ]; }); diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php index 547152f..8143cef 100644 --- a/tests/CreatesApplication.php +++ b/tests/CreatesApplication.php @@ -2,6 +2,9 @@ namespace Tests; +use Artisan; +use DB; +use Illuminate\Support\Facades\Hash; use Illuminate\Contracts\Console\Kernel; trait CreatesApplication @@ -14,9 +17,21 @@ trait CreatesApplication public function createApplication() { $app = require __DIR__.'/../bootstrap/app.php'; - + $app->loadEnvironmentFrom('.env.testing'); $app->make(Kernel::class)->bootstrap(); return $app; } + + public function setUp(): void + { + parent::setUp(); + Artisan::call('migrate'); + } + + public function tearDown(): void + { + Artisan::call('migrate:reset'); + parent::tearDown(); + } } diff --git a/tests/Feature/ProjectTest.php b/tests/Feature/ProjectTest.php new file mode 100644 index 0000000..5ef1cc8 --- /dev/null +++ b/tests/Feature/ProjectTest.php @@ -0,0 +1,35 @@ +mockProject = factory(Project::class)->make(); + $this->mockProject->save(); + $this->tempName = $this->mockProject->name; + } + + public function testProjectApiControllerTest() + { + $response = $this->get('/api/projects'); + $response->assertStatus(200); + $response->assertJsonStructure(['data' => [['id', 'user' => [], 'project_name']]]); + $response->assertJson(['data' => [['project_name' => $this->tempName]]]); + } + + public function testAddDataToProjectTest() + { + $allProjects = Project::all(); + $this->assertEquals($allProjects->count(), 1); + $this->assertEquals($allProjects->first()->name, $this->tempName); + } +}