PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/php/net/xp_framework/unittest/rdbms/FinderTest.class.php

https://github.com/treuter/xp-framework
PHP | 465 lines | 271 code | 32 blank | 162 comment | 0 complexity | 69a79a7cceaa68f20b2a0ce414fed89a MD5 | raw file
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses(
  7. 'unittest.TestCase',
  8. 'rdbms.DriverManager',
  9. 'rdbms.finder.GenericFinder',
  10. 'net.xp_framework.unittest.rdbms.mock.MockConnection',
  11. 'net.xp_framework.unittest.rdbms.dataset.JobFinder'
  12. );
  13. /**
  14. * TestCase
  15. *
  16. * @see xp://rdbms.finder.Finder
  17. * @purpose Unit test
  18. */
  19. class FinderTest extends TestCase {
  20. const MOCK_CONNECTION_CLASS = 'net.xp_framework.unittest.rdbms.mock.MockConnection';
  21. protected $fixture = NULL;
  22. /**
  23. * Mock connection registration
  24. *
  25. */
  26. #[@beforeClass]
  27. public static function registerMockConnection() {
  28. DriverManager::register('mock', XPClass::forName(self::MOCK_CONNECTION_CLASS));
  29. }
  30. /**
  31. * Setup method
  32. *
  33. */
  34. public function setUp() {
  35. $this->fixture= new JobFinder();
  36. $this->fixture->getPeer()->setConnection(DriverManager::getConnection('mock://mock/JOBS?autoconnect=1'));
  37. }
  38. /**
  39. * Helper method which invokes the finder's method() method and un-wraps
  40. * exceptions thrown.
  41. *
  42. * @param string name
  43. * @return rdbms.finder.FinderMethod
  44. * @throws lang.Throwable
  45. */
  46. protected function method($name) {
  47. try {
  48. return $this->fixture->method($name);
  49. } catch (FinderException $e) {
  50. throw $e->getCause();
  51. }
  52. }
  53. /**
  54. * Helper methods
  55. *
  56. * @return net.xp_framework.unittest.rdbms.mock.MockConnection
  57. */
  58. protected function getConnection() {
  59. return $this->fixture->getPeer()->getConnection();
  60. }
  61. /**
  62. * Tests the getPeer() method
  63. *
  64. */
  65. #[@test]
  66. public function peerObject() {
  67. $this->assertClass($this->fixture->getPeer(), 'rdbms.Peer');
  68. }
  69. /**
  70. * Tests the getPeer() method returns the same Peer instance that
  71. * Job::getPeer() returns.
  72. *
  73. */
  74. #[@test]
  75. public function jobPeer() {
  76. $this->assertEquals($this->fixture->getPeer(), Job::getPeer());
  77. }
  78. /**
  79. * Tests the entityMethods() method
  80. *
  81. */
  82. #[@test]
  83. public function entityMethods() {
  84. $methods= $this->fixture->entityMethods();
  85. $this->assertEquals(1, sizeof($methods));
  86. $this->assertClass($methods[0], 'rdbms.finder.FinderMethod');
  87. $this->assertEquals(ENTITY, $methods[0]->getKind());
  88. $this->assertEquals('byPrimary', $methods[0]->getName());
  89. $this->assertSubClass($methods[0]->invoke(array($pk= 1)), 'rdbms.SQLExpression');
  90. }
  91. /**
  92. * Tests the collectionMethods() method
  93. *
  94. */
  95. #[@test]
  96. public function collectionMethods() {
  97. static $invocation= array(
  98. 'all' => array(),
  99. 'newestJobs' => array(),
  100. 'expiredJobs' => array(),
  101. 'similarTo' => array('Test')
  102. );
  103. $methods= $this->fixture->collectionMethods();
  104. $this->assertEquals(4, sizeof($methods)); // three declared plu all()
  105. foreach ($methods as $method) {
  106. $this->assertClass($method, 'rdbms.finder.FinderMethod');
  107. $name= $method->getName();
  108. $this->assertEquals(COLLECTION, $method->getKind(), $name);
  109. $this->assertEquals(TRUE, isset($invocation[$name]), $name);
  110. $this->assertSubClass($method->invoke($invocation[$name]), 'rdbms.SQLExpression', $name);
  111. }
  112. }
  113. /**
  114. * Tests the allMethods() method
  115. *
  116. */
  117. #[@test]
  118. public function allMethods() {
  119. $methods= $this->fixture->allMethods(); // four declared plu all()
  120. $this->assertEquals(5, sizeof($methods));
  121. }
  122. /**
  123. * Tests the method() method
  124. *
  125. */
  126. #[@test]
  127. public function byPrimaryMethod() {
  128. $method= $this->fixture->method('byPrimary');
  129. $this->assertClass($method, 'rdbms.finder.FinderMethod');
  130. $this->assertEquals('byPrimary', $method->getName());
  131. $this->assertEquals(ENTITY, $method->getKind());
  132. }
  133. /**
  134. * Tests the method() method throws an exception when passed a
  135. * non-existant method name
  136. *
  137. */
  138. #[@test, @expect('lang.MethodNotImplementedException')]
  139. public function nonExistantMethod() {
  140. $this->method('@@NON-EXISTANT@@');
  141. }
  142. /**
  143. * Tests the method() method throws an exception when passed a
  144. * method name which refers to a non-finder method.
  145. *
  146. */
  147. #[@test, @expect('lang.IllegalArgumentException')]
  148. public function notAFinderMethod() {
  149. $this->method('getPeer');
  150. }
  151. /**
  152. * Tests find(byPrimary())
  153. *
  154. */
  155. #[@test]
  156. public function findByExistingPrimary() {
  157. $this->getConnection()->setResultSet(new MockResultSet(array(
  158. 0 => array( // First row
  159. 'job_id' => 1,
  160. 'title' => $this->getName(),
  161. 'valid_from' => Date::now(),
  162. 'expire_at' => NULL
  163. )
  164. )));
  165. $entity= $this->fixture->find($this->fixture->byPrimary(1));
  166. $this->assertClass($entity, 'net.xp_framework.unittest.rdbms.dataset.Job');
  167. }
  168. /**
  169. * Tests find()->byPrimary()
  170. *
  171. */
  172. #[@test]
  173. public function findByExistingPrimaryFluent() {
  174. $this->getConnection()->setResultSet(new MockResultSet(array(
  175. 0 => array( // First row
  176. 'job_id' => 1,
  177. 'title' => $this->getName(),
  178. 'valid_from' => Date::now(),
  179. 'expire_at' => NULL
  180. )
  181. )));
  182. $entity= $this->fixture->find()->byPrimary(1);
  183. $this->assertClass($entity, 'net.xp_framework.unittest.rdbms.dataset.Job');
  184. }
  185. /**
  186. * Tests find(byPrimary()) for the situation when nothing is returned.
  187. *
  188. */
  189. #[@test]
  190. public function findByNonExistantPrimary() {
  191. $this->assertNull($this->fixture->find($this->fixture->byPrimary(0)));
  192. }
  193. /**
  194. * Tests find(byPrimary()) for the situation when more than one result
  195. * is returned.
  196. *
  197. */
  198. #[@test, @expect('rdbms.finder.FinderException')]
  199. public function findUnexpectedResults() {
  200. $this->getConnection()->setResultSet(new MockResultSet(array(
  201. 0 => array( // First row
  202. 'job_id' => 1,
  203. 'title' => $this->getName(),
  204. 'valid_from' => Date::now(),
  205. 'expire_at' => NULL
  206. ),
  207. 1 => array( // Second row
  208. 'job_id' => 2,
  209. 'title' => $this->getName().' #2',
  210. 'valid_from' => Date::now(),
  211. 'expire_at' => NULL
  212. )
  213. )));
  214. $this->fixture->find($this->fixture->byPrimary(1));
  215. }
  216. /**
  217. * Tests get(byPrimary())
  218. *
  219. */
  220. #[@test]
  221. public function getByExistingPrimary() {
  222. $this->getConnection()->setResultSet(new MockResultSet(array(
  223. 0 => array( // First row
  224. 'job_id' => 1,
  225. 'title' => $this->getName(),
  226. 'valid_from' => Date::now(),
  227. 'expire_at' => NULL
  228. )
  229. )));
  230. $entity= $this->fixture->get($this->fixture->byPrimary(1));
  231. $this->assertClass($entity, 'net.xp_framework.unittest.rdbms.dataset.Job');
  232. }
  233. /**
  234. * Tests get()->byPrimary()
  235. *
  236. */
  237. #[@test]
  238. public function getByExistingPrimaryFluent() {
  239. $this->getConnection()->setResultSet(new MockResultSet(array(
  240. 0 => array( // First row
  241. 'job_id' => 1,
  242. 'title' => $this->getName(),
  243. 'valid_from' => Date::now(),
  244. 'expire_at' => NULL
  245. )
  246. )));
  247. $entity= $this->fixture->get()->byPrimary(1);
  248. $this->assertClass($entity, 'net.xp_framework.unittest.rdbms.dataset.Job');
  249. }
  250. /**
  251. * Tests find(byPrimary()) for the situation when nothing is returned.
  252. *
  253. */
  254. #[@test, @expect('rdbms.finder.NoSuchEntityException')]
  255. public function getByNonExistantPrimary() {
  256. $this->fixture->get($this->fixture->byPrimary(0));
  257. }
  258. /**
  259. * Tests find(byPrimary()) for the situation when more than one result
  260. * is returned.
  261. *
  262. */
  263. #[@test, @expect('rdbms.finder.FinderException')]
  264. public function getUnexpectedResults() {
  265. $this->getConnection()->setResultSet(new MockResultSet(array(
  266. 0 => array( // First row
  267. 'job_id' => 1,
  268. 'title' => $this->getName(),
  269. 'valid_from' => Date::now(),
  270. 'expire_at' => NULL
  271. ),
  272. 1 => array( // Second row
  273. 'job_id' => 2,
  274. 'title' => $this->getName().' #2',
  275. 'valid_from' => Date::now(),
  276. 'expire_at' => NULL
  277. )
  278. )));
  279. $this->fixture->get($this->fixture->byPrimary(1));
  280. }
  281. /**
  282. * Tests findAll(newestJobs())
  283. *
  284. */
  285. #[@test]
  286. public function findNewestJobs() {
  287. $this->getConnection()->setResultSet(new MockResultSet(array(
  288. 0 => array( // First row
  289. 'job_id' => 1,
  290. 'title' => $this->getName(),
  291. 'valid_from' => Date::now(),
  292. 'expire_at' => NULL
  293. ),
  294. 1 => array( // Second row
  295. 'job_id' => 2,
  296. 'title' => $this->getName().' #2',
  297. 'valid_from' => Date::now(),
  298. 'expire_at' => NULL
  299. )
  300. )));
  301. $collection= $this->fixture->findAll($this->fixture->newestJobs());
  302. $this->assertEquals(2, sizeof($collection));
  303. }
  304. /**
  305. * Tests findAll()->newestJobs()
  306. *
  307. */
  308. #[@test]
  309. public function findNewestJobsFluent() {
  310. $this->getConnection()->setResultSet(new MockResultSet(array(
  311. 0 => array( // First row
  312. 'job_id' => 1,
  313. 'title' => $this->getName(),
  314. 'valid_from' => Date::now(),
  315. 'expire_at' => NULL
  316. ),
  317. 1 => array( // Second row
  318. 'job_id' => 2,
  319. 'title' => $this->getName().' #2',
  320. 'valid_from' => Date::now(),
  321. 'expire_at' => NULL
  322. )
  323. )));
  324. $collection= $this->fixture->findAll()->newestJobs();
  325. $this->assertEquals(2, sizeof($collection));
  326. }
  327. /**
  328. * Tests getAll(newestJobs())
  329. *
  330. */
  331. #[@test]
  332. public function getNewestJobs() {
  333. $this->getConnection()->setResultSet(new MockResultSet(array(
  334. 0 => array( // First row
  335. 'job_id' => 1,
  336. 'title' => $this->getName(),
  337. 'valid_from' => Date::now(),
  338. 'expire_at' => NULL
  339. ),
  340. 1 => array( // Second row
  341. 'job_id' => 2,
  342. 'title' => $this->getName().' #2',
  343. 'valid_from' => Date::now(),
  344. 'expire_at' => NULL
  345. )
  346. )));
  347. $collection= $this->fixture->getAll($this->fixture->newestJobs());
  348. $this->assertEquals(2, sizeof($collection));
  349. }
  350. /**
  351. * Tests getAll()->newestJobs()
  352. *
  353. */
  354. #[@test]
  355. public function getNewestJobsFluent() {
  356. $this->getConnection()->setResultSet(new MockResultSet(array(
  357. 0 => array( // First row
  358. 'job_id' => 1,
  359. 'title' => $this->getName(),
  360. 'valid_from' => Date::now(),
  361. 'expire_at' => NULL
  362. ),
  363. 1 => array( // Second row
  364. 'job_id' => 2,
  365. 'title' => $this->getName().' #2',
  366. 'valid_from' => Date::now(),
  367. 'expire_at' => NULL
  368. )
  369. )));
  370. $collection= $this->fixture->getAll()->newestJobs();
  371. $this->assertEquals(2, sizeof($collection));
  372. }
  373. /**
  374. * Tests getAll(newestJobs())
  375. *
  376. */
  377. #[@test, @expect('rdbms.finder.NoSuchEntityException')]
  378. public function getNothingFound() {
  379. $this->fixture->getAll($this->fixture->newestJobs());
  380. }
  381. /**
  382. * Tests find() wraps SQLExceptions into FinderExceptions
  383. *
  384. */
  385. #[@test, @expect('rdbms.finder.FinderException')]
  386. public function findWrapsSQLException() {
  387. $this->getConnection()->makeQueryFail(6010, 'Not enough power');
  388. $this->fixture->find(new Criteria());
  389. }
  390. /**
  391. * Tests findAll() wraps SQLExceptions into FinderExceptions
  392. *
  393. */
  394. #[@test, @expect('rdbms.finder.FinderException')]
  395. public function findAllWrapsSQLException() {
  396. $this->getConnection()->makeQueryFail(6010, 'Not enough power');
  397. $this->fixture->findAll(new Criteria());
  398. }
  399. /**
  400. * Tests findAll() wraps SQLExceptions into FinderExceptions
  401. *
  402. */
  403. #[@test, @expect(class= 'lang.Error', withMessage= '/Call to undefined method .+JobFinder::nonExistantMethod/')]
  404. public function fluentNonExistantFinder() {
  405. $this->fixture->findAll()->nonExistantMethod(new Criteria());
  406. }
  407. /**
  408. * Test GenericFinder
  409. *
  410. */
  411. #[@test]
  412. public function genericFinderGetAll() {
  413. $this->getConnection()->setResultSet(new MockResultSet(array(
  414. 0 => array( // First row
  415. 'job_id' => 1,
  416. 'title' => $this->getName(),
  417. 'valid_from' => Date::now(),
  418. 'expire_at' => NULL
  419. ),
  420. 1 => array( // Second row
  421. 'job_id' => 2,
  422. 'title' => $this->getName().' #2',
  423. 'valid_from' => Date::now(),
  424. 'expire_at' => NULL
  425. )
  426. )));
  427. $all= create(new GenericFinder(Job::getPeer()))->getAll(new Criteria());
  428. $this->assertEquals(2, sizeof($all));
  429. $this->assertClass($all[0], 'net.xp_framework.unittest.rdbms.dataset.Job');
  430. $this->assertClass($all[1], 'net.xp_framework.unittest.rdbms.dataset.Job');
  431. }
  432. }
  433. ?>