Search code examples
phpsymfonydoctrine-ormdoctrine-odmsymfony-2.8

Persisting Updated Objects in Symfony


I have a symfony application where I am attempting to update an entity in the database using a setter. However when I update the entity and call $this->em->flush() the entity is not persisted to the database.

Here is my service:

<?php

namespace AppBundle\Service;

use AppBundle\Exceptions\UserNotFoundException;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\User;

/**
 * Class MyService
 * @package AppBundle\Service
 */
class MyService extends BaseService {

    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @var User
     */
    protected $user;


    /**
     * MyService constructor.
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em){
        $this->em = $em;
    }

    /**
     * See if a user email exists
     * @param string $email
     * @return bool
     */
    public function checkEmailExists($email){
        $this->user = $this->em
            ->getRepository('AppBundle:User')
            ->findOneBy(['email' => $email]);

        return !(is_null($this->user));
    }

    /**
     * add credit to a users account
     * @param User $user
     * @param integer $credit
     */
    public function addCredit(User $user, $credit){
        $user->addCredit($credit);

        $this->em->flush();
    }

    /**
     * add a credit to a users account
     * @param $email
     * @param $credit
     */
    public function addCreditByEmail($email, $credit){
        if(!($this->checkEmailExists($email))){
            throw new UserNotFoundException(sprintf('User with email %s not found.', $email));
        }
        $this->addCredit($this->user, $credit);
    }
}

Here is my test:

<?php

namespace AppBundle\Tests\Service;

use AppBundle\DataFixtures\ORM\PurchaseFixture;
use AppBundle\Entity\Vendor;
use AppBundle\Repository\OfferRepository;
use AppBundle\Tests\TestCase;
use AppBundle\Entity\Offer;
use AppBundle\DataFixtures\ORM\OfferFixture;
use AppBundle\DataFixtures\ORM\PaymentSystemFixture;

/**
 * Class UserOfferServiceTest
 * @package AppBundle\Tests\Service
 */
class MyServiceTest extends TestCase implements ServiceTestInterface
{


    function __construct($name = null, array $data = [], $dataName = '')
    {
        $this->setFixtures([
            'AppBundle\DataFixtures\ORM\CityFixture',
            'AppBundle\DataFixtures\ORM\CountryFixture',
            'AppBundle\DataFixtures\ORM\PaymentSystemFixture',
            'AppBundle\DAtaFixtures\ORM\UserFixture',

        ]);
        parent::__construct($name, $data, $dataName);
    }

    /**
     * test the checkEmailExists() of app.vendor
     */
    public function testCheckEmailExists(){
        $myService = $this->getService();


        $this->assertTrue($myService->checkEmailExists('[email protected]'));
        $this->assertFalse($myService->checkEmailExists($this->fake()->safeEmail));
    }

    /**
     * test the addCredit functionality
     */
    public function testAddCredit(){
        $myService = $this->getService();

        $user = $this->getUser();
        $this->assertEquals(0, $user->getCredit());

        $toAdd = $this->fake()->numberBetween(1, 500);
        $myService->addCredit($user, $toAdd);

        $this->assertEquals($toAdd, $user->getCredit());
    }

    /**
     * test the addCreditByEmail functionality
     */
    public function testAddCreditByEmail(){
        $myService = $this->getService();

        $user = $this->getUser();
        $email = $this->getUser()->getEmail();

        $this->assertEquals(0, $user->getCredit());

        $toAdd = $this->fake()->numberBetween(1, 500);
        $myService->addCreditByEmail($email, $toAdd);

        $updatedUser =  $this->getEntityManager()
            ->getRepository('AppBundle:User')
            ->findOneBy(['email' => $email]);

        $this->assertEquals($toAdd, $updatedUser->getCredit());
    }


    /**
     * @return \AppBundle\Service\VendorService|object
     */
    public function getService(){
        $this->seedDatabase();
        $this->client = $this->createClient();

        return $this->client->getContainer()->get('app.admin_kiosk');
    }


}

The testAddCredit() test passes (because I'm checking the same object), but the testAddCreditByEmail fails with the following error: 1) AppBundle\Tests\Service\MyServiceTest::testAddCreditByEmail Failed asserting that null matches expected 149.

I've tried persisting the entity, flushing the entity (like: $this->em->flush($user)) all to no avail. Please let me know how I can fix this.


Solution

  • I found the issue.

    In the test case I just had to refresh the entity by doing $this->getEntityManager()->refresh($user) (on the original $user entity). Thanks!