PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Case/Lib/BanchaApiTest.php

http://github.com/Bancha/Bancha
PHP | 291 lines | 147 code | 40 blank | 104 comment | 0 complexity | 76a9ab2e12df66c836d941d305ac579c MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Bancha : Seamlessly integrates CakePHP with Ext JS and Sencha Touch (http://bancha.io)
  4. * Copyright 2011-2014 codeQ e.U.
  5. *
  6. * @package Bancha.Test.Case.Lib
  7. * @copyright Copyright 2011-2014 codeQ e.U.
  8. * @link http://bancha.io Bancha
  9. * @since Bancha v 0.9.0
  10. * @author Roland Schuetz <mail@rolandschuetz.at>
  11. * @author Florian Eckerstorfer <f.eckerstorfer@gmail.com>
  12. */
  13. App::uses('BanchaApi', 'Bancha.Bancha');
  14. /**
  15. * BanchaApiTest
  16. *
  17. * @package Bancha.Test.Case.Lib
  18. * @author Roland Schuetz <mail@rolandschuetz.at>
  19. * @author Florian Eckerstorfer <f.eckerstorfer@gmail.com>
  20. * @since Bancha v 0.9.0
  21. */
  22. class BanchaApiTest extends CakeTestCase {
  23. public $fixtures = array(
  24. 'plugin.bancha.article',
  25. 'plugin.bancha.articles_tag',
  26. 'plugin.bancha.user',
  27. 'plugin.bancha.tag'
  28. );
  29. /**
  30. * Keeps a reference to the default paths, since
  31. * we need to change them in the setUp method.
  32. *
  33. * @var array
  34. */
  35. protected $_originalPaths = null;
  36. /**
  37. * setUp method
  38. *
  39. * @return void
  40. */
  41. public function setUp() {
  42. parent::setUp();
  43. $this->_originalPaths = App::paths();
  44. // build up the test paths
  45. App::build(array(
  46. 'Controller' => App::pluginPath('Bancha') . 'Test' . DS . 'test_app' . DS . 'Controller' . DS,
  47. 'Model' => App::pluginPath('Bancha') . 'Test' . DS . 'test_app' . DS . 'Model' . DS,
  48. 'Plugin' => App::pluginPath('Bancha') . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  49. ), App::RESET);
  50. // load plugin from test_app
  51. CakePlugin::load('TestPlugin');
  52. // force the cache to renew
  53. App::objects('plugin', null, false);
  54. }
  55. /**
  56. * tearDown method
  57. *
  58. * @return void
  59. */
  60. public function tearDown() {
  61. parent::tearDown();
  62. // tear down
  63. CakePlugin::unload('TestPlugin');
  64. // reset the paths
  65. App::build($this->_originalPaths, App::RESET);
  66. // force the cache to renew
  67. App::objects('plugin', null, false);
  68. }
  69. /**
  70. * Test retrieving the remotable models
  71. *
  72. * @return void
  73. */
  74. public function testGetRemotableModels() {
  75. // prepare
  76. $api = new BanchaApi();
  77. $remotableModels = $api->getRemotableModels();
  78. // test app models for set
  79. $this->assertContains('Article', $remotableModels);
  80. $this->assertContains('User', $remotableModels);
  81. $this->assertContains('Tag', $remotableModels);
  82. $this->assertContains('ArticlesTag', $remotableModels);
  83. // test plugin models are set
  84. $this->assertContains('TestPlugin.Comment', $remotableModels);
  85. }
  86. /**
  87. * Test filtering the remotable models
  88. *
  89. * @return void
  90. */
  91. public function testFilterRemotableModels() {
  92. // prepare
  93. $api = new BanchaApi();
  94. $remotableModels = array('Article', 'User', 'Tag', 'ArticlesTag', 'TestPlugin.Comment');
  95. // expose all remotable models
  96. $filteredModels = $api->filterRemotableModels($remotableModels, 'all');
  97. $this->assertCount(5, $filteredModels);
  98. $this->assertContains('Article', $filteredModels);
  99. $this->assertContains('ArticlesTag', $filteredModels);
  100. $this->assertContains('Tag', $filteredModels);
  101. $this->assertContains('User', $filteredModels);
  102. $this->assertContains('TestPlugin.Comment', $filteredModels);
  103. // expose one model
  104. $filteredModels = $api->filterRemotableModels($remotableModels, '[User]');
  105. $this->assertCount(1, $filteredModels);
  106. $this->assertContains('User', $filteredModels);
  107. // expose one model (alternative syntax)
  108. $filteredModels = $api->filterRemotableModels($remotableModels, 'User');
  109. $this->assertCount(1, $filteredModels);
  110. $this->assertContains('User', $filteredModels);
  111. // expose two models
  112. $filteredModels = $api->filterRemotableModels($remotableModels, '[User,Article]');
  113. $this->assertCount(2, $filteredModels);
  114. $this->assertContains('User', $filteredModels);
  115. $this->assertContains('Article', $filteredModels);
  116. $filteredModels = $api->filterRemotableModels($remotableModels, '[ User, Article]');
  117. $this->assertCount(2, $filteredModels);
  118. $this->assertContains('User', $filteredModels);
  119. $this->assertContains('Article', $filteredModels);
  120. // expose two models (alternative syntax)
  121. $filteredModels = $api->filterRemotableModels($remotableModels, 'User,Article');
  122. $this->assertCount(2, $filteredModels);
  123. $this->assertContains('User', $filteredModels);
  124. $this->assertContains('Article', $filteredModels);
  125. // expose two models (alternative usage)
  126. $filteredModels = $api->filterRemotableModels($remotableModels, array('User', 'Article'));
  127. $this->assertCount(2, $filteredModels);
  128. $this->assertContains('User', $filteredModels);
  129. $this->assertContains('Article', $filteredModels);
  130. // expose no models
  131. $filteredModels = $api->filterRemotableModels($remotableModels, '');
  132. $this->assertCount(0, $filteredModels);
  133. // expose two models, one from plugin
  134. $filteredModels = $api->filterRemotableModels($remotableModels, '[User,TestPlugin.Comment]');
  135. $this->assertCount(2, $filteredModels);
  136. $this->assertContains('User', $filteredModels);
  137. $this->assertContains('TestPlugin.Comment', $filteredModels);
  138. }
  139. /**
  140. * filterRemotableModels() should throw a MissingModelException when a model is provided in $filter which is not
  141. * remotable model.
  142. *
  143. * @return void
  144. * @expectedException MissingModelException
  145. */
  146. public function testFilterRemotableModelsMissingModel() {
  147. $api = new BanchaApi();
  148. $api->filterRemotableModels(array(), '[InvalidModel]');
  149. }
  150. /**
  151. * Tests if getMetadata returns meta data for all given models.
  152. *
  153. * @return void
  154. */
  155. public function testGetMetadata() {
  156. $api = new BanchaApi();
  157. $metadata = $api->getMetadata(array('User', 'Article', 'TestPlugin.Comment'));
  158. $this->assertCount(5, $metadata); // 3 models + 2 metadata properties
  159. $this->assertArrayHasKey('User', $metadata);
  160. $this->assertArrayHasKey('Article', $metadata);
  161. $this->assertArrayHasKey('TestPlugin.Comment', $metadata);
  162. $this->assertArrayHasKey('_UID', $metadata);
  163. $this->assertArrayHasKey('_ServerDebugLevel', $metadata);
  164. $this->assertTrue(is_array($metadata['User']));
  165. $this->assertTrue(is_array($metadata['Article']));
  166. $this->assertTrue(is_array($metadata['TestPlugin.Comment']));
  167. $this->assertTrue(strlen($metadata['_UID']) > 0);
  168. }
  169. /**
  170. * Test getControllerClassByModelClass
  171. *
  172. * @return void
  173. */
  174. public function testGetControllerClassByModelClass() {
  175. $api = new BanchaApi();
  176. $this->assertEquals('UsersController', $api->getControllerClassByModelClass('User'));
  177. $this->assertEquals('TestPlugin.CommentsController', $api->getControllerClassByModelClass('TestPlugin.Comment'));
  178. }
  179. /**
  180. * Test getCrudActionsOfController
  181. *
  182. * @return void
  183. */
  184. public function testGetCrudActionsOfController() {
  185. $api = new BanchaApi();
  186. // test app controller with full CRUD
  187. $crudActions = $api->getCrudActionsOfController('UsersController');
  188. $this->assertCount(6, $crudActions);
  189. $this->assertEquals('getAll', $crudActions[0]['name']);
  190. $this->assertEquals(0, $crudActions[0]['len']);
  191. $this->assertEquals('read', $crudActions[1]['name']);
  192. $this->assertEquals(1, $crudActions[1]['len']);
  193. $this->assertEquals('submit', $crudActions[5]['name']);
  194. $this->assertEquals(1, $crudActions[5]['len']);
  195. $this->assertEquals(true, $crudActions[5]['formHandler']);
  196. // test plugin controller with create, read, update
  197. $crudActions = $api->getCrudActionsOfController('TestPlugin.CommentsController');
  198. $this->assertCount(5, $crudActions);
  199. $this->assertEquals('getAll', $crudActions[0]['name']);
  200. $this->assertEquals(0, $crudActions[0]['len']);
  201. $this->assertEquals('read', $crudActions[1]['name']);
  202. $this->assertEquals(1, $crudActions[1]['len']);
  203. $this->assertEquals('submit', $crudActions[4]['name']);
  204. $this->assertEquals(1, $crudActions[4]['len']);
  205. $this->assertEquals(true, $crudActions[4]['formHandler']);
  206. // test plugin controller with no CRUD method
  207. App::uses('PluginTestsController', 'TestPlugin.Controller');
  208. $crudActions = $api->getCrudActionsOfController('TestPlugin.PluginTestsController');
  209. $this->assertCount(0, $crudActions);
  210. }
  211. /**
  212. * Test getRemotableMethods
  213. *
  214. * @return void
  215. */
  216. public function testGetRemotableMethods() {
  217. // prepare
  218. $api = new BanchaApi();
  219. $remotableMethods = $api->getRemotableMethods();
  220. $this->assertCount(2, $remotableMethods);
  221. // test app controller
  222. $this->assertContains('HelloWorld', array_keys($remotableMethods));
  223. $this->assertCount(2, $remotableMethods['HelloWorld']);
  224. $this->assertEquals('hello', $remotableMethods['HelloWorld'][0]['name']);
  225. $this->assertEquals(0, $remotableMethods['HelloWorld'][0]['len']);
  226. $this->assertEquals('helloyou', $remotableMethods['HelloWorld'][1]['name']);
  227. $this->assertEquals(2, $remotableMethods['HelloWorld'][1]['len']);
  228. // test plugin controller
  229. $this->assertContains('TestPlugin.PluginTest', array_keys($remotableMethods));
  230. $this->assertCount(1, $remotableMethods['TestPlugin.PluginTest']);
  231. $this->assertEquals('exposedTestMethod', $remotableMethods['TestPlugin.PluginTest'][0]['name']);
  232. $this->assertEquals(0, $remotableMethods['HelloWorld'][0]['len']);
  233. }
  234. /**
  235. * Test getRemotableModelActions
  236. *
  237. * @return void
  238. */
  239. public function testGetRemotableModelActions() {
  240. $api = new BanchaApi();
  241. // this is simply a wrapper function, se very simple testing is sufficient
  242. $remotableActions = $api->getRemotableModelActions(array('Article', 'TestPlugin.Comment'));
  243. $this->assertCount(2, $remotableActions);
  244. // test app model
  245. $this->assertCount(6, $remotableActions['Article']);
  246. $this->assertEquals('getAll', $remotableActions['Article'][0]['name']);
  247. // test plugin model
  248. $this->assertCount(5, $remotableActions['TestPlugin.Comment']); // (comments does not support delete)
  249. $this->assertEquals('getAll', $remotableActions['TestPlugin.Comment'][0]['name']);
  250. }
  251. }