php - Unit test persistence layer - Symfony -


i want test persistence in symfony2. wonder whether better mock entities , provide entity manager or whether better mock entity menager , pass entity manager ?

i first option entity manager throw exception object not entity doctrine. how test persistence symfony in phpunit?

rather writing unit tests should write integration tests persistence layer.

there's rule in unit testing "don't mock don't own".

you don't own doctrine classes nor interfaces, , can never sure assumptions made interfaces mocked true. if they're true @ time write test, cannot sure doctrine's behaviour didn't change on time.

whenever use 3rd party code should write integration test whatever uses it. integration test call database , make sure doctrine works way think works.

that's why it's decouple 3rd party stuff.

in case of doctrine it's easy. 1 of things introduce interface each of repositories.

interface articlerepository {     /**      * @param int $id      *      * @return article      *      * @throws articlenotfoundexception      */     public function findbyid($id); } 

you can mock or stub such interface:

class myservicetest extends \phpunit_framework_test_case {     public function test_it_does_something()     {         $article = new article();         $repository = $this->prophesize(articlerepository::class);         $repository->findbyid(13)->willreturn($article);          $myservice = new myservice($repository->reveal());         $myservice->dosomething();          // ...     } } 

the interface implemented doctrine:

use doctrine\orm\entityrepository;  class doctrinearticlerepository extends entityrepository implement articlerepository {     public function findbyid($id)     {         // call doctrine here     } } 

the implementation you're gonna write integration tests for. in integration test repository you'll call database:

use symfony\bundle\frameworkbundle\test\kerneltestcase;  class articletest extends kerneltestcase {     private $em;     private $repository;      protected function setup()     {         self::bootkernel();          $this->em = static::$kernel->getcontainer()             ->get('doctrine')             ->getmanager();         $this->repository = $this->em->getrepository(article::class);     }      public function test_it_finds_an_article()     {          $this->createarticle(13);           $article = $this->repository->findbyid(13);           $this->assertinstanceof(article::class, $article);          $this->assertsame(13, $article->getid());     }      /**      * @expectedexception articlenotfoundexception      */     public function test_it_throws_an_exception_if_article_is_not_found()     {         $this->repository->findbyid(42);     }      protected function teardown()     {         parent::teardown();          $this->em->close();     }      private function createarticle($id)     {         $article = new article($id);         $this->em->persist($article);         $this->em->flush();         // clear important, otherwise might objects stored doctrine in memory         $this->em->clear();     } } 

everywhere else, you'll use interface stubbed (or mocked). everywhere else won't hit database. makes tests fast.

entities in stubs can created or stubbed. of time create entity objects rather mock them.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -