Skip to content

Commit e1ca701

Browse files
committed
Two more command tests complete
1 parent 6bfc372 commit e1ca701

File tree

7 files changed

+152
-17
lines changed

7 files changed

+152
-17
lines changed

src/Console/Command/DeleteScope.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,41 @@
22

33
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Console\Command;
44

5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateName;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\InvalidName;
8+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ScopeHasApiKeys;
9+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
510
use Illuminate\Console\Command;
611

712
final class DeleteScope extends Command
813
{
14+
private ApiKeyService $apiKeyService;
15+
916
/**
1017
* The name and signature of the console command.
1118
*
1219
* @var string
1320
*/
14-
protected $signature = 'apikey:scope:delete {scopeName}';
21+
protected $signature = 'apikey:scope:delete {name}';
1522

1623
/**
1724
* The console command description.
1825
*
1926
* @var string
2027
*/
21-
protected $description = 'Delete a scope for ApiKeys';
28+
protected $description = 'Delete an ApiKey Scope (Delete a scope, not a relationship)';
2229

2330
/**
2431
* Create a new command instance.
2532
*
2633
* @return void
2734
*/
28-
public function __construct()
35+
public function __construct(ApiKeyService $apiKeyService)
2936
{
3037
parent::__construct();
38+
39+
$this->apiKeyService = $apiKeyService;
3140
}
3241

3342
/**
@@ -37,6 +46,32 @@ public function __construct()
3746
*/
3847
public function handle()
3948
{
40-
// $drip->send(User::find($this->argument('user')));
49+
$name = $this->argument('name');
50+
51+
$scopeRepository = $this->apiKeyService->getEntityManager()
52+
->getRepository(Scope::class);
53+
54+
$scope = $scopeRepository->findOneBy([
55+
'name' => $name,
56+
]);
57+
58+
if (! $scope) {
59+
$this->error('Cannot find scope with name: ' . $name);
60+
61+
return 1;
62+
}
63+
64+
try {
65+
$scopeRepository->delete($scope);
66+
$this->apiKeyService->getEntityManager()->flush();
67+
} catch (ScopeHasApiKeys $e) {
68+
$this->error($e->getMessage());
69+
70+
return 1;
71+
}
72+
73+
$this->info('Scope has been deleted');
74+
75+
return 0;
4176
}
4277
}

src/Console/Command/GenerateScope.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function handle()
5252
->getRepository(Scope::class);
5353

5454
try {
55-
$scopeRepository->generate($name);
55+
$scope = $scopeRepository->generate($name);
5656
} catch (DuplicateName $e) {
5757
$this->error($e->getMessage());
5858

@@ -63,16 +63,8 @@ public function handle()
6363
return 1;
6464
}
6565

66-
67-
68-
if (! $apiKey) {
69-
$this->error('Invalid apiKey name');
70-
71-
return 1;
72-
}
73-
74-
$headers = ['name', 'key', 'status'];
75-
$rows = [[$apiKey->getName(), $apiKey->getKey(), $apiKey->getIsActive() ? 'active': 'deactivated']];
66+
$headers = ['name', 'apiKey count'];
67+
$rows = [[$scope->getName(), sizeof($scope->getApiKeys())]];
7668

7769
$this->table($headers, $rows);
7870

src/Exception/ScopeHasApiKeys.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Exception;
4+
5+
use Exception;
6+
7+
class ScopeHasApiKeys extends Exception
8+
{
9+
10+
}

src/Repository/ScopeRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
66
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateName;
77
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\InvalidName;
8+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ScopeHasApiKeys;
89
use DateTime;
910
use Doctrine\ORM\EntityRepository;
1011
use Exception;
@@ -45,7 +46,7 @@ public function canDelete(Scope $scope): bool
4546
public function delete(Scope $scope): Scope|bool
4647
{
4748
if (! $this->canDelete($scope)) {
48-
return false;
49+
throw new ScopeHasApiKeys('Cannot delete scope because it has ApiKeys assigned to it.');
4950
}
5051

5152
$this->getEntityManager()->remove($scope);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace ApiSkeletonsTest\Laravel\Doctrine\ApiKey\Feature\Console\Command;
4+
5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ScopeHasApiKeys;
8+
use ApiSkeletonsTest\Laravel\Doctrine\ApiKey\TestCase;
9+
use DateTime;
10+
11+
final class DeleteScopeTest extends TestCase
12+
{
13+
public function testDeleteScope(): void
14+
{
15+
$entityManager = app('em');
16+
$this->createDatabase($entityManager);
17+
18+
$scopeRepository = $entityManager->getRepository(Scope::class);
19+
$scope = $scopeRepository->generate('testing');
20+
$entityManager->flush();
21+
22+
$this->artisan('apikey:scope:delete', [
23+
'name' => $scope->getName(),
24+
])->assertExitCode(0);
25+
}
26+
27+
public function testErrorOnInvalidName(): void
28+
{
29+
$entityManager = app('em');
30+
$this->createDatabase($entityManager);
31+
32+
$this->artisan('apikey:scope:delete', [
33+
'name' => 'invalid',
34+
])->assertExitCode(1);
35+
}
36+
37+
public function testCannotDeleteScopeWithApiKeys(): void
38+
{
39+
$entityManager = $this->createDatabase(app('em'));
40+
$apiKeyRepository = $entityManager->getRepository(ApiKey::class);
41+
$scopeRepository = $entityManager->getRepository(Scope::class);
42+
$apiKey = $apiKeyRepository->generate('testing');
43+
$scope = $scopeRepository->generate('testing');
44+
$entityManager->flush();
45+
46+
$apiKeyRepository->addScope($apiKey, $scope);
47+
$entityManager->flush();
48+
49+
$this->artisan('apikey:scope:delete', [
50+
'name' => $scope->getName(),
51+
])->assertExitCode(1);
52+
}
53+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace ApiSkeletonsTest\Laravel\Doctrine\ApiKey\Feature\Console\Command;
4+
5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
6+
use ApiSkeletonsTest\Laravel\Doctrine\ApiKey\TestCase;
7+
8+
final class GenerateScopeTest extends TestCase
9+
{
10+
public function testGenerateScope(): void
11+
{
12+
$this->createDatabase(app('em'));
13+
14+
$this->artisan('apikey:scope:generate', [
15+
'name' => 'testing',
16+
])->assertExitCode(0);
17+
}
18+
19+
public function testInvalidNameThrowsError(): void
20+
{
21+
$this->createDatabase(app('em'));
22+
23+
$this->artisan('apikey:scope:generate', [
24+
'name' => 'test^ing',
25+
])->assertExitCode(1);
26+
}
27+
28+
public function testDuplicateNameThrowsError(): void
29+
{
30+
$entityManager = app('em');
31+
32+
$this->createDatabase($entityManager);
33+
34+
$apiKeyRepository = $entityManager->getRepository(Scope::class);
35+
$apiKey = $apiKeyRepository->generate('testing');
36+
$entityManager->flush();
37+
38+
$this->artisan('apikey:scope:generate', [
39+
'name' => $apiKey->getName(),
40+
])->assertExitCode(1);
41+
}
42+
}

test/Feature/Repository/ScopeRepositoryTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
77
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateName;
88
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\InvalidName;
9+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ScopeHasApiKeys;
910
use ApiSkeletonsTest\Laravel\Doctrine\ApiKey\TestCase;
1011

1112
final class ScopeRepositoryTest extends TestCase
@@ -59,6 +60,8 @@ public function testCannotCreateDuplicateScope(): void
5960

6061
public function testCannotDeleteScopeWithApiKeys(): void
6162
{
63+
$this->expectException(ScopeHasApiKeys::class);
64+
6265
$entityManager = $this->createDatabase(app('em'));
6366
$apiKeyRepository = $entityManager->getRepository(ApiKey::class);
6467
$scopeRepository = $entityManager->getRepository(Scope::class);
@@ -70,6 +73,5 @@ public function testCannotDeleteScopeWithApiKeys(): void
7073
$entityManager->flush();
7174

7275
$result = $scopeRepository->delete($scope);
73-
$this->assertFalse($result);
7476
}
7577
}

0 commit comments

Comments
 (0)