DataSourceTest.php 5.1 KB

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