【发布时间】:2017-04-17 17:24:02
【问题描述】:
我有一个 symfony 应用程序,我正在尝试使用 setter 更新数据库中的实体。但是,当我更新实体并调用 $this->em->flush() 时,实体不会持久化到数据库中。
这是我的服务:
<?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);
}
}
这是我的测试:
<?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('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 \AppBundle\Service\VendorService|object
*/
public function getService(){
$this->seedDatabase();
$this->client = $this->createClient();
return $this->client->getContainer()->get('app.admin_kiosk');
}
}
testAddCredit() 测试通过(因为我检查的是同一个对象),但 testAddCreditByEmail 失败并出现以下错误:1) AppBundle\Tests\Service\MyServiceTest::testAddCreditByEmail
Failed asserting that null matches expected 149.
我尝试过持久化实体,刷新实体(如:$this->em->flush($user))都无济于事。请让我知道如何解决此问题。
【问题讨论】:
-
您的 PHPUnit 测试是否针对真实数据库运行?问题是否也出现在“实际使用”中,或者可能是您在测试期间如何使用 Doctrine 的一个方面?
-
是的,他们正在对一个 sqlite 数据库 @Darien 进行操作。我不这么认为。
-
我首先要验证
$this->getUser()应该始终为您提供与假定等效的findOneBy(...)调用相同的预先存在的用户实体。如果$this->getUser()正在创建一个新的,请确保在刷新之前进行->persist()调用。如果多行包含相同的电子邮件,findOneBy()可能会返回“错误”的电子邮件。 -
你在打电话给
$this->em->flush()之前是打电话给$this->em->persist($this->user);吗? -
我同意@Confidence...你必须坚持然后刷新。
标签: php symfony doctrine-orm doctrine-odm symfony-2.8