在Symfony中坚持更新的对象



我有一个Symfony应用程序,我正在尝试使用Setter在数据库中更新实体。但是,当我更新实体并调用$this->em->flush()时,该实体不持续到数据库。

这是我的服务:

<?php
namespace AppBundleService;
use AppBundleExceptionsUserNotFoundException;
use DoctrineORMEntityManager;
use AppBundleEntityUser;
/**
 * Class MyService
 * @package AppBundleService
 */
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);
    }
}

这是我的测试:

<?php
namespace AppBundleTestsService;
use AppBundleDataFixturesORMPurchaseFixture;
use AppBundleEntityVendor;
use AppBundleRepositoryOfferRepository;
use AppBundleTestsTestCase;
use AppBundleEntityOffer;
use AppBundleDataFixturesORMOfferFixture;
use AppBundleDataFixturesORMPaymentSystemFixture;
/**
 * Class UserOfferServiceTest
 * @package AppBundleTestsService
 */
class MyServiceTest extends TestCase implements ServiceTestInterface
{

    function __construct($name = null, array $data = [], $dataName = '')
    {
        $this->setFixtures([
            'AppBundleDataFixturesORMCityFixture',
            'AppBundleDataFixturesORMCountryFixture',
            'AppBundleDataFixturesORMPaymentSystemFixture',
            'AppBundleDAtaFixturesORMUserFixture',
        ]);
        parent::__construct($name, $data, $dataName);
    }
    /**
     * test the checkEmailExists() of app.vendor
     */
    public function testCheckEmailExists(){
        $myService = $this->getService();

        $this->assertTrue($myService->checkEmailExists('user1@user1.com'));
        $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 AppBundleServiceVendorService|object
     */
    public function getService(){
        $this->seedDatabase();
        $this->client = $this->createClient();
        return $this->client->getContainer()->get('app.admin_kiosk');
    }

}

testAddCredit()测试通过(因为我正在检查相同的对象(,但是testAddCreditByEmail失败了以下错误:1) AppBundleTestsServiceMyServiceTest::testAddCreditByEmail Failed asserting that null matches expected 149.

我尝试坚持实体,冲洗实体(例如: $this->em->flush($user)(无济于事。请让我知道如何解决此问题。

我找到了问题。

在测试案例中,我只需要通过执行$this->getEntityManager()->refresh($user)(在原始$用户实体上(来刷新实体。谢谢!

相关内容

  • 没有找到相关文章

最新更新