Skip to content

Commit a4e6c35

Browse files
committed
Added test suite.
1 parent f74feb7 commit a4e6c35

File tree

6 files changed

+1214
-7
lines changed

6 files changed

+1214
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# PHPStorm Files
22
/.idea
3+
/vendor
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
<?php
22

33
namespace MattDunbar\UserMicroServiceBundle\Tests\Controller;
4+
use MattDunbar\UserMicroServiceBundle\Controller\AuthenticationController;
5+
use MattDunbar\UserMicroServiceBundle\Tests\Utilities\Mocker;
46

5-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6-
7-
class AuthenticationControllerTest extends WebTestCase
7+
class AuthenticationControllerTest extends \PHPUnit_Framework_TestCase
88
{
9-
public function testIndex()
9+
/** @var \MattDunbar\UserMicroServiceBundle\Tests\Utilities\Mocker */
10+
protected $mocker;
11+
12+
protected function setUp()
1013
{
11-
$client = static::createClient();
14+
$this->mocker = new Mocker;
15+
}
16+
17+
/**
18+
* Tests check action.
19+
*/
20+
public function testCheckAction()
21+
{
22+
$sampleUserId = 123;
23+
$sampleUserEmail = '[email protected]';
24+
25+
$userMock = $this->mocker->mockUser($sampleUserId, $sampleUserEmail);
26+
$tokenMock = $this->mocker->mockToken($userMock);
27+
$securityContextMock = $this->mocker->mockSecurityContext($tokenMock);
28+
$userManagerMock = $this->getMock('\FOS\UserBundle\Model\UserManagerInterface');
29+
30+
$authenticationController = new AuthenticationController($userManagerMock, $securityContextMock);
31+
$checkAction = $authenticationController->checkAction();
1232

13-
$crawler = $client->request('GET', '/hello/Fabien');
33+
$jsonContent = $checkAction->getContent();
34+
$content = json_decode($jsonContent);
1435

15-
$this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0);
36+
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\JsonResponse', $checkAction);
37+
$this->assertEquals(200, $checkAction->getStatusCode());
38+
$this->assertEquals($sampleUserId, $content->user_id);
39+
$this->assertEquals($sampleUserEmail, $content->email);
1640
}
1741
}

Tests/Utilities/Mocker.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace MattDunbar\UserMicroServiceBundle\Tests\Utilities;
3+
4+
class Mocker
5+
{
6+
/**
7+
* Create mock user object.
8+
*
9+
* @param integer|bool $userId
10+
* @param string|bool $email
11+
* @return \MattDunbar\UserMicroServiceBundle\Entity\User
12+
*/
13+
function mockUser($userId = false, $email = false) {
14+
$user = $this->getMock('\MattDunbar\UserMicroServiceBundle\Entity\User');
15+
if($userId) {
16+
$user->expects($this->once())
17+
->method('getId')
18+
->will($this->returnValue($userId));
19+
}
20+
if($email) {
21+
$user->expects($this->once())
22+
->method('getEmail')
23+
->will($this->returnValue($email));
24+
}
25+
return $user;
26+
}
27+
28+
/**
29+
* Create mock token object.
30+
*
31+
* @param \MattDunbar\UserMicroServiceBundle\Entity\User|bool $user
32+
* @return \Symfony\Component\Security\Core\Authentication\Token\TokenInterface
33+
*/
34+
function mockToken($user = false) {
35+
$token = $this->getMock('\Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
36+
if($user) {
37+
$token->expects($this->once())
38+
->method('getUser')
39+
->will($this->returnValue($user));
40+
}
41+
return $token;
42+
}
43+
44+
/**
45+
* Create mock security context object.
46+
*
47+
* @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface|bool $token
48+
* @return \Symfony\Component\Security\Core\SecurityContextInterface
49+
*/
50+
function mockSecurityContext($token = false) {
51+
$securityContext = $this->getMockBuilder('\Symfony\Component\Security\Core\SecurityContextInterface')
52+
->disableOriginalConstructor()
53+
->getMock();
54+
if($token) {
55+
$securityContext->expects($this->once())
56+
->method('getToken')
57+
->will($this->returnValue($token));
58+
}
59+
return $securityContext;
60+
}
61+
}

Tests/bootstrap.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) {
3+
throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?');
4+
}
5+
require $autoloadFile;
6+
7+
require __DIR__.'/../Controller/AuthenticationController.php';

0 commit comments

Comments
 (0)