Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds projects support #150

Merged
merged 21 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes issues with deletion event with ignored uuids
  • Loading branch information
butschster committed Apr 29, 2024
commit 269b095df42f2a79af8e50976b6be4b5221d1f1b
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
build:
docker compose up --no-start;

start:
docker compose up --remove-orphans -d;

up: build start

stop:
docker compose stop;

down:
docker compose down;

restart:
docker compose restart;

list:
docker compose ps;

log-tail:
docker compose logs --tail=50 -f;

pull-latest:
docker compose pull;

# =========================

bash:
docker compose exec buggregator-server /bin/sh;

reset-server:
docker compose exec buggregator-server ./rr reset;

reset: reset-server
6 changes: 3 additions & 3 deletions app/database/Factory/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public function entity(): string

public function definition(): array
{
$type = $this->faker->randomElement(['sentry', 'monolog', 'var-dump', 'inspector', 'ray', 'profiler']);

return [
'uuid' => Uuid::generate(),
'type' => $this->faker->randomElement(['sentry', 'monolog', 'var-dump', 'inspector', 'ray', 'profiler']),
Expand All @@ -56,7 +54,9 @@ public function makeEntity(array $definition): object
type: $definition['type'],
payload: new Json($this->getPayload($definition['type'])),
timestamp: $definition['timestamp'],
project: $definition['project'] ? Key::create($definition['project']) : null,
project: $definition['project']
? ($definition['project'] instanceof Key ? $definition['project'] : Key::create($definition['project']))
: null,
);
}

Expand Down
2 changes: 1 addition & 1 deletion app/modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
#[Column(type: 'string(25)', typecast: Timestamp::class)]
private Timestamp $timestamp,

#[Column(type: 'string', nullable: true)]
#[Column(type: 'string', nullable: true, typecast: Key::class)]
private ?Key $project = null,
) {
}
Expand Down
8 changes: 4 additions & 4 deletions app/modules/Events/Interfaces/Commands/ClearEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public function __construct(
#[CommandHandler]
public function __invoke(ClearEvents $command): void
{
$args = ['project' => $command->project];
$scope = ['project' => $command->project];

if ($command->type) {
$args['type'] = $command->type;
$scope['type'] = $command->type;
}

if ($command->uuids) {
$args['uuid'] = $command->uuids;
$scope['uuid'] = $command->uuids;
}

$this->events->deleteAll($args);
$this->events->deleteAll($scope);
$this->dispatcher->dispatch(
new EventsWasClear(
type: $command->type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function store(Event $event): bool

public function deleteAll(array $scope = []): void
{
$events = $this->select()->where($scope)->fetchAll();
$events = $this->select()
->where($this->buildScope($scope))
->fetchAll();

foreach ($events as $event) {
$this->em->delete($event);
Expand All @@ -60,11 +62,29 @@ public function deleteByPK(string $uuid): bool

public function countAll(array $scope = []): int
{
return $this->select()->where($scope)->count();
return $this->select()
->where($this->buildScope($scope))
->count();
}

public function findAll(array $scope = [], array $orderBy = [], int $limit = 30, int $offset = 0): iterable
{
return $this->select()->where($scope)->orderBy($orderBy)->limit($limit)->offset($offset)->fetchAll();
return $this->select()
->where($this->buildScope($scope))
->orderBy($orderBy)
->limit($limit)
->offset($offset)
->fetchAll();
}

private function buildScope(array $scope): array
{
$newScope = [];

foreach ($scope as $key => $value) {
$newScope[$key] = \is_array($value) ? ['in' => $value] : $value;
}

return $newScope;
}
}
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php"
backupGlobals="false" colors="true" stderr="true" cacheDirectory=".phpunit.cache"
backupGlobals="false" colors="true" stderr="false" cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<php>
<env name="BROADCAST_CONNECTION" value="in-memory"/>
Expand All @@ -15,6 +15,7 @@
<env name="APP_ENV" value="testing"/>
<env name="PERSISTENCE_DRIVER" value="db"/>
<env name="DB_DRIVER" value="sqlite"/>
<env name="MONOLOG_DEFAULT_CHANNEL" value="stdout"/>
</php>
<source>
<include>
Expand Down
25 changes: 24 additions & 1 deletion tests/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,33 @@ protected function createProject(string $key = 'default'): Project
])->createOne();
}

protected function createEvent(string $type = 'fake'): Event
protected function createEvent(string $type = 'fake', ?string $project = null): Event
{
return EventFactory::new([
'type' => $type,
'project' => $project ? Key::create($project) : null,
])->createOne();
}

protected function assertEventExists(Event...$events): self
{
foreach ($events as $event) {
$this->assertEntity($event)->where([
'uuid' => $event->getUuid(),
])->assertExists();
}

return $this;
}

protected function assertEventMissing(Event ...$events): self
{
foreach ($events as $event) {
$this->assertEntity($event)->where([
'uuid' => $event->getUuid(),
])->assertMissing();
}

return $this;
}
}
63 changes: 54 additions & 9 deletions tests/Feature/Interfaces/Http/Events/ClearActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,91 @@ final class ClearActionTest extends ControllerTestCase
{
public function testClearAllEvents(): void
{
$this->fakeEvents()->eventShouldBeClear();
$event1 = $this->createEvent(type: 'foo');
$event2 = $this->createEvent(type: 'foo', project: 'default');
$event3 = $this->createEvent(type: 'bar');
$event4 = $this->createEvent(type: 'baz', project: 'default');

$this->http
->clearEvents()
->assertSuccessResource();

$this
->assertEventExists($event2, $event4)
->assertEventMissing($event1, $event3);
}

public function testClearEventsByType(): void
{
$this->fakeEvents()->eventShouldBeClear(type: 'test');
$event1 = $this->createEvent(type: 'foo');
$event2 = $this->createEvent(type: 'foo');
$event3 = $this->createEvent(type: 'bar');

$this->http
->clearEvents(type: 'test')
->clearEvents(type: 'bar')
->assertSuccessResource();

$this
->assertEventExists($event1, $event2)
->assertEventMissing($event3);
}

public function testClearEventsByProject(): void
{
$this->fakeEvents()->eventShouldBeClear(project: 'test');
$event1 = $this->createEvent(type: 'foo');
$event2 = $this->createEvent(type: 'foo', project: 'default');
$event3 = $this->createEvent(type: 'bar');
$event4 = $this->createEvent(type: 'baz', project: 'default');

$this->http
->clearEvents(project: 'test')
->clearEvents(project: 'default')
->assertSuccessResource();

$this
->assertEventMissing($event2, $event4)
->assertEventExists($event1, $event3);
}

public function testClearEventsByUuids(): void
{
$this->fakeEvents()->eventShouldBeClear(uuids: ['foo', 'bar']);
$event1 = $this->createEvent(type: 'foo');
$event2 = $this->createEvent(type: 'foo');
$event3 = $this->createEvent(type: 'foo');
$event4 = $this->createEvent(type: 'foo');
$event5 = $this->createEvent(type: 'bar');

$this->http
->clearEvents(uuids: ['foo', 'bar'])
->clearEvents(uuids: [
(string)$event1->getUuid(),
(string)$event2->getUuid(),
(string)$event3->getUuid(),
(string)$event4->getUuid(),
])
->assertSuccessResource();

$this
->assertEventMissing($event1, $event2, $event3, $event4)
->assertEventExists($event5);
}

public function testClearEventsByTypeAndUuids(): void
{
$this->fakeEvents()->eventShouldBeClear(type: 'test', uuids: ['foo', 'bar']);
$event1 = $this->createEvent(type: 'foo');
$event2 = $this->createEvent(type: 'foo');
$event3 = $this->createEvent(type: 'foo');
$event4 = $this->createEvent(type: 'foo');
$event5 = $this->createEvent(type: 'bar');

$this->http
->clearEvents(type: 'test', uuids: ['foo', 'bar'])
->clearEvents(type: 'foo', uuids: [
(string)$event1->getUuid(),
(string)$event2->getUuid(),
(string)$event3->getUuid(),
])
->assertSuccessResource();

$this
->assertEventMissing($event1, $event2, $event3)
->assertEventExists($event4, $event5);
}
}
14 changes: 3 additions & 11 deletions tests/Feature/Interfaces/Http/Events/DeleteActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,17 @@ public function testDeleteEvent(): void
{
$event = $this->createEvent();

$events = $this->fakeEvents();
$events->eventShouldBeFound($event->getUuid(), $event);
$events->eventShouldBeDeleted($event->getUuid());

$this->http
->deleteEvent($event->getUuid())
->assertSuccessResource();

$this->assertEventMissing($event);
}

public function testDeleteNonExistEvent(): void
{
$event = $this->createEvent();

$events = $this->fakeEvents();
$events->eventShouldBeFound($event->getUuid(), null);
$events->eventShouldNotBeDeleted($event->getUuid());

$this->http
->deleteEvent($event->getUuid())
->deleteEvent($this->randomUuid())
->assertSuccessResource();
}
}
12 changes: 1 addition & 11 deletions tests/Feature/Interfaces/Http/Events/ShowActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Tests\Feature\Interfaces\Http\Events;

use App\Application\Domain\Entity\Json;
use App\Application\Event\EventTypeMapperInterface;
use Modules\Events\Domain\Event;
use Modules\Events\Interfaces\Http\Resources\EventResource;
use Tests\Feature\Interfaces\Http\ControllerTestCase;

Expand All @@ -16,10 +13,6 @@ public function testShowEvent(): void
{
$event = $this->createEvent();

$this->fakeEvents()
->shouldRequestEventByUuid($event->getUuid())
->andReturnEvent($event);

$this->http
->showEvent($event->getUuid())
->assertOk()
Expand All @@ -31,15 +24,12 @@ public function testShowEvent(): void
public function testNotFoundShowEvent(): void
{
$uuid = $this->randomUuid();
$this->fakeEvents()
->shouldRequestEventByUuid($uuid)
->andThrowNotFound();

$this->http
->showEvent($uuid)
->assertNotFound()
->assertJsonResponseSame([
'message' => 'Event not found',
'message' => 'Event with given uuid ['.$uuid.'] was not found.',
'code' => 404,
]);
}
Expand Down
Loading