Skip to content

Instantly share code, notes, and snippets.

@deltaepsilon
Last active December 22, 2015 00:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deltaepsilon/6391565 to your computer and use it in GitHub Desktop.
Save deltaepsilon/6391565 to your computer and use it in GitHub Desktop.
Symfony 2.3: It's not hard to log in programatically via FOSUserBundle, but it's a bit trickier to get MockFileSessionStorage working correctly. I think I finally figured it out with this base class. I intend to extend BaseUserTest instead of WebTestCase for the rest of my test suite.
<?php
namespace CDE\TestBundle\Base;
use FOS\UserBundle\Model\User;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class BaseUserTest extends WebTestCase {
protected $client;
protected $container;
protected $storage;
protected $session;
protected $user;
protected $cookieJar;
protected $cookie;
protected $token;
public function __construct() {
$this->client = static::createClient();
$this->container = $this->client->getContainer();
$this->storage = new MockFileSessionStorage(__dir__.'/../../../../app/cache/test/sessions');
$this->session = new Session($this->storage);
}
public function getUserManager() {
return $this->container->get('cde_user.manager.user');
}
public function getSecurityManager() {
return $this->container->get('fos_user.security.login_manager');
}
public function getUser($role = null) {
if (!isset($this->user)) {
$user = $this->getUserManager()->loadByUsername('user');
if (isset($user)) {
$this->user = $user;
} else {
$this->user = $this->getUserManager()->create();
$this->user->setEnabled(true);
$this->user->setUsername('user');
$this->user->setEmail('user@quiver.is');
$this->user->setPlainPassword('user');
$this->getUserManager()->updatePassword($this->user);
if (isset($role)) {
$this->user->addRole($role);
}
$this->getUserManager()->add($this->user);
}
}
return $this->user;
}
public function logIn(User $user, Response $response) {
$this->session->start();
$this->cookie = new Cookie('MOCKSESSID', $this->storage->getId());
$this->cookieJar = new CookieJar();
$this->cookieJar->set($this->cookie);
$this->token = new UsernamePasswordToken($user, 'user', 'main', $user->getRoles());
$this->session->set('_security_main', serialize($this->token));
$this->getSecurityManager()->loginUser(
$this->container->getParameter('fos_user.firewall_name'),
$user,
$response
);
$this->session->save();
}
public function removeUser(User $user) {
}
}
<?php
namespace CDE\ContentBundle\Tests\Controller;
use CDE\TestBundle\Base\BaseUserTest;
use Symfony\Component\HttpFoundation\Response;
class RestControllerTest extends BaseUserTest
{
protected $comment;
public function __construct() {
parent::__construct();
$this->logIn($this->getUser('ROLE_ADMIN'), new Response());
}
public function getGalleryManager() {
return $this->container->get('cde_content.manager.gallery');
}
public function getAWSManager() {
return $this->container->get('cde_utility.manager.aws');
}
public function createGallery()
{
// Copy test.jpeg into the web folder
$filename = 'gallery/user-test.jpg';
copy(__DIR__.'/../Mock/test.jpeg', __DIR__.'/../../../../../web/'.$filename);
$this->getAWSManager()->copyGalleryFile($filename);
$gallery = $this->getGalleryManager()->create();
$gallery->setUser($this->getUser());
$gallery->setFilename($filename);
$gallery->setTitle('test gallery');
$gallery->setDescription('test gallery description');
$gallery->setMarked(false);
$this->getGalleryManager()->add($gallery);
$this->assertEquals($gallery->getMarked(), false);
}
public function createComment()
{
$galleries = $this->getGalleryManager()->findByUser($this->getUser());
$gallery = $galleries[0];
$client = static::createClient();
$client->getCookieJar()->set($this->cookie);
// $client = static::createClient(array(), new History(), $cookieJar);
$crawler = $client->request('POST', 'api/createComment/'.$gallery->getId(), array(
'comment' => 'testing testing 123',
'marked' => 'false'
));
$response = $client->getResponse();
$this->comment = json_decode($response->getContent());
$this->assertEquals($this->comment->comment, 'testing testing 123');
$this->assertFalse($this->comment->marked);
$this->assertEquals($response->getStatusCode(), 200);
}
public function getComment()
{
$client = static::createClient();
$crawler = $client->request('GET', 'api/getComment/'.$this->comment->id);
$response = $client->getResponse();
$comment = json_decode($response->getContent());
$this->assertEquals($comment->comment, 'testing testing 123');
$this->assertFalse($comment->marked);
$this->assertEquals($response->getStatusCode(), 200);
}
public function updateComment()
{
$client = static::createClient();
$crawler = $client->request('GET', 'api/updateComment');
}
public function deleteComment()
{
$client = static::createClient();
$crawler = $client->request('DELETE', 'api/deleteComment');
}
public function getComments()
{
$client = static::createClient();
$crawler = $client->request('GET', 'api/getComments');
}
public function getGalleries()
{
$client = static::createClient();
$crawler = $client->request('GET', 'api/getGalleries');
}
public function removeGallery() {
$galleries = $this->getGalleryManager()->findByUser($this->getUser());
foreach ($galleries as $gallery) {
$this->getGalleryManager()->remove($gallery);
}
}
public function testComments() {
$this->createGallery();
$this->createComment();
$this->getComment();
$this->updateComment();
$this->deleteComment();
$this->getComments();
$this->getGalleries();
$this->removeGallery();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment