Skip to content

Commit 6bfc372

Browse files
committed
Half way through command tests
1 parent 3f3d697 commit 6bfc372

31 files changed

+970
-250
lines changed

LaravelDoctrineApiKey.skipper

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<field name="id" type="bigint" required="true" unique="true" primary="true" auto-increment="true" uuid="1425ce9b-57d6-4c28-bd8b-89fffce92468"/>
66
<field name="name" type="string" required="true" unique="true" uuid="a0ea0895-aab4-4c93-b119-47c9539e95a2"/>
77
<field name="key" type="string" required="true" unique="true" uuid="e5a11137-9889-470a-8d5d-7ce1273d13df"/>
8-
<field name="is_deleted" type="boolean" required="true" uuid="ba95511b-7e28-47b1-8cd1-eaa9a488dc96"/>
8+
<field name="is_active" type="boolean" required="true" uuid="ba95511b-7e28-47b1-8cd1-eaa9a488dc96"/>
99
<field name="created_at" type="datetime" required="true" uuid="d09a3c83-1bd8-45fe-8b2c-9c46b4319c30"/>
10-
<field name="deleted_at" type="datetime" uuid="49072f5c-bbbd-4e04-85df-656eaffd9025"/>
10+
<field name="status_at" type="datetime" required="true" uuid="49072f5c-bbbd-4e04-85df-656eaffd9025"/>
1111
<orm-attributes>
1212
<attribute name="table">api_keys</attribute>
1313
<attribute name="repository-class">ApiSkeletons\Laravel\Doctrine\ApiKey\Repository\ApiKeyRepository</attribute>

config/orm/ApiSkeletons.Laravel.Doctrine.ApiKey.Entity.ApiKey.dcm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</id>
77
<field name="name" type="string" unique="true" nullable="false"/>
88
<field name="key" type="string" unique="true" nullable="false"/>
9-
<field name="is_deleted" type="boolean" nullable="false"/>
9+
<field name="is_active" type="boolean" nullable="false"/>
1010
<field name="created_at" type="datetime" nullable="false"/>
11-
<field name="deleted_at" type="datetime" nullable="true"/>
11+
<field name="status_at" type="datetime" nullable="false"/>
1212
<one-to-many field="accessEvents" target-entity="ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\AccessEvent" mapped-by="apiKey"/>
1313
<one-to-many field="adminEvents" target-entity="ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\AdminEvent" mapped-by="apiKey"/>
1414
<many-to-many field="scopes" target-entity="ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope" mapped-by="apiKeys"/>

phpunit.xml.dist

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
failOnWarning="true"
1313
verbose="true">
1414
<testsuites>
15-
<testsuite name="default">
16-
<directory suffix="Test.php">test</directory>
15+
<testsuite name="Unit">
16+
<directory suffix="Test.php">./test/Unit</directory>
17+
</testsuite>
18+
<testsuite name="Feature">
19+
<directory suffix="Test.php">./test/Feature</directory>
1720
</testsuite>
1821
</testsuites>
1922

@@ -23,4 +26,10 @@
2326
<directory suffix=".php">src</directory>
2427
</include>
2528
</coverage>
29+
30+
<php>
31+
<env name="DB_CONNECTION" value="sqlite"/>
32+
<env name="DB_DATABASE" value=":memory:"/>
33+
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
34+
</php>
2635
</phpunit>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Console\Command;
4+
5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateName;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\InvalidName;
8+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
9+
use Illuminate\Console\Command;
10+
11+
final class ActivateApiKey extends Command
12+
{
13+
private ApiKeyService $apiKeyService;
14+
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'apikey:activate {name}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Activate an ApiKey';
28+
29+
/**
30+
* Create a new command instance.
31+
*
32+
* @return void
33+
*/
34+
public function __construct(ApiKeyService $apiKeyService)
35+
{
36+
parent::__construct();
37+
38+
$this->apiKeyService = $apiKeyService;
39+
}
40+
41+
/**
42+
* Execute the console command.
43+
*
44+
* @return mixed
45+
*/
46+
public function handle()
47+
{
48+
$name = $this->argument('name');
49+
50+
$apiKeyRepository = $this->apiKeyService->getEntityManager()
51+
->getRepository(ApiKey::class);
52+
53+
$apiKey = $apiKeyRepository->findOneBy([
54+
'name' => $name,
55+
]);
56+
57+
if (! $apiKey) {
58+
$this->error('Invalid apiKey name');
59+
60+
return 1;
61+
}
62+
63+
$apiKeyRepository->updateActive($apiKey, true);
64+
$this->apiKeyService->getEntityManager()->flush();
65+
66+
$headers = ['name', 'key', 'status'];
67+
$rows = [[$apiKey->getName(), $apiKey->getKey(), $apiKey->getIsActive() ? 'active': 'deactivated']];
68+
69+
$this->table($headers, $rows);
70+
71+
return 0;
72+
}
73+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Console\Command;
66

7-
final class AddScopeToApiKey extends Command
7+
final class AddScope extends Command
88
{
99
/**
1010
* The name and signature of the console command.

src/Console/Command/CreateApiKey.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Console/Command/CreateScope.php

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Console\Command;
4+
5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateName;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\InvalidName;
8+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
9+
use Illuminate\Console\Command;
10+
11+
final class DeactivateApiKey extends Command
12+
{
13+
private ApiKeyService $apiKeyService;
14+
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'apikey:deactivate {name}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Deactivate an ApiKey';
28+
29+
/**
30+
* Create a new command instance.
31+
*
32+
* @return void
33+
*/
34+
public function __construct(ApiKeyService $apiKeyService)
35+
{
36+
parent::__construct();
37+
38+
$this->apiKeyService = $apiKeyService;
39+
}
40+
41+
/**
42+
* Execute the console command.
43+
*
44+
* @return mixed
45+
*/
46+
public function handle()
47+
{
48+
$name = $this->argument('name');
49+
50+
$apiKeyRepository = $this->apiKeyService->getEntityManager()
51+
->getRepository(ApiKey::class);
52+
53+
$apiKey = $apiKeyRepository->findOneBy([
54+
'name' => $name,
55+
]);
56+
57+
if (! $apiKey) {
58+
$this->error('Invalid apiKey name');
59+
60+
return 1;
61+
}
62+
63+
$apiKeyRepository->updateActive($apiKey, false);
64+
$this->apiKeyService->getEntityManager()->flush();
65+
66+
$headers = ['name', 'key', 'status'];
67+
$rows = [[$apiKey->getName(), $apiKey->getKey(), $apiKey->getIsActive() ? 'active': 'deactivated']];
68+
69+
$this->table($headers, $rows);
70+
71+
return 0;
72+
}
73+
}

src/Console/Command/DeleteApiKey.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)