Commit 5de0d8b7 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add tests for commands

parent 8e3b920b
......@@ -7,6 +7,8 @@ use Source\Entities\User;
class DoorCommandHandler implements CommandHandler
{
protected const OPCODE_LENGTH = 2;
/**
* @var \Source\UseCases\Door\Commands\Command[]
*/
......@@ -25,13 +27,13 @@ class DoorCommandHandler implements CommandHandler
public function handle(User $user, string $doorId, string $commandString): bool
{
// Need to be able to parse OPCode
if (strlen($commandString) < 2) {
if (strlen($commandString) < self::OPCODE_LENGTH) {
return false;
}
// First 2 digits are the command opcode
$opcode = substr($commandString, 0, 2);
$args = substr($commandString, 2);
// Separate opcode from arguments
$opcode = substr($commandString, 0, self::OPCODE_LENGTH);
$args = substr($commandString, self::OPCODE_LENGTH);
$command = $this->commands[$opcode] ?? null;
......
......@@ -86,7 +86,7 @@ class OverrideCommand extends Command
if ($override) {
try {
$this->overrides->updateOverride($override->getId(), new Override(
return $this->overrides->updateOverride($override->getId(), new Override(
$override->getId(),
$override->getReason() . "\nCancelled at keypad by {$user->getDisplayName()}.",
$override->getUserId(),
......@@ -94,7 +94,7 @@ class OverrideCommand extends Command
$override->getType(),
$override->getStart(),
Carbon::now()->subSecond()
));
)) !== null;
} catch (EntityNotFoundException $e) {
return false;
}
......
<?php
namespace Tests\Doubles;
use Source\Entities\User;
use Source\UseCases\Door\Commands\Command;
class BasicDoorCommandStub extends Command
{
protected string $opcode = '69';
public bool $success = true;
protected string $args;
/**
* @inheritDoc
*/
public function execute(User $user, string $doorId, string $args): bool
{
$this->args = $args;
return $this->success;
}
/**
* @return string
*/
public function getArgs(): string
{
return $this->args;
}
}
......@@ -3,7 +3,6 @@
namespace Tests\Doubles;
use Source\Entities\User;
use Source\UseCases\Door\Commands\CommandHandler;
......
......@@ -3,7 +3,6 @@
namespace Tests\Unit\Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use PHPUnit\Framework\TestCase;
......
......@@ -3,7 +3,6 @@
namespace Tests\Unit\Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use PHPUnit\Framework\TestCase;
......
<?php
namespace Tests\Unit\Source\UseCases\Door\Commands;
use Source\Entities\User;
use PHPUnit\Framework\TestCase;
use Tests\Doubles\BasicDoorCommandStub;
use Source\UseCases\Door\Commands\DoorCommandHandler;
class DoorCommandHandlerTest extends TestCase
{
/**
* @var \Tests\Doubles\BasicDoorCommandStub
*/
protected BasicDoorCommandStub $command;
/**
* @var \Source\UseCases\Door\Commands\DoorCommandHandler
*/
protected DoorCommandHandler $handler;
protected bool $success = false;
public function setUp(): void
{
parent::setUp();
$this->command = new BasicDoorCommandStub();
$this->handler = new DoorCommandHandler($this->command);
}
protected function handleTest(string $commandString): void
{
$this->success = $this->handler->handle(
new User(1, '', '', '', '', ),
'1',
$commandString
);
}
/**
* @test
*/
public function it_fails_if_command_is_not_large_enough(): void
{
$this->handleTest('a');
$this->assertFalse($this->success);
}
/**
* @test
*/
public function it_fails_if_opcode_does_not_match_any_command(): void
{
$this->handleTest('297974');
$this->assertFalse($this->success);
}
/**
* @test
*/
public function it_separates_arguments(): void
{
$this->handleTest('69123');
$this->assertEquals('123', $this->command->getArgs());
}
/**
* @test
*/
public function it_can_succeed(): void
{
$this->handleTest('69');
$this->assertTrue($this->success);
}
/**
* @test
*/
public function it_fails_if_command_fails(): void
{
$this->command->success = false;
$this->handleTest('69');
$this->assertFalse($this->success);
}
}
<?php
namespace Tests\Unit\Source\UseCases\Door\Commands;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Override;
use PHPUnit\Framework\TestCase;
use Source\UseCases\Door\Commands\OverrideCommand;
use Source\Gateways\Overrides\InMemoryOverridesRepository;
class OverrideCommandTest extends TestCase
{
protected const VALID_USER_ID = 200438;
protected const VALID_DOOR_ID = 191390;
/**
* @var \Source\Gateways\Overrides\InMemoryOverridesRepository
*/
protected InMemoryOverridesRepository $overrides;
/**
* @var \Source\UseCases\Door\Commands\OverrideCommand
*/
protected OverrideCommand $command;
protected bool $success = false;
public function setUp(): void
{
parent::setUp();
$this->overrides = new InMemoryOverridesRepository();
$this->command = new OverrideCommand($this->overrides);
}
protected function createUser(): User
{
return new User(self::VALID_USER_ID, '', '', '', '');
}
/**
* @param string $args
*/
protected function handleTest(string $args): void
{
$this->success = $this->command->execute($this->createUser(), self::VALID_DOOR_ID, $args);
}
/**
* @test
*/
public function it_fails_if_no_type_is_specified(): void
{
$this->handleTest('');
$this->assertFalse($this->success);
}
/**
* @test
*/
public function it_fails_if_bad_type_is_specified(): void
{
$this->handleTest('9abc');
$this->assertFalse($this->success);
}
/**
* @test
*/
public function it_adds_open_override(): void
{
$this->handleTest('112');
$this->assertTrue($this->success);
$override = $this->overrides->get(0);
$this->assertNotNull($override);
$this->assertEquals(Override::TYPE_OPEN, $override->getType());
$this->assertEquals(12, $override->getEnd()->diffInRealMinutes($override->getStart()));
$this->assertEquals(self::VALID_USER_ID, $override->getUserId());
$this->assertEquals(self::VALID_DOOR_ID, $override->getDoorId());
}
/**
* @test
*/
public function it_adds_closed_override(): void
{
$this->handleTest('210');
$this->assertTrue($this->success);
$override = $this->overrides->get(0);
$this->assertNotNull($override);
$this->assertEquals(Override::TYPE_LOCKED, $override->getType());
$this->assertEquals(10, $override->getEnd()->diffInRealMinutes($override->getStart()));
$this->assertEquals(self::VALID_USER_ID, $override->getUserId());
$this->assertEquals(self::VALID_DOOR_ID, $override->getDoorId());
}
/**
* @test
*/
public function it_cancels_override_for_current_door(): void
{
$override = $this->overrides->addOverride(new Override(
0,
'',
self::VALID_USER_ID,
self::VALID_DOOR_ID,
Override::TYPE_OPEN,
Carbon::now()->subDays(2),
Carbon::now()->addDays(2)
));
$this->handleTest('0');
$override = $this->overrides->get(0);
$this->assertTrue($this->success);
$this->assertTrue($override->getEnd()->isBefore(Carbon::now()->addDay()));
}
/**
* @test
*/
public function it_will_not_cancel_override_for_another_door(): void
{
$override = $this->overrides->addOverride(new Override(
0,
'',
self::VALID_USER_ID,
106271,
Override::TYPE_OPEN,
Carbon::now()->subDays(2),
Carbon::now()->addDays(2)
));
$this->handleTest('0');
$override = $this->overrides->get(0);
$this->assertTrue($this->success);
$this->assertTrue($override->getEnd()->isAfter(Carbon::now()->addDay()));
}
/**
* @test
*/
public function it_fails_on_open_override_when_no_length_is_given(): void
{
$this->handleTest('1');
$this->assertFalse($this->success);
}
/**
* @test
*/
public function it_fails_on_close_override_when_no_length_is_given(): void
{
$this->handleTest('2');
$this->assertFalse($this->success);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment