DataSourceTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * DataSourceTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model.Datasource
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Model', 'Model');
  20. App::uses('DataSource', 'Model/Datasource');
  21. /**
  22. * TestSource
  23. *
  24. * @package Cake.Test.Case.Model.Datasource
  25. */
  26. class TestSource extends DataSource {
  27. /**
  28. * _schema
  29. * @var type
  30. */
  31. protected $_schema = array(
  32. 'id' => array(
  33. 'type' => 'integer',
  34. 'null' => false,
  35. 'key' => 'primary',
  36. 'length' => 11,
  37. ),
  38. 'text' => array(
  39. 'type' => 'string',
  40. 'null' => true,
  41. 'length' => 140,
  42. ),
  43. 'status' => array(
  44. 'type' => 'string',
  45. 'null' => true,
  46. 'length' => 140,
  47. ),
  48. 'customField' => array(
  49. 'type' => 'string',
  50. 'null' => true,
  51. 'length' => 255,
  52. ),
  53. );
  54. /**
  55. * listSources
  56. *
  57. * @return boolean
  58. */
  59. public function listSources() {
  60. return null;
  61. }
  62. /**
  63. * Returns the schema for the datasource to enable create/update
  64. *
  65. * @param object $Model
  66. * @return array
  67. */
  68. public function describe(Model $Model) {
  69. return $this->_schema;
  70. }
  71. /**
  72. * Just return $func to pass to read() to figure out the COUNT
  73. * Required for delete/update to work
  74. *
  75. * @param Model $Model
  76. * @param type $func
  77. * @param type $params
  78. * @return array
  79. */
  80. public function calculate(Model $Model, $func, $params = array()) {
  81. return $func;
  82. }
  83. }
  84. /**
  85. * DataSourceTest class
  86. *
  87. * @package Cake.Test.Case.Model.Datasource
  88. */
  89. class DataSourceTest extends CakeTestCase {
  90. /**
  91. * Name of test source
  92. *
  93. * @var string
  94. */
  95. public $sourceName = 'myapitest';
  96. /**
  97. * setUp method
  98. *
  99. * @return void
  100. */
  101. public function setUp() {
  102. parent::setUp();
  103. $this->Source = $this->getMock(
  104. 'TestSource',
  105. array('create', 'read', 'update', 'delete')
  106. );
  107. ConnectionManager::create($this->sourceName, array(
  108. 'datasource' => get_class($this->Source),
  109. 'apiKey' => '1234abcd',
  110. ));
  111. $this->Model = $this->getMock(
  112. 'Model',
  113. array('getDataSource'),
  114. array(array('ds' => $this->sourceName))
  115. );
  116. $this->Model->expects($this->any())
  117. ->method('getDataSource')
  118. ->will($this->returnValue($this->Source));
  119. }
  120. /**
  121. * tearDown method
  122. *
  123. * @return void
  124. */
  125. public function tearDown() {
  126. parent::tearDown();
  127. unset($this->Model, $this->Source);
  128. ConnectionManager::drop($this->sourceName);
  129. }
  130. /**
  131. * testCreate
  132. *
  133. * @return void
  134. */
  135. public function testCreate() {
  136. $data = array(
  137. $this->Model->alias => array(
  138. 'text' => 'This is a test',
  139. 'status' => 'Test status',
  140. 'customField' => array(
  141. 'array', 'field', 'type',
  142. 'for', 'custom', 'datasources',
  143. ),
  144. ),
  145. );
  146. $this->Source->expects($this->once())
  147. ->method('create')
  148. ->with(
  149. $this->equalTo($this->Model),
  150. $this->equalTo(array_keys($data[$this->Model->alias])),
  151. $this->equalTo(array_values($data[$this->Model->alias]))
  152. );
  153. $this->Model->save($data);
  154. }
  155. /**
  156. * testRead
  157. *
  158. * @return void
  159. */
  160. public function testRead() {
  161. $expected = array(
  162. 'conditions' => array('status' => 'test'),
  163. 'fields' => null,
  164. 'joins' => array(),
  165. 'limit' => 10,
  166. 'offset' => null,
  167. 'order' => array(array('status')),
  168. 'page' => 1,
  169. 'group' => null,
  170. 'callbacks' => true,
  171. );
  172. $this->Source->expects($this->once())
  173. ->method('read')
  174. ->with(
  175. $this->anything(),
  176. $this->equalTo($expected)
  177. );
  178. $this->Model->find('all', array(
  179. 'conditions' => array('status' => 'test'),
  180. 'limit' => 10,
  181. 'order' => array('status'),
  182. ));
  183. }
  184. /**
  185. * testUpdate
  186. *
  187. * @return void
  188. */
  189. public function testUpdate() {
  190. $data = array(
  191. $this->Model->alias => array(
  192. 'id' => 1,
  193. 'text' => 'This is a test',
  194. 'status' => 'Test status',
  195. 'customField' => array(
  196. 'array', 'field', 'type',
  197. 'for', 'custom', 'datasources',
  198. ),
  199. ),
  200. );
  201. $this->Source->expects($this->any())
  202. ->method('read')
  203. ->will($this->returnValue(array(
  204. array($this->Model->alias => array('count' => 1))
  205. )));
  206. $this->Source->expects($this->once())
  207. ->method('update')
  208. ->with(
  209. $this->equalTo($this->Model),
  210. $this->equalTo(array_keys($data[$this->Model->alias])),
  211. $this->equalTo(array_values($data[$this->Model->alias]))
  212. );
  213. $this->Model->save($data);
  214. }
  215. /**
  216. * testDelete
  217. *
  218. * @return void
  219. */
  220. public function testDelete() {
  221. $this->Source->expects($this->any())
  222. ->method('read')
  223. ->will($this->returnValue(array(
  224. array($this->Model->alias => array('count' => 1))
  225. )));
  226. $this->Source->expects($this->once())
  227. ->method('delete')
  228. ->with(
  229. $this->equalTo($this->Model),
  230. $this->equalTo(array($this->Model->alias . '.id' => 1))
  231. );
  232. $this->Model->delete(1);
  233. }
  234. }