Tôi gặp sự cố với Trình quản lý đối tượng trong phpunit.Sự cố với Trình quản lý thực thể và phpunit trong Symfony 2
Đây là thử nghiệm của tôi:
public function testValidChangeEmail()
{
$client = self::createAuthClient('user','password');
$crawler = $client->request('GET', '/user/edit/30');
$crawler = $client->submit($crawler->selectButton('submit')->form(array(
'form[email]' => '[email protected]',
)));
/*
* With this em, this work perfectly
* $em = $client->getContainer()->get('doctrine.orm.entity_manager');
*/
$user = self::$em->getRepository('MyBundle:User')->findUser('[email protected]');
die(var_dump($user->getEmail()));
}
và đây là WebTestCase của tôi kéo dài WebTestCase gốc:
class WebTestCase extends BaseWebTestCase
{
static protected $container;
static protected $em;
static protected function createClient(array $options = array(), array $server = array())
{
$client = parent::createClient($options, $server);
self::$em = $client->getContainer()->get('doctrine.orm.entity_manager');
self::$container = $client->getContainer();
return $client;
}
protected function createAuthClient($user, $pass)
{
return self::createClient(array(), array(
'PHP_AUTH_USER' => $user,
'PHP_AUTH_PW' => $pass,
));
}
Như bạn thấy đấy, tôi thay thế tự :: $ em khi tôi tạo của tôi khách hàng.
Vấn đề của tôi:
Trong thử nghiệm của tôi, các die()
cho tôi email cũ và không phải là email mới ([email protected]
) đã đăng ký trong các thử nghiệm. Tuy nhiên trong cơ sở dữ liệu của tôi, tôi đã lưu [email protected]
chính xác.
Khi tôi truy xuất người dùng của mình trong cơ sở dữ liệu, tôi sử dụng sefl::$em
. Nếu tôi sử dụng các chú thích $em
, tôi sẽ nhận được email mới phù hợp.
Tôi không hiểu tại sao trong WebTestCase của tôi, tôi có thể truy cập vào các Entity Manager mới ...
Side lưu ý: Tại sao trên trái đất làm bạn chết (var_dump()), không phải làm var_dump trở lại bất cứ điều gì hữu ích, cũng không phải bạn nên cần phải chết() ở giữa một bài kiểm tra. – conny