CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeFixtureManager
  • CakeTestFixture
  • CakeTestModel
  1: <?php
  2: /**
  3:  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice
  8:  *
  9:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
 11:  * @package       Cake.TestSuite.Fixture
 12:  * @since         CakePHP(tm) v 1.2.0.4667
 13:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 14:  */
 15: 
 16: App::uses('CakeSchema', 'Model');
 17: 
 18: /**
 19:  * CakeTestFixture is responsible for building and destroying tables to be used 
 20:  * during testing.
 21:  *
 22:  * @package       Cake.TestSuite.Fixture
 23:  */
 24: class CakeTestFixture {
 25: 
 26: /**
 27:  * Name of the object
 28:  *
 29:  * @var string
 30:  */
 31:     public $name = null;
 32: 
 33: /**
 34:  * Cake's DBO driver (e.g: DboMysql).
 35:  *
 36:  * @var object
 37:  */
 38:     public $db = null;
 39: 
 40: /**
 41:  * Fixture Datasource
 42:  *
 43:  * @var string
 44:  */
 45:     public $useDbConfig = 'test';
 46: 
 47: /**
 48:  * Full Table Name
 49:  *
 50:  * @var string
 51:  */
 52:     public $table = null;
 53: 
 54: /**
 55:  * List of datasources where this fixture has been created
 56:  *
 57:  * @var array
 58:  */
 59:     public $created = array();
 60: 
 61: /**
 62:  * Instantiate the fixture.
 63:  *
 64:  * @throws CakeException on invalid datasource usage.
 65:  */
 66:     public function __construct() {
 67:         if ($this->name === null) {
 68:             if (preg_match('/^(.*)Fixture$/', get_class($this), $matches)) {
 69:                 $this->name = $matches[1];
 70:             } else {
 71:                 $this->name = get_class($this);
 72:             }
 73:         }
 74:         $connection = 'test';
 75:         if (!empty($this->useDbConfig)) {
 76:             $connection = $this->useDbConfig;
 77:             if (strpos($connection, 'test') !== 0) {
 78:                 throw new CakeException(__d('cake_dev', 'Invalid datasource %s for object %s', $connection, $this->name));
 79:             }
 80:         }
 81:         $this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => $connection));
 82:         $this->init();
 83:     }
 84: 
 85: /**
 86:  * Initialize the fixture.
 87:  *
 88:  * @return void
 89:  * @throws MissingModelException Whe importing from a model that does not exist.
 90:  */
 91:     public function init() {
 92:         if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
 93:             $import = array_merge(
 94:                 array('connection' => 'default', 'records' => false),
 95:                 is_array($this->import) ? $this->import : array('model' => $this->import)
 96:             );
 97: 
 98:             $this->Schema->connection = $import['connection'];
 99:             if (isset($import['model'])) {
100:                 list($plugin, $modelClass) = pluginSplit($import['model'], true);
101:                 App::uses($modelClass, $plugin . 'Model');
102:                 if (!class_exists($modelClass)) {
103:                     throw new MissingModelException(array('class' => $modelClass));
104:                 }
105:                 $model = new $modelClass(null, null, $import['connection']);
106:                 $db = $model->getDataSource();
107:                 if (empty($model->tablePrefix)) {
108:                     $model->tablePrefix = $db->config['prefix'];
109:                 }
110:                 $this->fields = $model->schema(true);
111:                 $this->fields[$model->primaryKey]['key'] = 'primary';
112:                 $this->table = $db->fullTableName($model, false, false);
113:                 ClassRegistry::config(array('ds' => 'test'));
114:                 ClassRegistry::flush();
115:             } elseif (isset($import['table'])) {
116:                 $model = new Model(null, $import['table'], $import['connection']);
117:                 $db = ConnectionManager::getDataSource($import['connection']);
118:                 $db->cacheSources = false;
119:                 $model->useDbConfig = $import['connection'];
120:                 $model->name = Inflector::camelize(Inflector::singularize($import['table']));
121:                 $model->table = $import['table'];
122:                 $model->tablePrefix = $db->config['prefix'];
123:                 $this->fields = $model->schema(true);
124:                 ClassRegistry::flush();
125:             }
126: 
127:             if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
128:                 $this->table = str_replace($db->config['prefix'], '', $this->table);
129:             }
130: 
131:             if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
132:                 $this->records = array();
133:                 $query = array(
134:                     'fields' => $db->fields($model, null, array_keys($this->fields)),
135:                     'table' => $db->fullTableName($model),
136:                     'alias' => $model->alias,
137:                     'conditions' => array(),
138:                     'order' => null,
139:                     'limit' => null,
140:                     'group' => null
141:                 );
142:                 $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
143: 
144:                 if ($records !== false && !empty($records)) {
145:                     $this->records = Set::extract($records, '{n}.' . $model->alias);
146:                 }
147:             }
148:         }
149: 
150:         if (!isset($this->table)) {
151:             $this->table = Inflector::underscore(Inflector::pluralize($this->name));
152:         }
153: 
154:         if (!isset($this->primaryKey) && isset($this->fields['id'])) {
155:             $this->primaryKey = 'id';
156:         }
157:     }
158: 
159: /**
160:  * Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
161:  *
162:  * @param object    $db An instance of the database object used to create the fixture table
163:  * @return boolean True on success, false on failure
164:  */
165:     public function create($db) {
166:         if (!isset($this->fields) || empty($this->fields)) {
167:             return false;
168:         }
169: 
170:         if (empty($this->fields['tableParameters']['engine'])) {
171:             $canUseMemory = true;
172:             foreach ($this->fields as $field => $args) {
173: 
174:                 if (is_string($args)) {
175:                     $type = $args;
176:                 } elseif (!empty($args['type'])) {
177:                     $type = $args['type'];
178:                 } else {
179:                     continue;
180:                 }
181: 
182:                 if (in_array($type, array('blob', 'text', 'binary'))) {
183:                     $canUseMemory = false;
184:                     break;
185:                 }
186:             }
187: 
188:             if ($canUseMemory) {
189:                 $this->fields['tableParameters']['engine'] = 'MEMORY';
190:             }
191:         }
192:         $this->Schema->build(array($this->table => $this->fields));
193:         try {
194:             $db->execute($db->createSchema($this->Schema), array('log' => false));
195:             $this->created[] = $db->configKeyName;
196:         } catch (Exception $e) {
197:             return false;
198:         }
199:         return true;
200:     }
201: 
202: /**
203:  * Run after all tests executed, should return SQL statement to drop table for this fixture.
204:  *
205:  * @param object    $db An instance of the database object used to create the fixture table
206:  * @return boolean True on success, false on failure
207:  */
208:     public function drop($db) {
209:         if (empty($this->fields)) {
210:             return false;
211:         }
212:         $this->Schema->build(array($this->table => $this->fields));
213:         try {
214: 
215:             $db->execute($db->dropSchema($this->Schema), array('log' => false));
216:             $this->created = array_diff($this->created, array($db->configKeyName));
217:         } catch (Exception $e) {
218:             return false;
219:         }
220:         return true;
221:     }
222: 
223: /**
224:  * Run before each tests is executed, should return a set of SQL statements to insert records for the table
225:  * of this fixture could be executed successfully.
226:  *
227:  * @param object $db An instance of the database into which the records will be inserted
228:  * @return boolean on success or if there are no records to insert, or false on failure
229:  */
230:     public function insert($db) {
231:         if (!isset($this->_insert)) {
232:             $values = array();
233:             if (isset($this->records) && !empty($this->records)) {
234:                 $fields = array();
235:                 foreach ($this->records as $record) {
236:                     $fields = array_merge($fields, array_keys(array_intersect_key($record, $this->fields)));
237:                 }
238:                 $fields = array_unique($fields);
239:                 $default = array_fill_keys($fields, null);
240:                 foreach ($this->records as $record) {
241:                     $fields = array_keys($record);
242:                     $values[] = array_values(array_merge($default, $record));
243:                 }
244:                 return $db->insertMulti($this->table, $fields, $values);
245:             }
246:             return true;
247:         }
248:     }
249: 
250: /**
251:  * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
252:  * truncate.
253:  *
254:  * @param object $db A reference to a db instance
255:  * @return boolean
256:  */
257:     public function truncate($db) {
258:         $fullDebug = $db->fullDebug;
259:         $db->fullDebug = false;
260:         $return = $db->truncate($this->table);
261:         $db->fullDebug = $fullDebug;
262:         return $return;
263:     }
264: 
265: }
266: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs