DataSourceTest.php 5.1 KB

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