Skip to content

Commit

Permalink
Merge pull request #3 from nicp0nim/tests
Browse files Browse the repository at this point in the history
make tests for app: register, login, logout, clients
  • Loading branch information
rafal-kucharski authored Aug 23, 2019
2 parents 3795668 + a4a1174 commit 350f21c
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 41 deletions.
46 changes: 46 additions & 0 deletions .env.travis
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
APP_NAME='RestApi'
APP_ENV=testing
APP_KEY=base64:CS3VLz1BGmBBZBTSCQLShZ5NaM+A1MZukOro1fT4hRU=
APP_DEBUG=true
APP_URL=http://localhost:8080

LOG_CHANNEL=stack

DB_CONNECTION=testing
DB_TEST_DATABASE=testing
DB_TEST_USERNAME=root
DB_TEST_PASSWORD=

QUEUE_DRIVER=sync
BROADCAST_DRIVER=log
CACHE_DRIVER=array
QUEUE_CONNECTION=sync
SESSION_DRIVER=array
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}"

TELESCOPE_ENABLED=false
TELESCOPE_USERS=1,2
14 changes: 12 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
language: php

services:
- mysql
before_script:
- composer install
- cp .env.example .env
- cp .env.travis .env
- mysql -e 'create database testing;'
- composer self-update
- composer install --no-interaction
- php artisan key:generate
- php artisan migrate --seed
- php artisan passport:install
script:
- vendor/bin/phpunit

4 changes: 2 additions & 2 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function login()
$token = $user->createToken('RestApi')->accessToken;
return $this->sendResponse($token);
} else {
return $this->sendError('Unauthorized.', [], 401);
return $this->sendError('Unauthorized.', [], 400);
}
}

Expand All @@ -34,7 +34,7 @@ public function register(Request $request)
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return $this->sendError('Validation error.', $validator->errors(), 401);
return $this->sendError('Validation error.', $validator->errors(), 400);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Authenticate extends Middleware
public function handle($request, Closure $next, ...$guards)
{
if ($this->authenticate($request, $guards) === 'authentication_failed') {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 400);
return response()->json(['success' => false, 'message' => 'Unauthorized'], 401);
}
return $next($request);
}
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"tests" : [
"vendor/bin/phpunit"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
Expand Down
27 changes: 13 additions & 14 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
Expand All @@ -63,6 +63,18 @@
]) : [],
],

'testing' => [
'driver' => 'mysql',
'host' => env('DB_TEST_HOST', 'localhost'),
'database' => env('DB_TEST_DATABASE', 'testing_db'),
'username' => env('DB_TEST_USERNAME', 'root'),
'password' => env('DB_TEST_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],

'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
Expand All @@ -78,19 +90,6 @@
'sslmode' => 'prefer',
],

'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],

],

/*
Expand Down
15 changes: 3 additions & 12 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
/** @var Factory $factory */

use App\User;
use Illuminate\Support\Str;
use Faker\Generator as Faker;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
use Illuminate\Database\Eloquent\Factory;

$factory->define(User::class, function (Faker $faker) {
return [
Expand Down
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
$this->call(UserSeeder::class);
$this->call(ClientSeeder::class);
}
}
17 changes: 17 additions & 0 deletions database/seeds/UserSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use App\User;
use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(User::class, 10)->create();
}
}
15 changes: 7 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>

<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
<whitelist>
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_DRIVER" value="sync"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>
3 changes: 2 additions & 1 deletion tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Tests;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;

trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
* @return Application
*/
public function createApplication()
{
Expand Down
142 changes: 142 additions & 0 deletions tests/Feature/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Tests\Feature;

use App\Client;
use App\User;
use Tests\TestCase;

class ClientTest extends TestCase
{
public function testsClientsAreCreatedCorrectly()
{
$user = factory(User::class)->create();
$token = $user->createToken('RestApi')->accessToken;
$headers = ['Authorization' => "Bearer $token"];
$payload = [
'name' => 'Szczepańska',
'vat_number' => '6529411372',
'street' => 'Sądowa 15A/28',
'city' => 'Bogatynia',
'post_code' => '22-090',
'email' => '[email protected]'
];

$this->json('POST', '/api/clients', $payload, $headers)
->assertStatus(200)
->assertJson([
'success' => true,
'data' => [
'id' => 11,
'name' => 'Szczepańska',
'vat_number' => '6529411372',
'street' => 'Sądowa 15A/28',
'city' => 'Bogatynia',
'post_code' => '22-090',
'email' => '[email protected]'
],
'message' => 'Client created successfully.'
]);
}

public function testsClientsAreUpdatedCorrectly()
{
$user = factory(User::class)->create();
$token = $user->createToken('RestApi')->accessToken;
$headers = ['Authorization' => "Bearer $token"];
$article = factory(Client::class)->create([
'name' => 'Szczepańska',
'vat_number' => '6529411372',
'street' => 'Sądowa 15A/28',
'city' => 'Bogatynia',
'post_code' => '22-090',
'email' => '[email protected]'
]);

$payload = [
'name' => 'Barańska',
'vat_number' => '1234567890',
'street' => 'Rolna 12',
'city' => 'Warszawa',
'post_code' => '01-000',
'email' => '[email protected]'
];

$response = $this->json('PUT', '/api/clients/'.$article->id, $payload, $headers)
->assertStatus(200)
->assertJson([
'success' => true,
'data' => [
'id' => 11,
'name' => 'Barańska',
'vat_number' => '1234567890',
'street' => 'Rolna 12',
'city' => 'Warszawa',
'post_code' => '01-000',
'email' => '[email protected]'
],
'message' => 'Client updated successfully.'
]);
}

public function testsClientsAreDeletedCorrectly()
{
$user = factory(User::class)->create();
$token = $user->createToken('RestApi')->accessToken;
$headers = ['Authorization' => "Bearer $token"];
$article = factory(Client::class)->create([
'name' => 'Szczepańska',
'vat_number' => '6529411372',
'street' => 'Sądowa 15A/28',
'city' => 'Bogatynia',
'post_code' => '22-090',
'email' => '[email protected]'
]);

$this->json('DELETE', '/api/clients/'.$article->id, [], $headers)
->assertStatus(200);
}

public function testClientsAreListedCorrectly()
{
factory(Client::class)->create([
'name' => 'Szczepańska',
'vat_number' => '6529411372',
'street' => 'Sądowa 15A/28',
'city' => 'Bogatynia',
'post_code' => '22-090',
'email' => '[email protected]'
]);

factory(Client::class)->create([
'name' => 'Barańska',
'vat_number' => '1234567890',
'street' => 'Rolna 12',
'city' => 'Warszawa',
'post_code' => '01-000',
'email' => '[email protected]'
]);

$user = factory(User::class)->create();
$token = $user->createToken('RestApi')->accessToken;
$headers = ['Authorization' => "Bearer $token"];

$response = $this->json('GET', '/api/clients', [], $headers)
->assertStatus(200)
->assertJsonStructure([
'success',
'data' => [
'*' => [
'id',
'name',
'vat_number',
'street',
'city',
'post_code',
'email'
]
],
'message'
]);
}
}
Loading

0 comments on commit 350f21c

Please sign in to comment.