Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto authored and StyleCIBot committed Feb 1, 2021
1 parent baaad3e commit 07cc670
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 123 deletions.
40 changes: 20 additions & 20 deletions tests/Controllers/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

namespace Tests\Controllers;

use App\Controllers\Home;
use CodeIgniter\Test\ControllerTester;
use Tests\Support\AuthTestCase;

class ExampleTest extends AuthTestCase
{
use ControllerTester;
public function testIndexHome()
{
$result = $this->controller(Home::class)
->execute('index');
$this->assertTrue($result->isOK());
}
}
<?php

namespace Tests\Controllers;

use App\Controllers\Home;
use CodeIgniter\Test\ControllerTester;
use Tests\Support\AuthTestCase;

class ExampleTest extends AuthTestCase
{
use ControllerTester;

public function testIndexHome()
{
$result = $this->controller(Home::class)
->execute('index');

$this->assertTrue($result->isOK());
}
}
179 changes: 89 additions & 90 deletions tests/Support/AuthTestCase.php
Original file line number Diff line number Diff line change
@@ -1,112 +1,111 @@
<?php
<?php

namespace Tests\Support;

use CodeIgniter\Test\ControllerTester;
use CodeIgniter\Session\Handlers\ArrayHandler;
use CodeIgniter\Test\Mock\MockSession;
use CodeIgniter\Test\CIDatabaseTestCase;
use CodeIgniter\Test\ControllerTester;
use CodeIgniter\Test\Mock\MockSession;
use Myth\Auth\Entities\User;
use Myth\Auth\Models\UserModel;

class AuthTestCase extends CIDatabaseTestCase
{
use ControllerTester;

/**
* Should the database be refreshed before each test?
*
* @var boolean
*/
protected $refresh = true;

/**
* The name of a seed file used for all tests within this test case.
*
* @var string
*/
protected $seed = 'agungsugiarto\boilerplate\Database\Seeds\BoilerplateSeeder';

/**
* The namespace to help us find the migration classes.
*
* @var string
*/
protected $namespace = ['Myth\Auth', 'agungsugiarto\boilerplate'];

/**
* @var \Myth\Auth\Models\UserModel
*/
protected $users;

protected $faker;

/**
* @var SessionHandler
*/
protected $session;

public function setUp(): void
{
parent::setUp();

$this->users = new UserModel();
$this->mockSession();

$this->faker = \Faker\Factory::create();
}

/**
* Pre-loads the mock session driver into $this->session.
*
*/
protected function mockSession()
{
require_once SYSTEMPATH . 'Test/Mock/MockSession.php';
$config = config('App');
$this->session = new MockSession(new ArrayHandler($config, '0.0.0.0'), $config);
\Config\Services::injectMock('session', $this->session);
$_SESSION = [];
}

/**
* Creates a user on-the-fly.
*
* @param string $reason
*
* @return $this
*/
protected function createUser(array $info = [])
{
$defaults = [
'email' => '[email protected]',
'username' => 'Fred',
'password' => 'secret'
];
$info = array_merge($info, $defaults);
$user = new User($info);

$userId = $this->users->insert($user);
$user = $this->users->find($userId);

// Delete any cached permissions
use ControllerTester;

/**
* Should the database be refreshed before each test?
*
* @var bool
*/
protected $refresh = true;

/**
* The name of a seed file used for all tests within this test case.
*
* @var string
*/
protected $seed = 'agungsugiarto\boilerplate\Database\Seeds\BoilerplateSeeder';

/**
* The namespace to help us find the migration classes.
*
* @var string
*/
protected $namespace = ['Myth\Auth', 'agungsugiarto\boilerplate'];

/**
* @var \Myth\Auth\Models\UserModel
*/
protected $users;

protected $faker;

/**
* @var SessionHandler
*/
protected $session;

public function setUp(): void
{
parent::setUp();

$this->users = new UserModel();
$this->mockSession();

$this->faker = \Faker\Factory::create();
}

/**
* Pre-loads the mock session driver into $this->session.
*/
protected function mockSession()
{
require_once SYSTEMPATH.'Test/Mock/MockSession.php';
$config = config('App');
$this->session = new MockSession(new ArrayHandler($config, '0.0.0.0'), $config);
\Config\Services::injectMock('session', $this->session);
$_SESSION = [];
}

/**
* Creates a user on-the-fly.
*
* @param string $reason
*
* @return $this
*/
protected function createUser(array $info = [])
{
$defaults = [
'email' => '[email protected]',
'username' => 'Fred',
'password' => 'secret',
];
$info = array_merge($info, $defaults);
$user = new User($info);

$userId = $this->users->insert($user);
$user = $this->users->find($userId);

// Delete any cached permissions
cache()->delete("{$userId}_permissions");

return $user;
}
return $user;
}

/**
* Creates a group on the fly
* Creates a group on the fly.
*
* @param array $info
*
* @return mixed
*/
protected function createGroup(array $info = [])
protected function createGroup(array $info = [])
{
$defaults = [
'name' => $this->faker->word,
'description' => $this->faker->sentence
'name' => $this->faker->word,
'description' => $this->faker->sentence,
];
$info = array_merge($info, $defaults);

Expand All @@ -125,8 +124,8 @@ protected function createGroup(array $info = [])
protected function createPermission(array $info = [])
{
$defaults = [
'name' => $this->faker->word,
'description' => $this->faker->sentence
'name' => $this->faker->word,
'description' => $this->faker->sentence,
];
$info = array_merge($info, $defaults);

Expand Down
6 changes: 3 additions & 3 deletions tests/Support/SessionTestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

namespace Tests\Support;

Expand All @@ -18,15 +18,15 @@ public function setUp(): void

$this->mockSession();
}

/**
* Pre-loads the mock session driver into $this->session.
*
* @var string
*/
protected function mockSession()
{
require_once SYSTEMPATH . 'Test/Mock/MockSession.php';
require_once SYSTEMPATH.'Test/Mock/MockSession.php';
$config = config('App');
$this->session = new MockSession(new ArrayHandler($config, '0.0.0.0'), $config);
\Config\Services::injectMock('session', $this->session);
Expand Down
20 changes: 10 additions & 10 deletions tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public function testGetPermissionsThroughUser()
$permission2 = $this->createPermission(['name' => 'second']);

$this->hasInDatabase('auth_users_permissions', [
'user_id' => $user->id,
'permission_id' => $permission1->id
'user_id' => $user->id,
'permission_id' => $permission1->id,
]);

$this->hasInDatabase('auth_users_permissions', [
'user_id' => $user->id,
'permission_id' => $permission2->id
'user_id' => $user->id,
'permission_id' => $permission2->id,
]);

$expected = [
Expand All @@ -35,13 +35,13 @@ public function testGetPermissionsThroughGroup()
$permission = $this->createPermission(['name' => 'first']);

$this->hasInDatabase('auth_groups_permissions', [
'group_id' => $group->id,
'permission_id' => $permission->id
'group_id' => $group->id,
'permission_id' => $permission->id,
]);

$this->hasInDatabase('auth_groups_users', [
'user_id' => $user->id,
'group_id' => $group->id
'user_id' => $user->id,
'group_id' => $group->id,
]);

$expected = [
Expand All @@ -56,8 +56,8 @@ public function testCan()
$user = $this->createUser();
$permission = $this->createPermission();
$this->hasInDatabase('auth_users_permissions', [
'user_id' => $user->id,
'permission_id' => $permission->id
'user_id' => $user->id,
'permission_id' => $permission->id,
]);

$this->assertTrue($user->can($permission->name));
Expand Down

0 comments on commit 07cc670

Please sign in to comment.