Skip to content

Commit

Permalink
Merge pull request #1504 from tarlepp/chore(tests)/refactor-missing-t…
Browse files Browse the repository at this point in the history
…ests

Chore(tests) - Added more integrationd tests
  • Loading branch information
tarlepp committed Dec 7, 2021
2 parents e83c4f9 + 7644684 commit 1d16f01
Show file tree
Hide file tree
Showing 18 changed files with 834 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Rest/Controller.php
Expand Up @@ -60,7 +60,7 @@ public function getResponseHandler(): ResponseHandlerInterface
}

#[Required]
public function setResponseHandler(ResponseHandler $responseHandler): self
public function setResponseHandler(ResponseHandler $responseHandler): static
{
$this->responseHandler = $responseHandler;

Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Interfaces/ControllerInterface.php
Expand Up @@ -41,7 +41,7 @@ public function getResponseHandler(): ResponseHandlerInterface;
* Setter method for `ResponseHandler` service, this is called by Symfony
* DI.
*/
public function setResponseHandler(ResponseHandler $responseHandler): self;
public function setResponseHandler(ResponseHandler $responseHandler): static;

/**
* Getter method for used DTO class for current controller.
Expand Down
50 changes: 50 additions & 0 deletions tests/Integration/Controller/v1/Profile/GroupsControllerTest.php
@@ -0,0 +1,50 @@
<?php
declare(strict_types = 1);
/**
* /tests/Integration/Controller/v1/Profile/GroupsControllerTest.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/

namespace App\Tests\Integration\Controller\v1\Profile;

use App\Controller\v1\Profile\GroupsController;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\SerializerInterface;
use Throwable;

/**
* Class GroupsControllerTest
*
* @package App\Tests\Integration\Controller\v1\Profile
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class GroupsControllerTest extends KernelTestCase
{
/**
* @throws Throwable
*
* @testdox Test that `__invoke(User $loggedInUser)` method calls expected service methods
*/
public function testThatInvokeMethodCallsExpectedMethods(): void
{
$user = new User();

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();

$serializer
->expects(self::once())
->method('serialize')
->with(
$user->getUserGroups()->toArray(),
'json',
[
'groups' => 'set.UserProfileGroups',
],
)
->willReturn('{}');

(new GroupsController($serializer))($user);
}
}
61 changes: 61 additions & 0 deletions tests/Integration/Controller/v1/Profile/IndexControllerTest.php
@@ -0,0 +1,61 @@
<?php
declare(strict_types = 1);
/**
* /tests/Integration/Controller/v1/Profile/IndexControllerTest.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/

namespace App\Tests\Integration\Controller\v1\Profile;

use App\Controller\v1\Profile\IndexController;
use App\Entity\User;
use App\Security\RolesService;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\SerializerInterface;
use Throwable;

/**
* Class IndexControllerTest
*
* @package App\Tests\Integration\Controller\v1\Profile
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class IndexControllerTest extends KernelTestCase
{
/**
* @throws Throwable
*
* @testdox Test that `__invoke(User $loggedInUser)` method calls expected service methods
*/
public function testThatInvokeMethodCallsExpectedMethods(): void
{
$user = new User();

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();

$rolesService = $this->getMockBuilder(RolesService::class)
->disableOriginalConstructor()
->getMock();

$serializer
->expects(self::once())
->method('serialize')
->with(
$user,
'json',
[
'groups' => 'set.UserProfile',
],
)
->willReturn('{"roles": ["foo", "bar"]}');

$rolesService
->expects(self::once())
->method('getInheritedRoles')
->with(['foo', 'bar'])
->willReturn(['foo', 'bar']);

(new IndexController($serializer, $rolesService))($user);
}
}
25 changes: 0 additions & 25 deletions tests/Integration/Controller/v1/Profile/ProfileControllerTest.php

This file was deleted.

46 changes: 46 additions & 0 deletions tests/Integration/Controller/v1/Profile/RolesControllerTest.php
@@ -0,0 +1,46 @@
<?php
declare(strict_types = 1);
/**
* /tests/Integration/Controller/v1/Profile/RolesControllerTest.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/

namespace App\Tests\Integration\Controller\v1\Profile;

use App\Controller\v1\Profile\RolesController;
use App\Entity\User;
use App\Security\RolesService;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Throwable;

/**
* Class RolesControllerTest
*
* @package App\Tests\Integration\Controller\v1\Profile
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class RolesControllerTest extends KernelTestCase
{
/**
* @throws Throwable
*
* @testdox Test that `__invoke(User $loggedInUser)` method calls expected service methods
*/
public function testThatInvokeMethodCallsExpectedMethods(): void
{
$user = new User();

$rolesService = $this->getMockBuilder(RolesService::class)
->disableOriginalConstructor()
->getMock();

$rolesService
->expects(self::once())
->method('getInheritedRoles')
->with($user->getRoles())
->willReturn([]);

(new RolesController($rolesService))->__invoke($user);
}
}
33 changes: 33 additions & 0 deletions tests/Integration/Controller/v1/Role/FindOneRoleControllerTest.php
Expand Up @@ -9,8 +9,12 @@
namespace App\Tests\Integration\Controller\v1\Role;

use App\Controller\v1\Role\FindOneRoleController;
use App\Entity\Role;
use App\Resource\RoleResource;
use App\Rest\ResponseHandler;
use App\Utils\Tests\RestIntegrationControllerTestCase;
use Symfony\Component\HttpFoundation\Request;
use Throwable;

/**
* Class FindOneRoleControllerTest
Expand All @@ -31,4 +35,33 @@ class FindOneRoleControllerTest extends RestIntegrationControllerTestCase
* @var class-string
*/
protected string $resourceClass = RoleResource::class;

/**
* @throws Throwable
*
* @testdox Test that `__invoke($role)` method calls expected service methods
*/
public function testThatInvokeMethodCallsExpectedMethods(): void
{
$resource = $this->getMockBuilder(RoleResource::class)->disableOriginalConstructor()->getMock();
$responseHandler = $this->getMockBuilder(ResponseHandler::class)->disableOriginalConstructor()->getMock();

$role = new Role('role');
$request = Request::create('/');

$resource
->expects(self::once())
->method('findOne')
->with('role', true)
->willReturn($role);

$responseHandler
->expects(self::once())
->method('createResponse')
->with($request, $role, $resource);

(new FindOneRoleController($resource))
->setResponseHandler($responseHandler)
->__invoke($request, 'role');
}
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types = 1);
/**
* /tests/Integration/Controller/v1/Role/InheritedRolesControllerTest.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/

namespace App\Tests\Integration\Controller\v1\Role;

use App\Controller\v1\Role\InheritedRolesController;
use App\Entity\Role;
use App\Security\RolesService;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* Class InheritedRolesControllerTest
*
* @package App\Tests\Integration\Controller\v1\Role
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class InheritedRolesControllerTest extends KernelTestCase
{
/**
* @testdox Test that `__invoke($role)` method calls expected service methods
*/
public function testThatInvokeMethodCallsExpectedMethods(): void
{
$rolesService = $this->getMockBuilder(RolesService::class)->disableOriginalConstructor()->getMock();
$role = new Role('Test');

$rolesService
->expects(self::once())
->method('getInheritedRoles')
->with([$role->getId()])
->willReturn([$role]);

(new InheritedRolesController($rolesService))($role);
}
}
@@ -0,0 +1,72 @@
<?php
declare(strict_types = 1);
/**
* /tests/Integration/Controller/v1/User/AttachUserGroupControllerTest.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/

namespace App\Tests\Integration\Controller\v1\User;

use App\Controller\v1\User\AttachUserGroupController;
use App\Entity\Role;
use App\Entity\User;
use App\Entity\UserGroup;
use App\Resource\UserGroupResource;
use App\Resource\UserResource;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\SerializerInterface;
use Throwable;

/**
* Class AttachUserGroupControllerTest
*
* @package App\Tests\Integration\Controller\v1\User
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class AttachUserGroupControllerTest extends KernelTestCase
{
/**
* @throws Throwable
*
* @testdox Test that `__invoke($user, $userGroup)` method calls expected service methods
*/
public function testThatInvokeMethodCallsExpectedMethods(): void
{
$userResource = $this->getMockBuilder(UserResource::class)->disableOriginalConstructor()->getMock();
$userGroupResource = $this->getMockBuilder(UserGroupResource::class)->disableOriginalConstructor()->getMock();
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();

$user = new User();
$userGroup = (new UserGroup())->setRole(new Role('role'));

$userResource
->expects(self::once())
->method('save')
->with($user, false)
->willReturn($user);

$userGroupResource
->expects(self::once())
->method('save')
->with($userGroup, true, true)
->willReturn($userGroup);

$serializer
->expects(self::once())
->method('serialize')
->willReturn('[]');

(new AttachUserGroupController($userResource, $userGroupResource, $serializer))($user, $userGroup);

self::assertTrue(
$user->getUserGroups()->contains($userGroup),
'User entity does not have expected user group',
);

self::assertTrue(
$userGroup->getUsers()->contains($user),
'UserGroup entity does not have expected user',
);
}
}

0 comments on commit 1d16f01

Please sign in to comment.