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