Skip to content

Commit 4b6bca1

Browse files
author
Mateus Junges
authored
Merge pull request mateusjunges#98 from mateusjunges/issue-78
Fixes tests
2 parents f16df7e + 5738609 commit 4b6bca1

File tree

6 files changed

+37
-61
lines changed

6 files changed

+37
-61
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ matrix:
1010
allow_failures:
1111
- php: 7.4snapshot
1212

13-
services:
14-
- postgresql
15-
1613
before_script:
17-
- psql -c 'create database laravel_acl_tests;' -U postgres
1814
- travis_retry composer self-update
1915
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
2016

2117
script:
22-
- vendor/bin/phpunit tests --coverage-text --coverage-clover=coverage.clover
18+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

phpunit.xml

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,19 @@
99
processIsolation="false"
1010
stopOnFailure="false">
1111
<testsuites>
12-
<testsuite name="Unit">
13-
<directory suffix="Test.php">./tests/Unit</directory>
14-
</testsuite>
15-
16-
<testsuite name="Feature">
17-
<directory suffix="Test.php">./tests/Feature</directory>
12+
<testsuite name="Laravel ACL Tests">
13+
<directory suffix="Test.php">tests</directory>
1814
</testsuite>
1915
</testsuites>
2016
<filter>
21-
<whitelist processUncoveredFilesFromWhitelist="true">
22-
<directory suffix=".php">./app</directory>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
2319
</whitelist>
2420
</filter>
2521
<php>
26-
<server name="APP_ENV" value="testing"/>
27-
<server name="BCRYPT_ROUNDS" value="4"/>
28-
<server name="CACHE_DRIVER" value="array"/>
29-
<server name="MAIL_DRIVER" value="array"/>
30-
<server name="QUEUE_CONNECTION" value="sync"/>
31-
<server name="SESSION_DRIVER" value="array"/>
22+
<env name="APP_ENV" value="testing"/>
23+
<env name="DB_CONNECTION" value="sqlite"/>
24+
<env name="DB_DATABASE" value=":memory:"/>
3225
</php>
3326
</phpunit>
3427

src/Console/Commands/CreateGroup.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,21 @@ public function handle()
4040
{
4141
try {
4242
$groupModel = app(config('acl.models.group'));
43-
if ($this->confirm("Do you want to create a group with the name '"
44-
.$this->argument('name')."' and slug '"
45-
.$this->argument('slug')."'?")) {
43+
try {
4644
$group = $groupModel->where('slug', $this->argument('slug'))
4745
->orWhere('name', $this->argument('name'))
4846
->first();
4947
if (! is_null($group)) {
5048
throw GroupAlreadyExistsException::create();
5149
}
5250
$groupModel->create([
53-
'name' => $this->argument('name'),
54-
'slug' => $this->argument('slug'),
55-
'description' => $this->argument('description'),
51+
'name' => $this->argument('name'),
52+
'slug' => $this->argument('slug'),
53+
'description' => $this->argument('description'),
5654
]);
5755
$this->info('Group created successfully!');
58-
} else {
59-
$this->info('Group was not created.');
56+
} catch (\Exception $exception) {
57+
$this->error('Group was not created!');
6058
}
6159
} catch (\Exception $exception) {
6260
$this->error($exception->getMessage());

src/Console/Commands/CreatePermission.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,21 @@ public function handle()
4141
try {
4242
$permissionModel = app(config('acl.models.permission'));
4343

44-
if ($this->confirm("Do you want to create a permission named '"
45-
.$this->argument('name')."' and with slug '"
46-
.$this->argument('slug')."'?")) {
44+
try {
4745
$permission = $permissionModel->where('slug', $this->argument('slug'))
4846
->orWhere('name', $this->argument('name'))
4947
->first();
5048
if (! is_null($permission)) {
5149
throw PermissionAlreadyExistsException::create();
5250
}
5351
$permissionModel->create([
54-
'name' => $this->argument('name'),
55-
'slug' => $this->argument('slug'),
56-
'description' => $this->argument('description'),
52+
'name' => $this->argument('name'),
53+
'slug' => $this->argument('slug'),
54+
'description' => $this->argument('description'),
5755
]);
5856
$this->info('Permission created successfully!');
59-
} else {
60-
$this->info('Permission was not created.');
57+
} catch (\Exception $exception) {
58+
$this->error('Permission was not created!');
6159
}
6260
} catch (\Exception $exception) {
6361
$this->error($exception->getMessage());

tests/CommandsTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Junges\Tests;
44

5+
use Junges\ACL\Test\Group;
56
use Junges\ACL\Test\TestCase;
7+
use Junges\ACL\Test\Permission;
8+
use Illuminate\Support\Facades\Artisan;
69

710
class CommandsTest extends TestCase
811
{
@@ -19,22 +22,27 @@ public function setUp()
1922
*/
2023
public function it_can_create_a_permission()
2124
{
22-
$this->artisan('permission:create', [
25+
$permission = Artisan::call('permission:create', [
2326
'name' => 'Command test permission',
2427
'slug' => 'command-test-permission',
2528
'description' => 'Command test',
26-
])->assertExitCode(0);
29+
]);
30+
31+
$this->assertCount(1, Permission::where('slug', 'command-test-permission')->get());
32+
$this->assertEquals(0, $permission);
2733
}
2834

2935
/**
3036
* @test
3137
*/
3238
public function it_can_create_a_group()
3339
{
34-
$this->artisan('group:create', [
40+
$group = Artisan::call('group:create', [
3541
'name' => 'Command test group',
3642
'slug' => 'command-test-group',
3743
'description' => 'Test command group',
38-
])->assertExitCode(0);
44+
]);
45+
$this->assertEquals(0, $group);
46+
$this->assertCount(1, Group::where('slug', 'command-test-group')->get());
3947
}
4048
}

tests/TestCase.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Junges\ACL\Test;
44

5-
use Illuminate\Support\Facades\DB;
65
use Junges\ACL\ACLServiceProvider;
76
use Junges\ACL\ACLAuthServiceProvider;
87
use Illuminate\Database\Schema\Blueprint;
@@ -104,15 +103,11 @@ public function getPackageProviders($app)
104103
*/
105104
public function getEnvironmentSetUp($app)
106105
{
107-
$app['config']->set('database.default', 'pgsql');
108-
$app['config']->set('database.connections.pgsql', [
109-
'driver' => 'pgsql',
110-
'username' => 'postgres',
111-
'port' => '5432',
112-
'host' => '127.0.0.1',
113-
'password' => env('DB_PASSWORD', ''),
114-
'database' => 'laravel_acl_tests',
115-
'prefix' => '',
106+
$app['config']->set('database.default', 'sqlite');
107+
$app['config']->set('database.connections.sqlite', [
108+
'driver' => 'sqlite',
109+
'database' => ':memory:',
110+
'prefix' => '',
116111
]);
117112
$app['config']->set('views.path', [__DIR__.'/resources/views']);
118113

@@ -126,13 +121,6 @@ public function getEnvironmentSetUp($app)
126121
*/
127122
public function configureDatabase($app)
128123
{
129-
DB::statement('DROP TABLE IF EXISTS test_permissions CASCADE;');
130-
DB::statement('DROP TABLE IF EXISTS test_users CASCADE;');
131-
DB::statement('DROP TABLE IF EXISTS test_groups CASCADE;');
132-
DB::statement('DROP TABLE IF EXISTS test_user_has_permissions CASCADE;');
133-
DB::statement('DROP TABLE IF EXISTS test_user_has_groups CASCADE;');
134-
DB::statement('DROP TABLE IF EXISTS test_group_has_permissions CASCADE;');
135-
136124
/*
137125
* Set up the tables for testing proposes
138126
*/
@@ -169,11 +157,6 @@ public function configureDatabase($app)
169157
/*
170158
* Create the tables on the database
171159
*/
172-
(new \CreatePermissionsTable())->down();
173-
(new \CreateGroupsTable())->down();
174-
(new \CreateGroupHasPermissionsTable())->down();
175-
(new \CreateUserHasPermissionsTable())->down();
176-
(new \CreateUserHasGroupsTable())->down();
177160
(new \CreatePermissionsTable())->up();
178161
(new \CreateGroupsTable())->up();
179162
(new \CreateGroupHasPermissionsTable())->up();

0 commit comments

Comments
 (0)