Skip to content

Commit

Permalink
change EventTable to one return Type
Browse files Browse the repository at this point in the history
  • Loading branch information
BibaltiK committed Jan 18, 2024
1 parent b48e2c2 commit dd27408
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/App/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

interface EventRepository extends Repository
{
public function insert(Event $event): int|bool;
public function insert(Event $event): int;

public function findByTitle(string $title): bool|array;
public function findByTitle(string $title): array;

public function findAllActive(): bool|array;
public function findAllActive(): array;

public function findAllInactive(): bool|array;
public function findAllInactive(): array;

public function remove(Event $event): bool|int;
public function remove(Event $event): bool;

public function findAll(string $order = 'startTime', string $sort = 'DESC'): array;
}
33 changes: 23 additions & 10 deletions src/App/Table/EventTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace App\Table;

use App\Entity\Event;
use App\Exception\DuplicateEntryException;
use App\Repository\EventRepository;

class EventTable extends AbstractTable implements EventRepository
{
public function insert(Event $event): int|bool
public function insert(Event $event): int
{
$values = [
'userId' => $event->getUserId(),
Expand All @@ -20,38 +21,50 @@ public function insert(Event $event): int|bool

$insertStatus = $this->query->insertInto($this->table, $values)->execute();

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

return (int)$insertStatus;
}

public function findAll(string $order = 'startTime', string $sort = 'DESC'): array
{
return $this->query->from($this->table)->orderBy($order . ' ' . $sort)->fetchAll() ?: [];
$result = $this->query->from($this->table)->orderBy($order . ' ' . $sort)->fetchAll();

return $result ?: [];
}

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

return $result ?: [];
}

public function findAllActive(): bool|array
public function findAllActive(): array
{
return $this->query->from($this->table)
$result = $this->query->from($this->table)
->where('active', 1)
->orderBy('startTime DESC')
->fetchAll();

return $result ?: [];
}

public function findAllInactive(): bool|array
public function findAllInactive(): array
{
return $this->query->from($this->table)
$result = $this->query->from($this->table)
->where('active', 0)
->orderBy('startTime DESC')
->fetchAll();

return $result ?: [];
}

public function remove(Event $event): bool|int
public function remove(Event $event): bool
{
return $this->query->deleteFrom($this->table)
->where('id', $event->getId())
Expand Down
38 changes: 34 additions & 4 deletions tests/Unit/App/Table/EventTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Test\Unit\App\Table;

use App\Entity\Event;
use App\Exception\DuplicateEntryException;
use App\Table\EventTable;
use Test\Unit\Mock\Database\MockQueryForCanNot;
use Test\Unit\Mock\TestConstants;

/**
Expand All @@ -19,6 +21,7 @@ public function testCanGetTableName(): void
public function testCanInsertEvent(): void
{
$event = new Event();

$event->setTitle(TestConstants::EVENT_CREATE_TITLE);

$insertLastId = $this->table->insert($event);
Expand All @@ -30,9 +33,9 @@ public function testCanNotInsertEvent(): void
{
$event = new Event();

$insertLastId = $this->table->insert($event);
self::expectException(DuplicateEntryException::class);

self::assertSame(false, $insertLastId);
$this->table->insert($event);
}

public function testCanFindById(): void
Expand All @@ -42,7 +45,7 @@ public function testCanFindById(): void
self::assertSame($this->fetchResult, $event);
}

public function testFindByIdHaveEmptyResult(): void
public function testFindByIdHasEmptyResult(): void
{
$event = $this->table->findById(TestConstants::EVENT_ID_UNUSED);

Expand All @@ -56,6 +59,15 @@ public function testCanFindAll(): void
self::assertSame($this->fetchAllResult, $event);
}

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

$event = $table->findAll();

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

public function testCanFindByName(): void
{
$event = $this->table->findByTitle(TestConstants::EVENT_TITLE);
Expand All @@ -70,21 +82,39 @@ public function testCanFindAllActive(): void
self::assertSame($this->fetchAllResult, $event);
}

public function testFindAllActiveHasEmptyResult(): void
{
$table = new EventTable(new MockQueryForCanNot());

$event = $table->findAllActive();

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

public function testCanFindAllNotActive(): void
{
$event = $this->table->findAllInactive();

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

public function testFindAllNotActiveHasEmptyResult(): void
{
$table = new EventTable(new MockQueryForCanNot());

$event = $table->findAllInactive();

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

public function testCanRemoveEvent(): void
{
$event = new Event();
$event->setId(TestConstants::EVENT_ID);

$removeStatus = $this->table->remove($event);

self::assertSame(1, $removeStatus);
self::assertSame(true, $removeStatus);
}

public function testCanNotRemoveEvent(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Mock/Database/MockDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public function execute(): bool|int
return $this->handle($this->statements['DELETE FROM'], $this->statements['WHERE'], $this->parameters['WHERE']);
}

private function handle(string $table, array $where, array $value): bool|int
private function handle(string $table, array $where, array $value): bool
{
return match ($table) {
'Event', 'MockEvent' => $this->handleEvent($where, $value),
default => false,
};
}

private function handleEvent(array $where, array $value): bool|int
private function handleEvent(array $where, array $value): bool
{
if ($where[0][1] === 'id = ?' && $value[0] === TestConstants::EVENT_ID) {
return 1;
return true;
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Mock/Database/MockSelectForFetchAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function fetch(?string $column = null, int $cursorOrientation = PDO::FETC
return false;
}

public function fetchAll($index = '', $selectOnly = ''): bool|array
public function fetchAll($index = '', $selectOnly = ''): array
{
return false;
return [];
}

private function handle(string $from, array $where, array $params): bool|array
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Mock/Table/MockEventTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function findById(int $id): array
};
}

public function findByTitle(string $title): bool|array
public function findByTitle(string $title): array
{
return match ($title) {
TestConstants::EVENT_TITLE => [
'title' => $title,
],
default => false
default => []
};
}
}

0 comments on commit dd27408

Please sign in to comment.