Skip to content

Commit

Permalink
Merge pull request #227 from buggregator/hotfix/default-project
Browse files Browse the repository at this point in the history
Adds default project on buggregator startup.
  • Loading branch information
butschster authored Jul 28, 2024
2 parents 751aea8 + 098b7d6 commit 15a57b7
Show file tree
Hide file tree
Showing 23 changed files with 253 additions and 184 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Database\Migrations;

use Cycle\Migrations\Migration;
use Cycle\ORM\EntityManagerInterface;
use Modules\Projects\Domain\Project;
use Modules\Projects\Domain\ProjectRepositoryInterface;
use Modules\Projects\Domain\ValueObject\Key;
use Spiral\Core\ContainerScope;

class OrmDefaultD93e77c9f5556975e93bfbc969442732 extends Migration
{
protected const DATABASE = 'default';

public function up(): void
{
$this->getEntityManager()
->persist(new Project(Key::create(Project::DEFAULT_KEY), 'Default project'))
->run();
}

public function down(): void
{
$defaultProject = $this->getRepository()->findOne(['key' => Project::DEFAULT_KEY]);
if ($defaultProject !== null) {
$this->getEntityManager()->delete($defaultProject)->run();
}
}

private function getEntityManager(): EntityManagerInterface
{
return ContainerScope::getContainer()->get(EntityManagerInterface::class);
}

private function getRepository(): ProjectRepositoryInterface
{
return ContainerScope::getContainer()->get(ProjectRepositoryInterface::class);
}
}
5 changes: 4 additions & 1 deletion app/modules/Events/Interfaces/Commands/StoreEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Modules\Events\Domain\EventRepositoryInterface;
use Modules\Events\Domain\Events\EventWasReceived;
use Modules\Events\Domain\ValueObject\Timestamp;
use Modules\Projects\Domain\Project;
use Modules\Projects\Domain\ValueObject\Key;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Cqrs\Attribute\CommandHandler;
use Spiral\Cqrs\QueryBusInterface;
Expand Down Expand Up @@ -40,7 +42,8 @@ public function handle(HandleReceivedEvent $command): void
type: $command->type,
payload: new Json($command->payload),
timestamp: Timestamp::create(),
project: $project?->getKey(),
// todo: use better option for default project
project: $project?->getKey() ?? Key::create(Project::DEFAULT_KEY),
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function validatePayload(mixed $payload): void
}
}

if (!empty($errors)) {
if ($errors !== []) {
throw new ValidationException($errors);
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/modules/Projects/Domain/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#[Entity(
repository: ProjectRepositoryInterface::class,
)]
class Project
class Project implements ProjectInterface
{
public const DEFAULT_KEY = 'default';

/** @internal */
public function __construct(
#[Column(type: 'string(36)', primary: true, typecast: Key::class)]
Expand Down
13 changes: 13 additions & 0 deletions app/modules/Projects/Domain/ProjectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Modules\Projects\Domain;

use Modules\Projects\Domain\ValueObject\Key;

interface ProjectInterface
{
public function getKey(): Key;
public function getName(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
namespace Modules\Projects\Interfaces\Http\Resources;

use App\Application\HTTP\Response\JsonResource;
use Modules\Projects\Domain\Project;
use Modules\Projects\Domain\ProjectInterface;
use OpenApi\Attributes as OA;

/**
* @property-read Project $data
* @property-read ProjectInterface $data
*/
#[OA\Schema(
schema: 'Project',
properties: [
new OA\Property(property: 'uuid', type: 'string', format: 'uuid'),
new OA\Property(property: 'key', type: 'string'),
new OA\Property(property: 'name', type: 'string'),
],
)]
final class ProjectResource extends JsonResource
{
public function __construct(Project $data)
public function __construct(ProjectInterface $data)
{
parent::__construct($data);
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/Interfaces/Centrifugo/ConnectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function handle(RequestInterface $request): void
$projects = $this->bus->ask(new FindAllProjects());

/** @var non-empty-string[] $channels */
$channels = [(string) new EventsChannel()];

$channels = [];
foreach ($projects as $project) {
$channels[] = (string) new EventsChannel($project->getKey());
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/Interfaces/Http/Controller/SettingsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Application\HTTP\Response\JsonResource;
use App\Application\HTTP\Response\ResourceInterface;
use App\Application\Ide\UrlTemplate;
use Modules\Projects\Domain\Project;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Router\Annotation\Route;

Expand All @@ -30,6 +31,10 @@ public function __invoke(
'url_template' => $ideUrl->template,
],
'version' => $appVersion->version,
'project' => [
// todo: use better option for default project
'default' => Project::DEFAULT_KEY,
],
]);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"rr get-binary"
],
"psalm": "vendor/bin/psalm --config=psalm.xml ./app",
"rector": "vendor/bin/rector process --dry-run",
"cs-check": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --dry-run",
"cs-fix": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -vvv --using-cache=no",
"deptrack": [
Expand Down
20 changes: 10 additions & 10 deletions tests/Feature/Interfaces/Http/HttpDumps/HttpDumpsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public function testHttpDumpViaHttpUser(): void
)
->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
$this->broadcastig->assertPushed('events.project.default', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('http-dump', $data['data']['type']);
$this->assertSame(null, $data['data']['project']);
$this->assertSame('default', $data['data']['project']);
$this->assertSame('{"foo":"bar"}', $data['data']['payload']['request']['body']);

return true;
Expand Down Expand Up @@ -51,24 +51,24 @@ public function testHttpDumpViaHttpUserWithProject(): void

public function testHttpDumpWithProjectFromHeader(): void
{
$this->createProject('default');
$this->createProject('foo');

$this->http
->postJson(
uri: '/',
data: ['foo' => 'bar'],
headers: [
'X-Buggregator-Event' => 'http-dump',
'X-Buggregator-Project' => 'default',
'X-Buggregator-Project' => 'foo',
],
cookies: ['foo' => 'bar'],
)
->assertOk();

$this->broadcastig->assertPushed((string) new EventsChannel('default'), function (array $data) {
$this->broadcastig->assertPushed((string) new EventsChannel('foo'), function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('http-dump', $data['data']['type']);
$this->assertSame('default', $data['data']['project']);
$this->assertSame('foo', $data['data']['project']);

return true;
});
Expand All @@ -85,10 +85,10 @@ public function testHttpDumpsPost(): void
)
->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
$this->broadcastig->assertPushed('events.project.default', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('http-dump', $data['data']['type']);
$this->assertSame(null, $data['data']['project']);
$this->assertSame('default', $data['data']['project']);
$this->assertSame('POST', $data['data']['payload']['request']['method']);
$this->assertSame('', $data['data']['payload']['request']['uri']);
$this->assertSame(['http-dump'], $data['data']['payload']['request']['headers']['X-Buggregator-Event']);
Expand Down Expand Up @@ -117,7 +117,7 @@ public function testHttpDumpsGet(): void
)
->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
$this->broadcastig->assertPushed('events.project.default', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('http-dump', $data['data']['type']);
$this->assertSame('GET', $data['data']['payload']['request']['method']);
Expand Down Expand Up @@ -148,7 +148,7 @@ public function testHttpDumpsDelete(): void
)
->assertOk();

$this->broadcastig->assertPushed('events', function (array $data) {
$this->broadcastig->assertPushed('events.project.default', function (array $data) {
$this->assertSame('event.received', $data['event']);
$this->assertSame('http-dump', $data['data']['type']);
$this->assertSame('DELETE', $data['data']['payload']['request']['method']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,20 @@ public function testSendDataWithClientHeaders(): void

public function testSendDataWithProject(): void
{
$this->createProject('default');
$project = 'foo';
$this->createProject($project);

$this->http
->post(
uri: 'http://inspector:default@localhost/',
uri: 'http://inspector:'.$project.'@localhost/',
data: Stream::create(self::PAYLOAD),
headers: [
'X-Inspector-Key' => 'test',
'X-Inspector-Version' => '1.0.0',
],
)->assertOk();

$this->assertEvent('default');
$this->assertEvent($project);
}

#[Env('INSPECTOR_SECRET_KEY', 'test')]
Expand Down Expand Up @@ -102,7 +103,7 @@ public function testSendDataWithWrongSecretKey(): void
)->assertForbidden();
}

public function assertEvent(?string $project = null): void
public function assertEvent(string $project = 'default'): void
{
$this->broadcastig->assertPushed((string) new EventsChannel($project), function (array $data) use($project) {
$this->assertSame('event.received', $data['event']);
Expand Down
9 changes: 5 additions & 4 deletions tests/Feature/Interfaces/Http/Profiler/ProfilerActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ public function testSendData(): void
],
)->assertOk();

$this->assertEvent();
$this->assertEvent('default');
}

public function testSendDataWithProject(): void
{
$this->createProject('default');
$project = 'foo';
$this->createProject($project);

$this->http
->post(
uri: '/api/profiler/store',
data: Stream::create(self::PAYLOAD),
headers: [
'X-Buggregator-Event' => 'profiler',
'X-Buggregator-Project' => 'default',
'X-Buggregator-Project' => $project,
],
)->assertOk();

$this->assertEvent('default');
$this->assertEvent($project);
}

public function testSendInvalidPayload(): void
Expand Down
Loading

0 comments on commit 15a57b7

Please sign in to comment.