StofDoctrineExtensionsBundle icon indicating copy to clipboard operation
StofDoctrineExtensionsBundle copied to clipboard

[Blameable] Not working in Unit Tests

Open m-radzikowski opened this issue 11 years ago • 3 comments

Blameable doesn't detects user in Unit Tests too. My code:

public function testAddEntity() {
    $client = static::createClient();
    $client->request('GET', '/admin/');
    $container = $client->getContainer();
    $doctrine = $container->get('doctrine');

    $user = $this->loadUser($doctrine, $username); // returns User Entity object

    $request  = $client->getRequest();

    $token = new UsernamePasswordToken($user, $user->getPassword(), "main", $user->getRoles());
    $container->get("security.context")->setToken($token);

    $event = new InteractiveLoginEvent($request, $token);
    $container->get("event_dispatcher")->dispatch("security.interactive_login", $event);

    $this->assertTrue( $client->getContainer()->get('security.context')->isGranted('ROLE_ADMIN') ); // correct, user is logged

    $newEntity = new Post;
    $this->em->persist($newEntity);
    $this->em->flush(); // error: Integrity constraint violation: 1048 Column 'created_by' cannot be null
}

I've tried this method of authentication (above) and standard HTTP authentication, but there is always MySQL error when I try to flush.

m-radzikowski avatar Sep 07 '13 10:09 m-radzikowski

I have similar problem when I try to submit form in my test. My code for reference:

public function testAdminUsersCreate()
    {
        $crawler = $this->client->request('GET', '/admin/users/create');

        $form = $crawler->selectButton('Save')->form();
        $crawler = $this->client->submit($form, array('user_create[email]' => '[email protected]', 'user_create[username]' => 'test', 'user_create[password][first]' => 'testtest', 'user_create[password][second]' => 'testtest')); //returns error in a Reponse

        $this->assertEquals(5, $crawler->filter('tr')->count());
    }

    /**
     * {@inheritDoc}
     */
    protected function setUp()
    {
        $this->client = static::createClient();

        $this->client->insulate();

        $this->logIn();
    }

    private function logIn()
    {
        $session = $this->client->getContainer()->get('session');

        $firewall = 'main';
        $token = new UsernamePasswordToken('admin', 'admin', $firewall, array('ROLE_SUPER_ADMIN'));
        $session->set('_security_' . $firewall, serialize($token));
        $session->save();

        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }

The error message I get in a Reponse:

Blame is reference, user must be an object (500 Internal Server Error)

jacek-foremski avatar Apr 09 '16 09:04 jacek-foremski

Not sure if this is related, but I'm running my API tests using codeception using the REST module, and for some reason, only during my tests, the created/updatedBy is not being set.

I've tracked it down to the fact that the instance of \Gedmo\Blameable\BlameableListener that the \Stof\DoctrineExtensionsBundle\EventListener\BlameListener is setting the user is somehow different to the instance used during the Doctrine lifecycle events (different spl object id).

I don't have time to track this down right now, as on a deadline, but will update with more info when I do.

toby-griffiths avatar Feb 15 '19 08:02 toby-griffiths

@toby-griffiths any leads on this issue? would appreciate the help been stuck for long while.

alialaaICS avatar Oct 08 '23 21:10 alialaaICS