dongwei3866 2017-02-03 15:37
浏览 27
已采纳

一次测试后,夹具的参考文献消失了

In my LoadFixture.php, I add reference to all my fixtures like this :

public function load(ObjectManager $manager) {
    $user = new user("Dummy");
    $this->persist($user);
    $this->addReference("user", $user);
}

In my test class I load them like this :

public function setUp() {
        if(self::$do_setup){
            $this->loadFixtures(array(
                "Bundle\\Tests\\Fixtures\\LoadUser"
            )) ;
        }
}

In my tests I use them like this :

public function testOne() {
    $client = $this->createClient($this->getReference("user_a"));
    $client->request('GET', '/');
    $this->assertStatusCode(200, $client);
    self::$do_setup=false;
}

public function testTwo() {
    $client = $this->createClient($this->getReference("user_a"));
    $client->request('GET', '/home');
    $this->assertStatusCode(200, $client);
}

The thing is, technically, I dont need to use setUp() for each test, so I use $do_setup and a if to execute setUp if needed.

But if I dont execute the setUp() in my testTwo, while my fixtures are in my database, $this->getReference("user_a") is giving me an error :

Call to a member function getReferenceRepository() on a non-object

How can I solve that ?

UPDATE
I have found a solution. So I post it here, just in case someone face the same problem as me. Many thanks to @Damien Flament for his answer, regarding the fact that the TestCase is deleted after each test.

I changed the name of my setUp() method to open(), and my tearDown() method to close(). The first method of the class call the open() method, and now return $this. The next method is annoted @depends testOne and take a parameter. With this parameter I can use my references again. Ex :

// new setUp Metod
public function open() {
        if(self::$do_setup){
            $this->loadFixtures(array(
                "Bundle\\Tests\\Fixtures\\LoadUser"
            )) ;
        }
}
//new tearDown method
public function close() {
   $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection()->close();
}
public function testOne() {
    $this->open();
    $client = $this->createClient($this->getReference("user_a"));
    $client->request('GET', '/');
    $this->assertStatusCode(200, $client);
    return $this;
}
/**
 * @depends testOne
 */
public function testTwo($it) {
    $client = $this->createClient($it->getReference("user_a"));
    $client->request('GET', '/home');
    $this->assertStatusCode(200, $client);
    return $it;
}
/**
 * @depends testTwo
 */
public function testThree($it) {
    $client = $this->createClient($it->getReference("user_a"));
    $client->request('GET', '/about');
    $this->assertStatusCode(200, $client);
    $this->close();
}
  • 写回答

1条回答 默认 最新

  • dongzhong5756 2017-02-07 06:10
    关注

    I think the TestCase object is deleted and recreated by PHPUnit (I didn't read the PHPUnit source code, but I think it's the more easy way to reset the testing environment for each test).

    So your object (probably referenced by a test class object attribute) is probably garbage collected.

    To setup fixture once per test class, use the TestCase::setUpBeforeClass() method.

    See documention on "Sharing fixtures".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度