(Symfony 3 / Doctrine 2) fixtures 的更新保存了 chagens 但没有在 WebTestCase 中得到它们
(Symfony 3 / Doctrine 2 ) update of fixtures saves chagens but doesn't get them in WebTestCase
最近,我开始使用 TDD,我正在使用 Symfony 3、Doctrine 2、fixtures 和 WebTestCase 编写功能测试。昨天,我在测试中发现了一个奇怪的行为:
CONTEXT 这是对 RESTfull API 编辑功能的测试,因此我必须登录(使用 JWT)才能编辑配置文件。这就是我在 setUp 函数上所做的。 (JWT-)Token 连同我要更新的更改一起发送到编辑控制器。这就像一个魅力。
问题 当我从数据库中获取更新的值并将它们与我发送的值进行比较时,问题就出现了。它们应该是相同的,但它们不是......最奇怪的是我正在过滤要与我用来比较的值进行比较的查询。这意味着如果数据库中有任何行具有此值,则必须具有我正在比较的相同值。
我的LoadUserData.php
namespace BackendBundle\DataFixtures\ORM;
use BackendBundle\Entity\User;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
class LoadUserData implements FixtureInterface
{
static public $users = array();
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$user = new User();
$user->setUsername('User1');
$user->setEmail('user1@hotmail.com');
$user->setPassword('user1');
$user->setDateValidFrom(new \DateTime('2016-10-11 12:22:25'));
$user->setProfilePicture('test.jpg');
$user->setDateValidTo(new \DateTime('2016-10-11 12:22:25'));
$user->setDateLastEdited(new \DateTime('2016-10-11 12:22:25'));
$user->setUsergroup(null);
$user2 = new User();
$user2->setUsername('User2');
$user2->setEmail('user2@hotmail.com');
$user2->setPassword('user2');
$user2->setDateValidFrom(new \DateTime('2016-10-11 12:22:25'));
$user2->setProfilePicture('test.jpg');
$user2->setDateValidTo(new \DateTime('2016-10-11 12:22:25'));
$user2->setDateLastEdited(new \DateTime('2016-10-11 12:22:25'));
$user2->setUsergroup(null);
$manager->persist($user);
$manager->persist($user2);
$manager->flush();
self::$users = array($user, $user2);
}
}
我的UserControllerTest.php
namespace tests\BackendBundle\Controller;
use BackendBundle\DataFixtures\ORM\LoadUserData;
use BackendBundle\Entity\User;
use Liip\FunctionalTestBundle\Test\WebTestCase;
class UserControllerTest extends WebTestCase
{
private $jwtService;
private $jwt;
private $userRep;
private $currentUser;
public function setUp(){
$this->jwtService = $this->getContainer()->get('app.jwt_auth');
$this->userRep = $this->getContainer()->get('doctrine')->getManager()
->getRepository('BackendBundle:User');
$fixtures = array('BackendBundle\DataFixtures\ORM\LoadUserData');
$this->loadFixtures($fixtures);
$users = LoadUserData::$users;
/** @var User $user */
$user = $users[0];
$this->currentUser = $user;
************* THE USER HAVE TO BE LOGGED TO CAN EDIT HIS PROFILE ******************
$this->jwt = $this->jwtService->signup(
$user->getEmail(),
$user->getPassword(),
true
);
$this->client = static::createClient();
}
public function testEditAction() {
$route = $this->getUrl(
'edit',
array(
'authorization' => $this->jwt,
'json' => '{"email":"email@email.com","username":"admin","password":"admin"}',
'_format' => 'json'
)
);
$this->client->request('POST', $route, array('ACCEPT' => 'application/json'));
$response = $this->client->getResponse();
**************** GET THE USER WITH THE NEW EMAIL ****************
/** @var User $updatedUser */
$updatedUser = $this->userRep->findOneBy(
array('email' => 'email@email.com')
);
************* COMPARE NEW EMAIL WITH THE SENT ONE ***********
$this->assertEquals($updatedUser->getEmail(), 'email@email.com');
}
}
测试结果:
1) tests\BackendBundle\Controller\UserControllerTest::testEditAction
Failed asserting that two strings are equal.
--- Expected
+++ Actual @@ @@
-'user1@hotmail.com'
+'email@email.com'
非常感谢!
尝试使用实体管理器对象上的刷新方法强制重新加载数据,如下所示:
**************** GET THE USER WITH THE NEW EMAIL ****************
/** @var User $updatedUser */
$updatedUser = $this->userRep->findOneBy(
array('email' => 'email@email.com')
);
$em = $this->getContainer()->get('doctrine')->getManager();
$em->refresh($updatedUser);
************* COMPARE NEW EMAIL WITH THE SENT ONE ***********
$this->assertEquals($updatedUser->getEmail(), 'email@email.com');
希望对您有所帮助
最近,我开始使用 TDD,我正在使用 Symfony 3、Doctrine 2、fixtures 和 WebTestCase 编写功能测试。昨天,我在测试中发现了一个奇怪的行为:
CONTEXT 这是对 RESTfull API 编辑功能的测试,因此我必须登录(使用 JWT)才能编辑配置文件。这就是我在 setUp 函数上所做的。 (JWT-)Token 连同我要更新的更改一起发送到编辑控制器。这就像一个魅力。
问题 当我从数据库中获取更新的值并将它们与我发送的值进行比较时,问题就出现了。它们应该是相同的,但它们不是......最奇怪的是我正在过滤要与我用来比较的值进行比较的查询。这意味着如果数据库中有任何行具有此值,则必须具有我正在比较的相同值。
我的LoadUserData.php
namespace BackendBundle\DataFixtures\ORM;
use BackendBundle\Entity\User;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
class LoadUserData implements FixtureInterface
{
static public $users = array();
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$user = new User();
$user->setUsername('User1');
$user->setEmail('user1@hotmail.com');
$user->setPassword('user1');
$user->setDateValidFrom(new \DateTime('2016-10-11 12:22:25'));
$user->setProfilePicture('test.jpg');
$user->setDateValidTo(new \DateTime('2016-10-11 12:22:25'));
$user->setDateLastEdited(new \DateTime('2016-10-11 12:22:25'));
$user->setUsergroup(null);
$user2 = new User();
$user2->setUsername('User2');
$user2->setEmail('user2@hotmail.com');
$user2->setPassword('user2');
$user2->setDateValidFrom(new \DateTime('2016-10-11 12:22:25'));
$user2->setProfilePicture('test.jpg');
$user2->setDateValidTo(new \DateTime('2016-10-11 12:22:25'));
$user2->setDateLastEdited(new \DateTime('2016-10-11 12:22:25'));
$user2->setUsergroup(null);
$manager->persist($user);
$manager->persist($user2);
$manager->flush();
self::$users = array($user, $user2);
}
}
我的UserControllerTest.php
namespace tests\BackendBundle\Controller;
use BackendBundle\DataFixtures\ORM\LoadUserData;
use BackendBundle\Entity\User;
use Liip\FunctionalTestBundle\Test\WebTestCase;
class UserControllerTest extends WebTestCase
{
private $jwtService;
private $jwt;
private $userRep;
private $currentUser;
public function setUp(){
$this->jwtService = $this->getContainer()->get('app.jwt_auth');
$this->userRep = $this->getContainer()->get('doctrine')->getManager()
->getRepository('BackendBundle:User');
$fixtures = array('BackendBundle\DataFixtures\ORM\LoadUserData');
$this->loadFixtures($fixtures);
$users = LoadUserData::$users;
/** @var User $user */
$user = $users[0];
$this->currentUser = $user;
************* THE USER HAVE TO BE LOGGED TO CAN EDIT HIS PROFILE ******************
$this->jwt = $this->jwtService->signup(
$user->getEmail(),
$user->getPassword(),
true
);
$this->client = static::createClient();
}
public function testEditAction() {
$route = $this->getUrl(
'edit',
array(
'authorization' => $this->jwt,
'json' => '{"email":"email@email.com","username":"admin","password":"admin"}',
'_format' => 'json'
)
);
$this->client->request('POST', $route, array('ACCEPT' => 'application/json'));
$response = $this->client->getResponse();
**************** GET THE USER WITH THE NEW EMAIL ****************
/** @var User $updatedUser */
$updatedUser = $this->userRep->findOneBy(
array('email' => 'email@email.com')
);
************* COMPARE NEW EMAIL WITH THE SENT ONE ***********
$this->assertEquals($updatedUser->getEmail(), 'email@email.com');
}
}
测试结果:
1) tests\BackendBundle\Controller\UserControllerTest::testEditAction
Failed asserting that two strings are equal.
--- Expected
+++ Actual @@ @@
-'user1@hotmail.com'
+'email@email.com'
非常感谢!
尝试使用实体管理器对象上的刷新方法强制重新加载数据,如下所示:
**************** GET THE USER WITH THE NEW EMAIL ****************
/** @var User $updatedUser */
$updatedUser = $this->userRep->findOneBy(
array('email' => 'email@email.com')
);
$em = $this->getContainer()->get('doctrine')->getManager();
$em->refresh($updatedUser);
************* COMPARE NEW EMAIL WITH THE SENT ONE ***********
$this->assertEquals($updatedUser->getEmail(), 'email@email.com');
希望对您有所帮助