服务覆盖后夹具损坏

Fixture breaking after service override

此测试是 运行 由 Sebastian Bergmann 在 Silverstripe 3.4.0 上使用 PHPUnit 3.7.38

OverrideTest.yml

ExtMember:
  ext_member:
    Email: ext@email.com
    Password: extpassword

OverrideTest.php

class ExtMember extends Member {}

class OverrideTest extends SapphireTest
{
    protected static $fixture_file = 'OverrideTest.yml';

    public function testBrokenFixtures()
    {
        $MockExtMember = $this->getMockBuilder('ExtMember')->getMock();


        $extMember     = $this->objFromFixture("ExtMember", "ext_member");
        Injector::inst()->registerService($MockExtMember, 'ExtMember');
        $extMemberNull = $this->objFromFixture("ExtMember", "ext_member");


        $this->assertEquals("ext@email.com", $extMember->Email);
        $this->assertNull($extMemberNull->Email);

    }
}

$extMemberNull 对象发生了什么事?为什么在我将 DataObject 注册为服务后,所有调用 return 都为空?就可测试性而言,这是一个巨大的障碍

当您访问 DataObject ($extMember->Email) 上的 属性 时,它由魔术方法 __get() 处理并导致调用:

  • $extMember->getEmail();
  • $extMember->getField('Email');

由于您尝试在没有配置的情况下使用模拟对象,因此这些方法 return NULL