2017-04-25 54 views
1

我試圖模擬EntityRepositoryfind方法,以便測試不查找數據庫中的數據,但似乎不起作用。下面是測試類的setUp方法如何在Symfony 3中模擬「查找」方法

public function setUp() 
{ 
    parent::setUp(); 

    $this->client = static::createClient(); 
    $this->peopleManager = $this->getMockBuilder(PeopleManager::class) 
     ->setMethods(['createPerson','peopleUpdate', 'peopleDelete', 'peopleRead']) 
     ->disableOriginalConstructor() 
     ->getMock(); 

    $this->repository = $this->getMockBuilder(EntityRepository::class) 
     ->disableOriginalConstructor() 
     ->getMock(); 

    $this->em = $this->getMockBuilder(EntityManager::class) 
     ->disableOriginalConstructor() 
     ->getMock(); 
} 

這就是我們所說的查找功能的方法

public function updatePersonAction($id, Request $request) 
{ 
    $repository = $this->getDoctrine()->getRepository('GeneralBundle:People'); 
    $person= $repository->find($id); 
    if($person) 
    { 
     $data = $request->request->get('array'); 
     $createdPeople = array(); 
     $UpdatedPerson = ""; 
     foreach($data as $content) 
     { 
      $prueba = $this->get('people.manager'); 
      $UpdatedPerson = $prueba->peopleUpdate(
       $person, 
       $content['name'], 
       $content['surname'], 
       $content['secondSurname'], 
       $content['nationality'], 
       $content['birthday'], 
       $content['identityCard'], 
       $content['identityCardType'] 
      ); 
      array_push($createdPeople, $person); 
     } 
     $serializedEntity = $this->get('serializer')->serialize($UpdatedPerson, 'json'); 
     return new Response($serializedEntity); 
    } else { 
     $serializedEntity = $this->get('serializer')->serialize('Doesn\'t exists any person with this id', 'json'); 
     return new Response($serializedEntity); 
    } 
} 

調試表明peoplemanager類嘲笑,但它並沒有嘲笑實體管理器和存儲庫。

謝謝< 3.

+1

的嘲笑它相當瑣碎http://docs.mockery.io/en/latest/試試看像'$ repo-> shouldReceive( '發現') - >一次() - >和返回([1,2,3]);' – Confidence

+0

謝謝,這是非常有用的! –

回答

4

假設你要測試的類看起來是這樣的:

// src/AppBundle/Salary/SalaryCalculator.php 
namespace AppBundle\Salary; 

use Doctrine\Common\Persistence\ObjectManager; 

class SalaryCalculator 
{ 
    private $entityManager; 

    public function __construct(ObjectManager $entityManager) 
    { 
     $this->entityManager = $entityManager; 
    } 

    public function calculateTotalSalary($id) 
    { 
     $employeeRepository = $this->entityManager 
      ->getRepository('AppBundle:Employee'); 
     $employee = $employeeRepository->find($id); 

     return $employee->getSalary() + $employee->getBonus(); 
    } 
} 

由於的ObjectManager被注入到通過構造函數的類,可以很容易地通過一個模擬對象在一個測試中:

// tests/AppBundle/Salary/SalaryCalculatorTest.php 
namespace Tests\AppBundle\Salary; 

use AppBundle\Entity\Employee; 
use AppBundle\Salary\SalaryCalculator; 
use Doctrine\ORM\EntityRepository; 
use Doctrine\Common\Persistence\ObjectManager; 
use PHPUnit\Framework\TestCase; 

class SalaryCalculatorTest extends TestCase 
{ 
    public function testCalculateTotalSalary() 
    { 
     // First, mock the object to be used in the test 
     $employee = $this->createMock(Employee::class); 
     $employee->expects($this->once()) 
      ->method('getSalary') 
      ->will($this->returnValue(1000)); 
     $employee->expects($this->once()) 
      ->method('getBonus') 
      ->will($this->returnValue(1100)); 

     // Now, mock the repository so it returns the mock of the employee 
     $employeeRepository = $this 
      ->getMockBuilder(EntityRepository::class) 
      ->disableOriginalConstructor() 
      ->getMock(); 
     $employeeRepository->expects($this->once()) 
      ->method('find') 
      ->will($this->returnValue($employee)); 

     // Last, mock the EntityManager to return the mock of the repository 
     $entityManager = $this 
      ->getMockBuilder(ObjectManager::class) 
      ->disableOriginalConstructor() 
      ->getMock(); 
     $entityManager->expects($this->once()) 
      ->method('getRepository') 
      ->will($this->returnValue($employeeRepository)); 

     $salaryCalculator = new SalaryCalculator($entityManager); 
     $this->assertEquals(2100, $salaryCalculator->calculateTotalSalary(1)); 
    } 
} 

在這個例子中,你從內向外構建模擬,首先創建被返回的僱員Repository,它本身由EntityManager返回。這樣,沒有真正的課程參與測試。

來源: http://symfony.com/doc/current/testing/database.html

+0

謝謝兄弟! –