Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Doctrine orm 具有多种方法的嘲弄条令/存储库_Doctrine Orm_Phpunit_Mockery - Fatal编程技术网

Doctrine orm 具有多种方法的嘲弄条令/存储库

Doctrine orm 具有多种方法的嘲弄条令/存储库,doctrine-orm,phpunit,mockery,Doctrine Orm,Phpunit,Mockery,我正在使用mockry测试一个方法,该方法使用不同的存储库进行了大量的条令存储库调用。 这是我设置所有存储库模拟的方法: public function testService() { $mockDoctrine = $this->getMockDoctrine(); $mockDoctrine->shouldReceive('getRepository')->once() ->andReturn($this->getReposito

我正在使用mockry测试一个方法,该方法使用不同的存储库进行了大量的条令存储库调用。 这是我设置所有存储库模拟的方法:

public function testService()
{
    $mockDoctrine = $this->getMockDoctrine();
    $mockDoctrine->shouldReceive('getRepository')->once()
        ->andReturn($this->getRepositoryAMock());

    $mockDoctrine->shouldReceive('getRepository')->once()
        ->andReturn($this->getRepositoryBMock());

    $mockDoctrine->shouldReceive('getRepository')->once()
        ->andReturn($this->getRepositoryCMock());

    //here is where i hit my test
    $products = $this->service->fire(1, 1);

    $this->assertInstanceOf('Illuminate\Support\Collection', $products);
    foreach ($products as $v) {
        $this->assertInstanceOf('Illuminate\Support\Collection', $v);
    }
}
这就是我嘲笑教义的方法:

public function getMockDoctrine()
{
    $mockDoctrine = \App::make('Doctrine');
    $mockDoctrine->shouldReceive('persist')
        ->andReturn(true);
    $mockDoctrine->shouldReceive('flush')
        ->andReturn(true);

    return $mockDoctrine;
}
这些是我的

public function getRepositoryAMock()
{
    $repository = \Mockery::mock('MyARepository');
    $repository->shouldReceive('findBy')
        ->with(['paramA' => 1, 'paramB' => 1])
        ->andReturn($this->getMockA());

    return $repository;
}

public function getRepositoryBMock()
{
    $repository = \Mockery::mock('MyBRepository');
    $repository->shouldReceive('findById')
        ->with(1)
        ->andReturn($this->getMockA());

    return $repository;
}

public function getRepositoryCMock()
{
    $repository = \Mockery::mock('MyCRepository');
    $repository->shouldReceive('findOneBy')
        ->with(['paramA' => 1, 'paramB' => 1])
        ->andReturn($this->getMockA());

    return $repository;
}
事实上,这就是我设定我的模拟回归的地方

public function getMockA()
{
    $obj = new MyClass();
    $reflection = new \ReflectionClass($obj);
    $id = $reflection->getProperty('id');
    $id->setAccessible(true);
    $id->setValue($obj, 1);
    $obj
        ->setLogin('foo')
        ->setPassword('bar')
        ->setCode(1);

    return $obj;
}
然后我收到这样一个错误:

1) MyClassTest::testService BadMethodCallException:此模拟对象上不存在方法Mockery_2_ClassBRepository::findOneBy()

假设我有3个在testService()方法中调用存储库的方法,Mockry没有找到的方法在第三个方法中,但Mockry认为它在第二个方法中,所以显然他不会找到,因为在第二个方法中,不存在第三个方法中的“findOneBy()”原则方法


我怎样才能解决这个问题

您应该能够使用mockry的with()。
例如:

$mockDoctrine
    ->shouldReceive('getRepository')
    ->with('MyAReposiotry')->once()
    ->andReturn($this->getRepositoryAMock());
对于每个存储库都是如此(在中具有不同的值)

但我宁愿将存储库注入到该服务中,而不是从服务内部的EntityManager获取它。这是更好的测试方式。看看。

多亏了伊万先生

现在我的方法奏效了

在我真正的类中,我使用实体类获取存储库,
比如:

因此,我更改了测试方法,使用“with”传递实体类:

public function testService()
{
    $mockDoctrine = $this->getMockDoctrine();
    $mockDoctrine->shouldReceive('getRepository')
        ->with('MyEntityClassA')->once()
        ->andReturn($this->getRepositoryAMock());

    $mockDoctrine->shouldReceive('getRepository')->once()
        ->with('MyEntityClassB')->once()
        ->andReturn($this->getRepositoryBMock());

    $mockDoctrine->shouldReceive('getRepository')->once()
        ->with('MyEntityClassC')->once()
        ->andReturn($this->getRepositoryCMock());

    //here is where i hit my test
    $products = $this->service->fire(1, 1);

    $this->assertInstanceOf('Illuminate\Support\Collection', $products);
    foreach ($products as $v) {
        $this->assertInstanceOf('Illuminate\Support\Collection', $v);
    }
}

另一个错误是“with”:mockry\Exception\NoMatchingExpectationException:找不到mockry\u 1\u的匹配处理程序。该方法是意外的,或者其参数与此方法的预期参数列表不匹配。是否确实将正确的值与()一起放入?您应该使用相同的值来获取服务中的实体。是的,它是相同的,我是否应该更改“getRepositoryAMock”方法中的某些内容?实际上不是(如果MyARepository是具有正确名称空间的真实类)。但问题是,我得到的存储库是实体名称,而不是存储库名称(->getRepository('Bundle:SomeEntity')->getRepository('SomeRepository'))。这就是为什么问你是否确定你是否得到了这样的存储库。也许你可以粘贴服务本身。哦,是的,它成功了!我也在使用实体来获取存储库,我在代码中改变了这一点,现在它可以工作了!非常感谢伊万先生
public function testService()
{
    $mockDoctrine = $this->getMockDoctrine();
    $mockDoctrine->shouldReceive('getRepository')
        ->with('MyEntityClassA')->once()
        ->andReturn($this->getRepositoryAMock());

    $mockDoctrine->shouldReceive('getRepository')->once()
        ->with('MyEntityClassB')->once()
        ->andReturn($this->getRepositoryBMock());

    $mockDoctrine->shouldReceive('getRepository')->once()
        ->with('MyEntityClassC')->once()
        ->andReturn($this->getRepositoryCMock());

    //here is where i hit my test
    $products = $this->service->fire(1, 1);

    $this->assertInstanceOf('Illuminate\Support\Collection', $products);
    foreach ($products as $v) {
        $this->assertInstanceOf('Illuminate\Support\Collection', $v);
    }
}