Commit 81101e30 authored by Nicolas Widart's avatar Nicolas Widart

Adding a test for the UserRegistration class

parent 018570f4
<?php
namespace Modules\User\Tests;
use Illuminate\View\ViewServiceProvider;
use Modules\Core\Providers\CoreServiceProvider;
use Modules\User\Providers\UserServiceProvider;
use Nwidart\Modules\LaravelModulesServiceProvider;
use Orchestra\Testbench\TestCase;
abstract class BaseUserTestCase extends TestCase
{
public function setUp()
{
parent::setUp();
$this->resetDatabase();
}
protected function getPackageProviders($app)
{
return [
ViewServiceProvider::class,
LaravelModulesServiceProvider::class,
CoreServiceProvider::class,
UserServiceProvider::class,
];
}
protected function getEnvironmentSetUp($app)
{
$app['path.base'] = __DIR__ . '/..';
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', array(
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
));
$app['config']->set('translatable.locales', ['en', 'fr']);
$app['config']->set('modules.paths.modules', __DIR__ . '/../Modules');
}
private function resetDatabase()
{
// Makes sure the migrations table is created
$this->artisan('migrate', [
'--database' => 'sqlite',
]);
// We empty all tables
$this->artisan('migrate:reset', [
'--database' => 'sqlite',
]);
// Migrate
$this->artisan('migrate', [
'--database' => 'sqlite',
]);
$this->artisan('migrate');
}
}
<?php
namespace Modules\User\Tests;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Modules\User\Repositories\RoleRepository;
use Modules\User\Repositories\UserRepository;
use Modules\User\Services\UserRegistration;
class UserRegistrationTest extends BaseUserTestCase
{
/**
* @var RoleRepository
*/
private $role;
/**
* @var UserRepository
*/
private $user;
public function setUp()
{
parent::setUp();
$this->role = app(RoleRepository::class);
$this->user = app(UserRepository::class);
}
/** @test */
public function it_registers_a_new_user_with_user_role()
{
$this->createRole('User');
app(UserRegistration::class)->register([
'email' => 'n.widart@gmail.com',
'password' => 'demo1234',
]);
$user = $this->user->find(1);
self::assertCount(1, $this->user->all());
self::assertEquals('n.widart@gmail.com', $user->email);
self::assertEquals('User', $user->roles->first()->name);
}
private function createRole($name)
{
return $this->role->create([
'name' => $name,
'slug' => str_slug($name),
]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment