2014-09-03 1 views
2

Я пытаюсь создать макет менеджера сущностей для модульных тестов, который возвращает структуру глубоких сущностей.Преобразование объекта Doctrine в mock для модульного теста

Basicaly Я хочу, чтобы превратить это:

$p1 = new Product(); 
    $p1->setName("product 1"); 
    // ... 

    $c = new Command(); 
    $c->setDate(new Date()); 
    $c->setId(1); 
    $c->addProduct($p1); 
    // ... 

В это:

p1 = $this->getMock('\Acme\DemoBundle\Entity\Product'); 
    $p1->expects($this->any()) 
     ->method('getName') 
     ->will($this->returnValue("product 1")); 
    // ... 

    $c = $this->getMock('\Acme\DemoBundle\Entity\Command'); 
    $c->expects($this->any()) 
     ->method('getDate') 
     ->will($this->returnValue(new Date())); 
    $c->expects($this->any()) 
     ->method('getId') 
     ->will($this->returnValue(1)); 
    $c->expects($this->any()) 
     ->method('getProducts') 
     ->will($this->returnValue(array($p1))); 
    // ... 

Есть простой и не очень многословным способ получить это?

Благодаря

+0

Вы можете использовать насмешку немного проще –

+0

Может быть, вы должны смотреть на [BazingaFakerBundle] (https://github.com/willdurand/BazingaFakerBundle), пакет Symfony2 на основе [Faker] (https://github.com/fzaninotto/Faker). – mneute

ответ

1

Thake взгляд на Phake: http://phake.readthedocs.org/en/latest/index.html

Вы можете Метод заглушек как:

$this->item1 = Phake::mock('Item'); 
    $this->item2 = Phake::mock('Item'); 
    $this->item3 = Phake::mock('Item'); 

    Phake::when($this->item1)->getPrice()->thenReturn(100); 
    Phake::when($this->item2)->getPrice()->thenReturn(200); 
    Phake::when($this->item3)->getPrice()->thenReturn(300); 

    $this->shoppingCart = new ShoppingCart(); 
    $this->shoppingCart->addItem($this->item1); 
    $this->shoppingCart->addItem($this->item2); 
    $this->shoppingCart->addItem($this->item3);