Skip to content

Instantly share code, notes, and snippets.

@manuelgeek
Last active March 1, 2018 19:21
Show Gist options
  • Save manuelgeek/b8dbeea9dcc8be67c50a4aaa7c9d4587 to your computer and use it in GitHub Desktop.
Save manuelgeek/b8dbeea9dcc8be67c50a4aaa7c9d4587 to your computer and use it in GitHub Desktop.
git clone https://github.com/manuelgeek/gtt_api
create .env file in tje root folder of the project, copy contents in .env.example and paste in .env
create a db, name it gtt_test, or any name you want
then chage the db credential in the .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gtt_test
DB_USERNAME=root
DB_PASSWORD=
meanwhile run
composer update
in the terminal, if you hadnt, this is to download the laravel vendor files which were ignored in git commit,
in you wish not to do this, you can always remove the /vendor in .gitignore file
php artisan key:generate
then run
php artisan migrate
[21:03, 3/1/2018] +254 724 540039: I prefer we use chrome for better experince,
[21:04, 3/1/2018] +254 724 540039: so, laravel uses PHPUnit fot its tests,
test files reside in the /tests folder
[21:05, 3/1/2018] +254 724 540039: there is feature test and unit test, in the laravel documentation there are fully expalined
were wre going to do 2 browser tests the two unit tests
[21:05, 3/1/2018] +254 724 540039: BROWSER TEST is an example of feature test
[21:07, 3/1/2018] +254 724 540039: run
php artisan make:test RegisterTest
[21:08, 3/1/2018] +254 724 540039: one thing we have to note is that, your naming must be in NameTest.php
[21:08, 3/1/2018] +254 724 540039: and functions within the tests must start with testTest()
[21:19, 3/1/2018] +254 724 540039: lets run this command
composer require --dev laravel/dusk
[21:21, 3/1/2018] +254 724 540039: i almost forgot this, in laravel 5.6, browser tests uses Dusk, so we have to install it
i've been using laravel 5.2 for long in some old project tests hadi i almost forgot, sorry
[21:23, 3/1/2018] +254 724 540039: if you are getting an error, Dusk just got updated
so use
[21:23, 3/1/2018] +254 724 540039: composer require --dev laravel/dusk:^2.0
[21:24, 3/1/2018] +254 724 540039: for laravel 5.5 which is currently what we have
then run
php artisan dusk:install
[21:35, 3/1/2018] +254 724 540039: so make sre in .env your APP_URL is like this
APP_URL=http://localhost:8000
[21:35, 3/1/2018] +254 724 540039: Replace the code i n your /tests/Browser/RegisterTest.php with this code
//start
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class RegisterTest extends DuskTestCase
{
/**
* A Dusk test example.
*
* @return void
*/
public function testExample()
{
$this->browse(function ($browser) {
$browser->visit('/register')
->type('name','Geek Manu')
->type('email','[email protected]')
->type('password','admin1234')
->type('password_confirmation','admin1234')
->press('Register')
->assertPathIs('/home')
->assertSee('You are logged in!');
});
}
}
//end
[21:37, 3/1/2018] +254 724 540039: the run
php artisan dusk
[21:38, 3/1/2018] +254 724 540039: use another tab, make sure php artisan serve is running
[21:42, 3/1/2018] +254 724 540039: ok ,so let us create one test for our api, the register api
[21:42, 3/1/2018] +254 724 540039: yea
[21:44, 3/1/2018] +254 724 540039: so for the unit tests, we will create a RegisterTest in the /tests/Unit/RegisterTest.php
[21:44, 3/1/2018] +254 724 540039: run
[21:44, 3/1/2018] +254 724 540039: php artisan make:test RegisterTest --unit
//start
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class RegisterTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testTegisterAPI()
{
//empty request
$response = $this->json('POST', '/api/register', $payload= []);
$response
->assertStatus(422)
->assertJson([
"success"=> false,
"message"=> [
"name"=> [
"The name field is required."
],
"email"=> [
"The email field is required."
],
"password"=> [
"The password field is required."
]
]
]);
//with payload
$payload = [
"name" => "Geek Manu",
"email" => "mail".rand(100,9999)."@mail.com",//in order to be unique
"password" => "1234"
];
$response = $this->json('POST', '/api/register', $payload);
$response
->assertStatus(200)
->assertJsonStructure([
"success",
"data" => [
"id",
"name",
"email",
"created_at",
"updated_at",
"api_token"
]//we use assertJsonStructure cause json itself will be different at every request
]);
}
}
//end
with browser test you run
php artisan duk
for unit test
vendor/bin/phpunit
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class RegisterTest extends DuskTestCase
{
/**
* A Dusk test example.
*
* @return void
*/
public function testExample()
{
$this->browse(function ($browser) {
$browser->visit('/register')
->type('name','Geek Manu')
->type('email','[email protected]')
->type('password','admin1234')
->type('password_confirmation','admin1234')
->press('Register')
->assertPathIs('/home')
->assertSee('You are logged in!');
});
}
}
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class RegisterTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testTegisterAPI()
{
//empty request
$response = $this->json('POST', '/api/register', $payload= []);
$response
->assertStatus(422)
->assertJson([
"success"=> false,
"message"=> [
"name"=> [
"The name field is required."
],
"email"=> [
"The email field is required."
],
"password"=> [
"The password field is required."
]
]
]);
//with payload
$payload = [
"name" => "Geek Manu",
"email" => "mail".rand(100,9999)."@mail.com",//in order to be unique
"password" => "1234"
];
$response = $this->json('POST', '/api/register', $payload);
$response
->assertStatus(200)
->assertJsonStructure([
"success",
"data" => [
"id",
"name",
"email",
"created_at",
"updated_at",
"api_token"
]//we use assertJsonStructure cause json itself will be different at every request
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment