Skip to content

Commit

Permalink
refactor: change ParticipantTable to one return Type
Browse files Browse the repository at this point in the history
  • Loading branch information
BibaltiK committed Jan 23, 2024
1 parent dd27408 commit 9b1c889
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/App/Repository/ParticipantRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

interface ParticipantRepository extends Repository
{
public function insert(Participant $participant): int|bool;
public function insert(Participant $participant): int;

public function remove(Participant $participant): int|bool;
public function remove(Participant $participant): bool;

public function findByUserId(int $userId): bool|array;
public function findByUserId(int $userId): array;

public function findUserForAnEvent(int $userId, int $eventId): bool|array;
public function findUserForAnEvent(int $userId, int $eventId): array;

public function findActiveParticipantsByEvent(int $eventId): array;
}
14 changes: 10 additions & 4 deletions src/App/Table/AbstractTable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace App\Table;

Expand All @@ -25,13 +27,17 @@ public function getTableName(): string

public function findById(int $id): array
{
return $this->query->from($this->table)
$result = $this->query->from($this->table)
->where('id', $id)
->fetch() ?: [];
->fetch();

return $result ?: [];
}

public function findAll(): array
{
return $this->query->from($this->table)->fetchAll() ?: [];
$result = $this->query->from($this->table)->fetchAll();

return $result ?: [];
}
}
32 changes: 22 additions & 10 deletions src/App/Table/ParticipantTable.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace App\Table;

use App\Entity\Participant;
use App\Exception\DuplicateEntryException;
use App\Repository\ParticipantRepository;

class ParticipantTable extends AbstractTable implements ParticipantRepository
{
public function insert(Participant $participant): int|bool
public function insert(Participant $participant): int
{
$values = [
'userId' => $participant->getUserId(),
Expand All @@ -19,41 +21,51 @@ public function insert(Participant $participant): int|bool
->onDuplicateKeyUpdate(['subscribed' => 1])
->execute();

return $insertStatus !== false ? (int)$insertStatus : false;
if (!$insertStatus) {
throw new DuplicateEntryException('Participant', $participant->getId());
}

return (int)$insertStatus;
}

public function remove(Participant $participant): int|bool
public function remove(Participant $participant): bool
{
return $this->query->update($this->table)
return (bool)$this->query->update($this->table)
->set(['subscribed' => 0])
->where('userId', $participant->getUserId())
->where('eventId', $participant->getEventId())
->execute();
}

public function findByUserId(int $userId): bool|array
public function findByUserId(int $userId): array
{
return $this->query->from($this->table)
$result = $this->query->from($this->table)
->where('userId', $userId)
->fetch();

return $result ?: [];
}

public function findUserForAnEvent(int $userId, int $eventId): bool|array
public function findUserForAnEvent(int $userId, int $eventId): array
{
return $this->query->from($this->table)
$result = $this->query->from($this->table)
->where('userId', $userId)
->where('eventId', $eventId)
->where('subscribed', 1)
->fetch();

return $result ?: [];
}

public function findActiveParticipantsByEvent(int $eventId): array
{
return $this->query->from($this->table)
$result = $this->query->from($this->table)
->where('eventId', $eventId)
->where('subscribed', 1)
->where('approved', 1)
->where('disqualified', 0)
->fetchAll();

return $result ?: [];
}
}
9 changes: 8 additions & 1 deletion tests/Unit/App/Table/EventTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testCanInsertEvent(): void
self::assertSame(1, $insertLastId);
}

public function testCanNotInsertEvent(): void
public function testInsertEventThrowsException(): void
{
$event = new Event();

Expand Down Expand Up @@ -75,6 +75,13 @@ public function testCanFindByName(): void
self::assertSame($this->fetchResult, $event);
}

public function testFindByNameHasEmptyResult(): void
{
$event = $this->table->findByTitle(TestConstants::EVENT_TITLE_UNUSED);

self::assertSame([], $event);
}

public function testCanFindAllActive(): void
{
$event = $this->table->findAllActive();
Expand Down
44 changes: 40 additions & 4 deletions tests/Unit/App/Table/ParticipantTableTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Test\Unit\App\Table;

use App\Entity\Participant;
use App\Exception\DuplicateEntryException;
use App\Table\ParticipantTable;
use Test\Unit\Mock\Database\MockQueryForCanNot;
use Test\Unit\Mock\TestConstants;

/**
Expand All @@ -26,14 +30,14 @@ public function testCanInsertParticipant(): void
self::assertSame(1, $insertParticipant);
}

public function testCanNotInsertParticipant(): void
public function testInsertParticipantThrowsException(): void
{
$participant = new Participant();
$participant->setUserId(TestConstants::USER_ID);

$insertParticipant = $this->table->insert($participant);
self::expectException(DuplicateEntryException::class);

self::assertSame(false, $insertParticipant);
$this->table->insert($participant);
}

public function testCanRemoveParticipant(): void
Expand Down Expand Up @@ -66,24 +70,56 @@ public function testCanFindAll(): void
self::assertSame($this->fetchAllResult, $project);
}

public function testFindAllHasEmptyResult(): void
{
$table = new ParticipantTable(new MockQueryForCanNot());

$project = $table->findAll();

self::assertSame([], $project);
}

public function testCanFindByUserId(): void
{
$participant = $this->table->findByUserId(TestConstants::USER_ID);

self::assertSame($this->fetchResult, $participant);
}

public function testFindByUserIdHasEmptyResult(): void
{
$participant = $this->table->findByUserId(TestConstants::USER_ID_UNUSED);

self::assertSame([], $participant);
}

public function testCanFindByUserIdAndEventId(): void
{
$participant = $this->table->findUserForAnEvent(TestConstants::USER_ID, TestConstants::EVENT_ID);

self::assertSame($this->fetchResult, $participant);
}

public function testFindByUserIdAndEventIdHasEmptyResult(): void
{
$participant = $this->table->findUserForAnEvent(TestConstants::USER_ID_UNUSED, TestConstants::EVENT_ID_UNUSED);

self::assertSame([], $participant);
}

public function testCanFindActiveParticipantByEvent(): void
{
$participant = $this->table->findActiveParticipantsByEvent(TestConstants::EVENT_ID);

self::assertSame($this->fetchAllResult, $participant);
}

public function testFindActiveParticipantByEventHasEmptyResult(): void
{
$table = new ParticipantTable(new MockQueryForCanNot());

$participant = $table->findActiveParticipantsByEvent(TestConstants::EVENT_ID_UNUSED);

self::assertSame([], $participant);
}
}
15 changes: 8 additions & 7 deletions tests/Unit/Mock/Table/MockParticipantTable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Test\Unit\Mock\Table;

Expand All @@ -13,22 +14,22 @@ public function __construct()
parent::__construct(new MockQuery());
}

public function remove(Participant $participant): int|bool
public function remove(Participant $participant): bool
{
return $participant->getId() === 1 ? 1 : false;
return $participant->getId() === 1;
}

public function findById(int $id): array
{
return $id === 1 ? ['id' => $id] : [];
}

public function findByUserId(int $userId): bool|array
public function findByUserId(int $userId): array
{
return $userId === 1 ? ['userId' => 1] : false;
return $userId === 1 ? ['userId' => 1] : [];
}

public function findUserForAnEvent(int $userId, int $eventId): bool|array
public function findUserForAnEvent(int $userId, int $eventId): array
{
if ($userId === 1 && $eventId === 1) {
return [
Expand All @@ -37,6 +38,6 @@ public function findUserForAnEvent(int $userId, int $eventId): bool|array
];
}

return false;
return [];
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Mock/TestConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class TestConstants

public const EVENT_TITLE = 'Test Title';

public const EVENT_TITLE_UNUSED = 'Title can not be found';

public const EVENT_TITLE_THROW_EXCEPTION = 'Test Title throw Exception';

public const EVENT_CREATE_TITLE = 'Test Create Title';
Expand Down

0 comments on commit 9b1c889

Please sign in to comment.