DataSourceTest.php 5.3 KB

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