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.0 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 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
        • Auth
    • Core
    • Error
    • 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/view/1196/Testing>
  4:  * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  * @link          http://book.cakephp.org/view/1196/Testing 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:  */
 37:     public $db = null;
 38: 
 39: /**
 40:  * Full Table Name
 41:  *
 42:  */
 43:     public $table = null;
 44: 
 45: /**
 46:  * Instantiate the fixture.
 47:  *
 48:  */
 49:     public function __construct() {
 50:         if ($this->name === null) {
 51:             if (preg_match('/^(.*)Fixture$/', get_class($this), $matches)) {
 52:                 $this->name = $matches[1];
 53:             } else {
 54:                 $this->name = get_class($this);
 55:             }
 56:         }
 57:         $this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test'));
 58:         $this->init();
 59:     }
 60: 
 61: /**
 62:  * Initialize the fixture.
 63:  *
 64:  */
 65:     public function init() {
 66:         if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
 67:             $import = array_merge(
 68:                 array('connection' => 'default', 'records' => false),
 69:                 is_array($this->import) ? $this->import : array('model' => $this->import)
 70:             );
 71: 
 72:             if (isset($import['model'])) {
 73:                 list($plugin, $modelClass) = pluginSplit($import['model'], true);
 74:                 App::uses($modelClass, $plugin . 'Model');
 75:                 if (!class_exists($modelClass)) {
 76:                     throw new MissingModelException(array('class' => $modelClass));
 77:                 }
 78:                 $model = new $modelClass(null, null, $import['connection']);
 79:                 $db = $model->getDataSource();
 80:                 if (empty($model->tablePrefix)) {
 81:                     $model->tablePrefix = $db->config['prefix'];
 82:                 }
 83:                 $this->fields = $model->schema(true);
 84:                 $this->fields[$model->primaryKey]['key'] = 'primary';
 85:                 $this->table = $db->fullTableName($model, false);
 86:                 ClassRegistry::config(array('ds' => 'test'));
 87:                 ClassRegistry::flush();
 88:             } elseif (isset($import['table'])) {
 89:                 $model = new Model(null, $import['table'], $import['connection']);
 90:                 $db = ConnectionManager::getDataSource($import['connection']);
 91:                 $db->cacheSources = false;
 92:                 $model->useDbConfig = $import['connection'];
 93:                 $model->name = Inflector::camelize(Inflector::singularize($import['table']));
 94:                 $model->table = $import['table'];
 95:                 $model->tablePrefix = $db->config['prefix'];
 96:                 $this->fields = $model->schema(true);
 97:                 ClassRegistry::flush();
 98:             }
 99: 
100:             if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
101:                 $this->table = str_replace($db->config['prefix'], '', $this->table);
102:             }
103: 
104:             if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
105:                 $this->records = array();
106:                 $query = array(
107:                     'fields' => $db->fields($model, null, array_keys($this->fields)),
108:                     'table' => $db->fullTableName($model),
109:                     'alias' => $model->alias,
110:                     'conditions' => array(),
111:                     'order' => null,
112:                     'limit' => null,
113:                     'group' => null
114:                 );
115:                 $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
116: 
117:                 if ($records !== false && !empty($records)) {
118:                     $this->records = Set::extract($records, '{n}.' . $model->alias);
119:                 }
120:             }
121:         }
122: 
123:         if (!isset($this->table)) {
124:             $this->table = Inflector::underscore(Inflector::pluralize($this->name));
125:         }
126: 
127:         if (!isset($this->primaryKey) && isset($this->fields['id'])) {
128:             $this->primaryKey = 'id';
129:         }
130:     }
131: 
132: /**
133:  * Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
134:  *
135:  * @param object    $db An instance of the database object used to create the fixture table
136:  * @return boolean True on success, false on failure
137:  */
138:     public function create($db) {
139:         if (!isset($this->fields) || empty($this->fields)) {
140:             return false;
141:         }
142: 
143:         if (empty($this->fields['tableParameters']['engine'])) {
144:             $canUseMemory = true;
145:             foreach ($this->fields as $field => $args) {
146: 
147:                 if (is_string($args)) {
148:                     $type = $args;
149:                 } elseif (!empty($args['type'])) {
150:                     $type = $args['type'];
151:                 } else {
152:                     continue;
153:                 }
154: 
155:                 if (in_array($type, array('blob', 'text', 'binary'))) {
156:                     $canUseMemory = false;
157:                     break;
158:                 }
159:             }
160: 
161:             if ($canUseMemory) {
162:                 $this->fields['tableParameters']['engine'] = 'MEMORY';
163:             }
164:         }
165:         $this->Schema->build(array($this->table => $this->fields));
166:         try {
167:             $db->execute($db->createSchema($this->Schema), array('log' => false));
168:         } catch (Exception $e) {
169:             return false;
170:         }
171:         return true;
172:     }
173: 
174: /**
175:  * Run after all tests executed, should return SQL statement to drop table for this fixture.
176:  *
177:  * @param object    $db An instance of the database object used to create the fixture table
178:  * @return boolean True on success, false on failure
179:  */
180:     public function drop($db) {
181:         if (empty($this->fields)) {
182:             return false;
183:         }
184:         $this->Schema->build(array($this->table => $this->fields));
185:         try {
186: 
187:             $db->execute($db->dropSchema($this->Schema), array('log' => false));
188:         } catch (Exception $e) {
189:             return false;
190:         }
191:         return true;
192:     }
193: 
194: /**
195:  * Run before each tests is executed, should return a set of SQL statements to insert records for the table
196:  * of this fixture could be executed successfully.
197:  *
198:  * @param object $db An instance of the database into which the records will be inserted
199:  * @return boolean on success or if there are no records to insert, or false on failure
200:  */
201:     public function insert($db) {
202:         if (!isset($this->_insert)) {
203:             $values = array();
204:             if (isset($this->records) && !empty($this->records)) {
205:                 $fields = array();
206:                 foreach ($this->records as $record) {
207:                     $fields = array_merge($fields, array_keys(array_intersect_key($record, $this->fields)));
208:                 }
209:                 $fields = array_unique($fields);
210:                 $default = array_fill_keys($fields, null);
211:                 foreach ($this->records as $record) {
212:                     $fields = array_keys($record);
213:                     $values[] = array_values(array_merge($default, $record));
214:                 }
215:                 return $db->insertMulti($this->table, $fields, $values);
216:             }
217:             return true;
218:         }
219:     }
220: 
221: 
222: /**
223:  * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
224:  * truncate.
225:  *
226:  * @param object $db A reference to a db instance
227:  * @return boolean
228:  */
229:     public function truncate($db) {
230:         $fullDebug = $db->fullDebug;
231:         $db->fullDebug = false;
232:         $return = $db->truncate($this->table);
233:         $db->fullDebug = $fullDebug;
234:         return $return;
235:     }
236: }
237: 
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