1+ <?php 
2+ 
3+ namespace  DHolmes \DoctrineExtras \ORM ;
4+ 
5+ use  PHPUnit_Framework_TestCase ;
6+ use  InvalidArgumentException ;
7+ use  Doctrine \Common \DataFixtures \FixtureInterface ;
8+ use  Doctrine \Common \Persistence \ObjectManager ;
9+ use  Doctrine \ORM \EntityManager ;
10+ use  DHolmes \DoctrineExtras \ORM \OperationsHelper ;
11+ 
12+ abstract  class  DoctrineORMTestCase extends  PHPUnit_Framework_TestCase implements  FixtureInterface
13+ {
14+  /** @inheritDoc */ 
15+  public  function  load (ObjectManager $ manager
16+  {
17+  $ entities$ this getTestEntities ();
18+  $ allEntities$ this extractAllEntitiesToPersist ($ entities
19+  array_walk ($ allEntitiesarray ($ manager'persist ' ));
20+  
21+  $ managerflush ();
22+  }
23+  
24+  /** 
25+  * This is added to help test readability. The idea is that this method would expand all entity 
26+  * relations that would otherwise need to be persisted explicitly. e.g. 
27+  *  
28+  * protected function getTestEntities() 
29+  * { 
30+  * return array(new Person(new Country('Australia'))); 
31+  * } 
32+  * 
33+  * protected function extractAllEntitiesToPersist(array $entities) 
34+  * { 
35+  * $all = array(); 
36+  * foreach ($entities as $person) 
37+  * { 
38+  * $all[] = $person; 
39+  * $all[] = $person->getCountry(); 
40+  * } 
41+  * return $all; 
42+  * } 
43+  * 
44+  * extractAllEntitiesToPersist would usually go in a data source shared abstract test case 
45+  *  
46+  * @param array $entities 
47+  * @return array  
48+  */ 
49+  protected  function  extractAllEntitiesToPersist (array  $ entities
50+  {
51+  return  $ entities
52+  }
53+  
54+  /** @return array */ 
55+  abstract  protected  function  getTestEntities ();
56+  
57+  /** @var OperationsHelper */ 
58+  private  $ operationsHelper
59+  
60+  /** @return OperationsHelper */ 
61+  private  function  getOperationsHelper ()
62+  {
63+  if  ($ this operationsHelper  === null )
64+  {
65+  $ this operationsHelper  = $ this createOperationsHelper ();
66+  } 
67+  return  $ this operationsHelper ;
68+  }
69+  
70+  /** @return OperationsHelper */ 
71+  protected  function  createOperationsHelper ()
72+  {
73+  $ helpernew  OperationsHelper ();
74+  $ helpersetIsSchemaCacheEnabled (true );
75+  // Cannot really compare entities properly with this enabled because actual entity not  
76+  // inserted in session. Might be possible if compare contents (excluding id and timestamps?) 
77+  // but that could be innacurate 
78+  $ helpersetIsFixturesCacheEnabled (false );
79+  return  $ helper
80+  }
81+  
82+  /** @return EntityManager */ 
83+  abstract  protected  function  getEntityManager ();
84+  
85+  protected  function  setUpDatabase ()
86+  {
87+  $ entityManager$ this getEntityManager ();
88+  $ this getOperationsHelper ()->setUpDatabase ($ entityManagerarray ($ this 
89+  }
90+  
91+  /** 
92+  * @param object $entity 
93+  * @return object  
94+  */ 
95+  protected  function  ensureEntityManaged ($ entity
96+  {
97+  $ em$ this getEntityManager ();
98+  $ merged$ emmerge ($ entity
99+  if  ($ emgetUnitOfWork ()->isScheduledForInsert ($ merged
100+  {
101+  $ desc$ this getEntityDescription ($ entity
102+  throw  new  InvalidArgumentException (sprintf ('Entity %s not within manager ' , $ desc
103+  }
104+  return  $ merged
105+  }
106+  
107+  /** 
108+  * @param object $expectedEntity 
109+  * @param object $entity  
110+  */ 
111+  protected  function  assertSameEntities ($ expectedEntity$ entity
112+  {
113+  $ entityManager$ this getEntityManager ();
114+  
115+  if  ($ expectedEntitynull )
116+  {
117+  $ expectedEntity$ entityManagermerge ($ expectedEntity
118+  }
119+  if  ($ entitynull )
120+  {
121+  $ entity$ entityManagermerge ($ entity
122+  }
123+  
124+  $ expectedDesc$ this getEntityDescription ($ expectedEntity
125+  $ entityDesc$ this getEntityDescription ($ entity
126+  
127+  $ assertMessagesprintf ('Entities not the same: '  . "\n"  . ' %s '  . "\n"  . ' %s ' , 
128+  $ expectedDesc$ entityDesc
129+  $ this assertSame ($ expectedEntity$ entity$ assertMessage
130+  }
131+  
132+  /** 
133+  * @param object $entity 
134+  * @return string 
135+  */ 
136+  private  function  getEntityDescription ($ entity
137+  {
138+  $ entityString'NULL ' ;
139+  if  (method_exists ($ entity'__toString ' ))
140+  {
141+  $ entityStringsprintf ('%s <%s> ' , (string )$ entityget_class ($ entity
142+  }
143+  else  if  ($ entitynull )
144+  {
145+  $ entityStringsprintf ('%s <%s> ' , spl_object_hash ($ entityget_class ($ entity
146+  }
147+  return  $ entityString
148+  }
149+  
150+  /** 
151+  * @param array $expected 
152+  * @param array $actual  
153+  */ 
154+  protected  function  assertEntityCollectionEquals (array  $ expectedarray  $ actual
155+  {
156+  $ entityManager$ this getEntityManager ();
157+  $ expectedarray_map (function ($ entityuse  ($ entityManager
158+  {
159+  return  $ entityManagermerge ($ entity
160+  }, $ expected
161+  
162+  $ actualarray_map (function ($ entityuse  ($ entityManager
163+  {
164+  return  $ entityManagermerge ($ entity
165+  }, $ actual
166+  
167+  $ this assertEquals ($ expected$ actual
168+ 
169+  // Cannot simply compare because comparison goes in to some infinite loop 
170+  /*$idMap = function($entity) 
171+  { 
172+  return $entity->getId(); 
173+  }; 
174+  $expectedIds = array_map($idMap, $expected); 
175+  $actualIds = array_map($idMap, $actual); 
176+  sort($expectedIds); 
177+  sort($actualIds); 
178+   
179+  $this->assertEquals($expectedIds, $actualIds);*/ 
180+  }
181+  
182+  protected  function  tearDownDatabase ()
183+  {
184+  $ entityManager$ this getEntityManager ();
185+  $ this getOperationsHelper ()->tearDownDatabase ($ entityManager
186+  }
187+ }
0 commit comments