Commit f4e75ac8 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

setup code coverage paths and fix some bugs and also add group user tests

parent ccb58bd8
Pipeline #2546 failed with stages
in 1 minute and 53 seconds
......@@ -11,3 +11,4 @@ Homestead.yaml
npm-debug.log
yarn-error.log
.php_cs.cache
cov/
......@@ -26,6 +26,11 @@
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix=".php">./app/Console</directory>
<directory suffix="ServiceProvider.php">./src</directory>
</exclude>
</whitelist>
</filter>
<php>
......
......@@ -9,7 +9,6 @@ use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Groups\DatabaseGroupsRepository;
// TODO: Write tests for this
class DatabaseGroupUserRepository implements GroupUserRepository
{
/**
......@@ -18,7 +17,7 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function getGroupsForUser(string $userId): array
{
/** @var User|null $user */
$user = User::find($userId);
$user = User::find((int)$userId);
if (!$user) {
throw new EntityNotFoundException();
......@@ -35,7 +34,7 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function getUsersForGroup(string $groupId): array
{
/** @var Group|null $group */
$group = Group::find($groupId);
$group = Group::find((int)$groupId);
if (!$group) {
throw new EntityNotFoundException();
......@@ -52,9 +51,9 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function addUserToGroup(string $userId, string $groupId): void
{
/** @var User|null $user */
$user = User::find($userId);
$user = User::find((int)$userId);
/** @var Group|null $group */
$group = Group::find($groupId);
$group = Group::find((int)$groupId);
if (!$user || !$group) {
throw new EntityNotFoundException();
......@@ -69,9 +68,9 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function removeUserFromGroup(string $userId, string $groupId): void
{
/** @var User|null $user */
$user = User::find($userId);
$user = User::find((int)$userId);
/** @var Group|null $group */
$group = Group::find($groupId);
$group = Group::find((int)$groupId);
if (!$user || !$group) {
throw new EntityNotFoundException();
......
......@@ -12,7 +12,7 @@ class DatabaseGroupsRepository implements GroupsRepository
*/
public function get(string $groupId): ?Group
{
$group = \App\Group::find($groupId);
$group = \App\Group::find((int)$groupId);
if (!$group) {
return null;
......@@ -25,7 +25,7 @@ class DatabaseGroupsRepository implements GroupsRepository
* @param \App\Group $group
* @return \Source\Entities\Group
*/
protected static function makeGroupFromDbGroup(\App\Group $group): Group
public static function makeGroupFromDbGroup(\App\Group $group): Group
{
return new Group(
$group->id,
......@@ -74,7 +74,7 @@ class DatabaseGroupsRepository implements GroupsRepository
*/
public function update(string $groupId, Group $group): ?Group
{
$dbGroup = \App\Group::find($groupId);
$dbGroup = \App\Group::find((int)$groupId);
if (!$dbGroup) {
return null;
......@@ -92,7 +92,7 @@ class DatabaseGroupsRepository implements GroupsRepository
*/
public function delete(string $groupId): bool
{
return \App\Group::destroy($groupId);
return \App\Group::destroy((int)$groupId);
}
/**
......@@ -100,6 +100,6 @@ class DatabaseGroupsRepository implements GroupsRepository
*/
public function exists(string $groupId): bool
{
return \App\Group::find($groupId) !== null;
return $this->get($groupId) !== null;
}
}
......@@ -12,7 +12,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function get(string $userId): ?User
{
$user = \App\User::find($userId);
$user = \App\User::find((int)$userId);
if (!$user) {
return null;
......@@ -25,7 +25,7 @@ class DatabaseUsersRepository implements UsersRepository
* @param \App\User $user
* @return \Source\Entities\User
*/
protected static function makeUserFromDbUser(\App\User $user): User
public static function makeUserFromDbUser(\App\User $user): User
{
return new User(
$user->id,
......@@ -122,7 +122,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function update(string $userId, User $user): ?User
{
$dbUser = \App\User::find($userId);
$dbUser = \App\User::find((int)$userId);
if (!$dbUser) {
return null;
......@@ -140,7 +140,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function delete(string $userId): bool
{
return \App\User::destroy($userId);
return \App\User::destroy((int)$userId);
}
/**
......@@ -148,7 +148,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function exists(string $userId): bool
{
return \App\User::find($userId) !== null;
return $this->get($userId) !== null;
}
/**
......
......@@ -27,6 +27,6 @@ class DoorDatabaseTest extends DatabaseTestCase
$this->assertEquals($door, $this->doors->getByToken('token'));
$this->assertNull($this->doors->getByToken('reee'));
$this->assertNull($this->doors->getByToken('i am autistic'));
}
}
......@@ -52,6 +52,14 @@ class GroupDatabaseTest extends DatabaseTestCase
$this->assertEquals($group, $this->groups->get(1));
}
/**
* @test
*/
public function it_cannot_get_non_existent_user(): void
{
$this->assertNull($this->groups->get('asdf'));
}
/**
* @test
*/
......@@ -74,6 +82,8 @@ class GroupDatabaseTest extends DatabaseTestCase
$this->assertFalse($this->groups->exists(1));
$this->assertFalse($this->groups->delete(2));
$this->assertFalse($this->groups->delete('asdf'));
}
/**
......@@ -85,6 +95,7 @@ class GroupDatabaseTest extends DatabaseTestCase
$this->assertTrue($this->groups->exists(1));
$this->assertFalse($this->groups->exists(2));
$this->assertFalse($this->groups->exists('ree'));
}
/**
......@@ -101,4 +112,12 @@ class GroupDatabaseTest extends DatabaseTestCase
$this->assertEquals('new title', $group->getTitle());
$this->assertEquals('new desc', $group->getDescription());
}
/**
* @test
*/
public function it_cannot_update_non_existent_group(): void
{
$this->assertNull($this->groups->update(1, new Group(0, 'new title', 'new desc')));
}
}
<?php
namespace Tests\Database;
use Source\Entities\User;
use Source\Entities\Group;
use Tests\DatabaseTestCase;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Groups\DatabaseGroupsRepository;
use Source\Gateways\GroupUser\DatabaseGroupUserRepository;
class GroupUserDatabaseTest extends DatabaseTestCase
{
/**
* @var \Source\Gateways\Users\DatabaseUsersRepository
*/
protected DatabaseUsersRepository $users;
/**
* @var \Source\Gateways\Groups\DatabaseGroupsRepository
*/
protected DatabaseGroupsRepository $groups;
/**
* @var \Source\Gateways\GroupUser\DatabaseGroupUserRepository
*/
protected DatabaseGroupUserRepository $groupUsers;
public function setUp(): void
{
parent::setUp();
$this->users = new DatabaseUsersRepository();
$this->groups = new DatabaseGroupsRepository();
$this->groupUsers = new DatabaseGroupUserRepository();
$this->users->create(new User(0, '', '', '', null, '', null, null));
$this->groups->create(new Group(0, '', ''));
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_get_groups_for_non_existent_user(): void
{
$this->expectException(EntityNotFoundException::class);
$this->groupUsers->getGroupsForUser('ree');
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_get_users_for_non_existent_group(): void
{
$this->expectException(EntityNotFoundException::class);
$this->groupUsers->getUsersForGroup('ree');
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_add_user_to_non_existent_group(): void
{
$this->expectException(EntityNotFoundException::class);
$this->groupUsers->addUserToGroup('1', 'ree');
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_add_group_to_non_existent_user(): void
{
$this->expectException(EntityNotFoundException::class);
$this->groupUsers->addUserToGroup('re', '1');
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_remove_user_to_non_existent_group(): void
{
$this->expectException(EntityNotFoundException::class);
$this->groupUsers->removeUserFromGroup('1', 'ree');
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_cannot_remove_group_to_non_existent_user(): void
{
$this->expectException(EntityNotFoundException::class);
$this->groupUsers->removeUserFromGroup('re', '1');
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_can_add_remove_and_get_groups_for_users(): void
{
$this->groupUsers->addUserToGroup('1', '1');
$this->assertCount(1, $this->groupUsers->getGroupsForUser('1'));
$this->groupUsers->removeUserFromGroup('1', '1');
$this->assertCount(0, $this->groupUsers->getGroupsForUser('1'));
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_can_get_users_for_a_group(): void
{
$this->groupUsers->addUserToGroup('1', '1');
$this->assertCount(1, $this->groupUsers->getUsersForGroup('1'));
}
}
......@@ -121,6 +121,8 @@ class UserDatabaseTest extends DatabaseTestCase
$this->assertFalse($this->users->exists(1));
$this->assertFalse($this->users->delete(2));
$this->assertFalse($this->users->delete('asdf'));
}
/**
......@@ -135,6 +137,14 @@ class UserDatabaseTest extends DatabaseTestCase
$this->assertEquals($user, $userToCompare);
}
/**
* @test
*/
public function it_cannot_get_non_existent_user(): void
{
$this->assertNull($this->users->get('asdf'));
}
/**
* @test
*/
......@@ -144,6 +154,7 @@ class UserDatabaseTest extends DatabaseTestCase
$this->assertTrue($this->users->exists(1));
$this->assertFalse($this->users->exists(2));
$this->assertFalse($this->users->exists('asdf'));
}
/**
......
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