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:  * A factory class to manage the life cycle of test fixtures
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       Cake.TestSuite.Fixture
 16:  * @since         CakePHP(tm) v 2.0
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: App::uses('ConnectionManager', 'Model');
 21: App::uses('ClassRegistry', 'Utility');
 22: 
 23: /**
 24:  * A factory class to manage the life cycle of test fixtures
 25:  *
 26:  * @package       Cake.TestSuite.Fixture
 27:  */
 28: class CakeFixtureManager {
 29: 
 30: /**
 31:  * Was this class already initialized?
 32:  *
 33:  * @var boolean
 34:  */
 35:     protected $_initialized = false;
 36: 
 37: /**
 38:  * Default datasource to use
 39:  *
 40:  * @var DataSource
 41:  */
 42:     protected $_db = null;
 43: 
 44: /**
 45:  * Holds the fixture classes that where instantiated
 46:  *
 47:  * @var array
 48:  */
 49:     protected $_loaded = array();
 50: 
 51: /**
 52:  * Holds the fixture classes that where instantiated indexed by class name
 53:  *
 54:  * @var array
 55:  */
 56:     protected $_fixtureMap = array();
 57: 
 58: /**
 59:  * Inspects the test to look for unloaded fixtures and loads them
 60:  *
 61:  * @param CakeTestCase $test the test case to inspect
 62:  * @return void
 63:  */
 64:     public function fixturize($test) {
 65:         if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) {
 66:             $test->db = $this->_db;
 67:             return;
 68:         }
 69:         $this->_initDb();
 70:         $test->db = $this->_db;
 71:         if (!is_array($test->fixtures)) {
 72:             $test->fixtures = array_map('trim', explode(',', $test->fixtures));
 73:         }
 74:         if (isset($test->fixtures)) {
 75:             $this->_loadFixtures($test->fixtures);
 76:         }
 77: 
 78:         $this->_processed[get_class($test)] = true;
 79:     }
 80: 
 81: /**
 82:  * Initializes this class with a DataSource object to use as default for all fixtures
 83:  *
 84:  * @return void
 85:  */
 86:     protected function _initDb() {
 87:         if ($this->_initialized) {
 88:             return;
 89:         }
 90:         $db = ConnectionManager::getDataSource('test');
 91:         $db->cacheSources = false;
 92:         $this->_db = $db;
 93:         ClassRegistry::config(array('ds' => 'test'));
 94:         $this->_initialized = true;
 95:     }
 96: 
 97: /**
 98:  * Looks for fixture files and instantiates the classes accordingly
 99:  *
100:  * @param array $fixtures the fixture names to load using the notation {type}.{name}
101:  * @return void
102:  */
103:     protected function _loadFixtures($fixtures) {
104:         foreach ($fixtures as $index => $fixture) {
105:             $fixtureFile = null;
106:             $fixtureIndex = $fixture;
107:             if (isset($this->_loaded[$fixture])) {
108:                 continue;
109:             }
110: 
111:             if (strpos($fixture, 'core.') === 0) {
112:                 $fixture = substr($fixture, strlen('core.'));
113:                 $fixturePaths[] = CAKE . 'Test' . DS . 'Fixture';
114:             } elseif (strpos($fixture, 'app.') === 0) {
115:                 $fixture = substr($fixture, strlen('app.'));
116:                 $fixturePaths = array(
117:                     TESTS . 'Fixture'
118:                 );
119:             } elseif (strpos($fixture, 'plugin.') === 0) {
120:                 $parts = explode('.', $fixture, 3);
121:                 $pluginName = $parts[1];
122:                 $fixture = $parts[2];
123:                 $fixturePaths = array(
124:                     CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture',
125:                     TESTS . 'Fixture'
126:                 );
127:             } else {
128:                 $fixturePaths = array(
129:                     TESTS . 'Fixture',
130:                     CAKE  . 'Test' . DS . 'Fixture'
131:                 );
132:             }
133: 
134:             foreach ($fixturePaths as $path) {
135:                 $className = Inflector::camelize($fixture);
136:                 if (is_readable($path . DS . $className . 'Fixture.php')) {
137:                     $fixtureFile = $path . DS . $className . 'Fixture.php';
138:                     require_once($fixtureFile);
139:                     $fixtureClass = $className . 'Fixture';
140:                     $this->_loaded[$fixtureIndex] = new $fixtureClass($this->_db);
141:                     $this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex];
142:                     break;
143:                 }
144:             }
145:         }
146:     }
147: 
148: /**
149:  * Runs the drop and create commands on the fixtures if necessary.
150:  *
151:  * @param CakeTestFixture $fixture the fixture object to create
152:  * @param DataSource $db the datasource instance to use
153:  * @param boolean $drop whether drop the fixture if it is already created or not
154:  * @return void
155:  */
156:     protected function _setupTable($fixture, $db = null, $drop = true) {
157:         if (!$db) {
158:             $db = $this->_db;
159:         }
160:         if (!empty($fixture->created) && $fixture->created == $db->configKeyName) {
161:             return;
162:         }
163: 
164:         $sources = $db->listSources();
165:         $table = $db->config['prefix'] . $fixture->table;
166: 
167:         if ($drop && in_array($table, $sources)) {
168:             $fixture->drop($db);
169:             $fixture->create($db);
170:             $fixture->created = $db->configKeyName;
171:         } elseif (!in_array($table, $sources)) {
172:             $fixture->create($db);
173:             $fixture->created = $db->configKeyName;
174:         }
175:     }
176: 
177: /**
178:  * Crates the fixtures tables and inserts data on them
179:  *
180:  * @param CakeTestCase $test the test to inspect for fixture loading
181:  * @return void
182:  */
183:     public function load(CakeTestCase $test) {
184:         if (empty($test->fixtures)) {
185:             return;
186:         }
187:         $fixtures = $test->fixtures;
188:         if (empty($fixtures) || $test->autoFixtures == false) {
189:             return;
190:         }
191: 
192:         $test->db->begin();
193:         foreach ($fixtures as $f) {
194:             if (!empty($this->_loaded[$f])) {
195:                 $fixture = $this->_loaded[$f];
196:                 $this->_setupTable($fixture, $test->db, $test->dropTables);
197:                 $fixture->insert($test->db);
198:             }
199:         }
200:         $test->db->commit();
201:     }
202: 
203: /**
204:  * Truncates the fixtures tables
205:  *
206:  * @param CakeTestCase $test the test to inspect for fixture unloading
207:  * @return void
208:  */
209:     public function unload(CakeTestCase $test) {
210:         $fixtures = !empty($test->fixtures) ? $test->fixtures : array();
211:         foreach (array_reverse($fixtures) as $f) {
212:             if (isset($this->_loaded[$f])) {
213:                 $fixture = $this->_loaded[$f];
214:                 if (!empty($fixture->created)) {
215:                     $fixture->truncate($test->db);
216:                 }
217:             }
218:         }
219:     }
220: 
221: /**
222:  * Truncates the fixtures tables
223:  *
224:  * @param CakeTestCase $test the test to inspect for fixture unloading
225:  * @return void
226:  * @throws UnexpectedValueException if $name is not a previously loaded class
227:  */
228:     public function loadSingle($name, $db = null) {
229:         $name .= 'Fixture';
230:         if (isset($this->_fixtureMap[$name])) {
231:             if (!$db) {
232:                 $db = $this->_db;
233:             }
234:             $fixture = $this->_fixtureMap[$name];
235:             $this->_setupTable($fixture, $db);
236:             $fixture->truncate($db);
237:             $fixture->insert($db);
238:         } else {
239:             throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s not found', $name));
240:         }
241:     }
242: 
243: /**
244:  * Drop all fixture tables loaded by this class
245:  *
246:  * @return void
247:  */
248:     public function shutDown() {
249:         foreach ($this->_loaded as $fixture) {
250:             if (!empty($fixture->created)) {
251:                 $fixture->drop($this->_db);
252:             }
253:         }
254:     }
255: }
256: 
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