(Symfony 3 / Doctrine 2)更新夹具可以保存chagens,但不能在WebTestCase中获取它们

时间:2016-10-12 09:10:26

标签: php doctrine-orm symfony functional-testing fixtures

最近,我开始使用TDD,我正在使用Symfony 3,Doctrine 2,fixture和WebTestCase编写功能测试。昨天,我在测试中发现了一个奇怪的行为:

CONTEXT 这是对RESTfull API的编辑功能的测试,因此我必须记录(使用JWT)才能编辑配置文件。这就是我在setUp函数上所做的。 (JWT-)令牌与我想要更新的更改一起发送到编辑控制器。这就像一个魅力。

问题当我从数据库中获取更新的值并将其与我发送的值进行比较时,问题就出现了。它们应该是相同的,但它们不是......最奇怪的是我正在过滤查询以与我用来比较的值进行比较。这意味着如果数据库中有任何行,这个值必须具有我正在比较的相同值。

我的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)测试\ BackendBundle \ Controller \ UserControllerTest :: testEditAction

     

断言两个字符串相等无效。

     

---预计

     

+++ Actual @@ @@

     

- 'user1@hotmail.com'

     

+ 'email@email.com'

非常感谢!

1 个答案:

答案 0 :(得分:1)

尝试使用实体管理器对象上的refresh方法强制重新加载数据,如下所示:

    **************** 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');

希望这个帮助