找不到PHPUnit_Framework_MockObject_Generator

时间:2017-07-11 06:19:47

标签: phpunit

我正在从PHPUnit 4.5迁移,测试运行并且传递正常。

在PHPUnit 4.5中,我们使用了( { "mall_id" = M0550; "mall_name" = "Amrita Shopping Complex"; }, { "mall_id" = M0509; "mall_name" = "Ashoka Market"; }, { "mall_id" = M0943; "mall_name" = "Biju Pattnaik Commercial Complex"; }, { "mall_id" = M0457; "mall_name" = "BMC Bhawani Mall"; }, { "mall_id" = M0460; "mall_name" = "BMC Keshari Mall"; }, { "mall_id" = M0571; "mall_name" = "BMC Market Complex"; }, { "mall_id" = M0453; "mall_name" = "Forum Mart"; }, { "mall_id" = M0609; "mall_name" = "Indradhanu Market"; }, { "mall_id" = M0558; "mall_name" = "Kalyani Plaza Market Complex"; }, { "mall_id" = M0463; "mall_name" = "Maa Barabhuja Mall"; }, { "mall_id" = M0553; "mall_name" = "Mahaveer Complex"; }, { "mall_id" = M0570; "mall_name" = "Market Building"; }, { "mall_id" = M0452; "mall_name" = "Pal Heights Mall"; }, { "mall_id" = M0466; "mall_name" = "Priyadarshini Market Complex"; }, { "mall_id" = M0677; "mall_name" = "Ruchika Market"; }, { "mall_id" = M0504; "mall_name" = "Shubham Market Complex"; }, { "mall_id" = M0564; "mall_name" = "Subhadra Complex"; }, { "mall_id" = M0559; "mall_name" = "Sultania Shopping Complex"; }, { "mall_id" = M0552; "mall_name" = "Tathastu Complex"; }, { "mall_id" = M0568; "mall_name" = "Western Tower Market Building"; } ) ,现在显然不存在于PHPUnit 6中。

我们正在使用它:

PHPUnit_Framework_MockObject_Generator

我怎样才能重写这个不给出类找不到的错误?我不知道PHPUnit 6相当于public static function getMock() { $generator = new PHPUnit_Framework_MockObject_Generator(); $mock = $generator->generate( 'ezcDbHandlerMysql', array(), '', true, true ); if (class_exists($mock['mockClassName'], false) === false) { eval($mock['code']); } $args = array( 'host' => TEST_DB_HOST, 'database' => TEST_DB_NAME, 'username' => TEST_DB_USER, 'password' => TEST_DB_PASS, 'driver-opts' => array(PDO::ATTR_PERSISTENT => TEST_DB_PERSISTENCE), ); $class = new ReflectionClass($mock['mockClassName']); $object = $class->newInstanceArgs(array($args)); return $object; } 是什么。

1 个答案:

答案 0 :(得分:2)

你有2个选项,你仍然可以通过使用getMockClass()中使用的命名空间MockObjectGenerator切换它来偷偷使用你的旧代码。如果您关注the manual,则可以使用createMockgetMockBuilder()并以编程方式设置模拟。我发现后者提供了更多的便利,因为我不必将一堆参数设置为它们的默认值以获得我实际关心的第n个参数,例如当试图禁用原始构造函数调用时。因此升级到getMockBUilder可能是您的不错选择。

因为你必须更新你的模拟用法,你可能还想要切换你正在使用的模拟库。 PHPUnit与Prophecy捆绑在一起,我最喜欢大多数用例。它是一个opionated库,所以做某些事情比使用PHPUnit_Mock库更难,但我通常使用它作为重构这些案例的代码的动力。还有其他模拟库,例如你可能想要查看的Mockery