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:  * 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-2012, 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-2012, 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 (!$this->_initialized) {
 66:             ClassRegistry::config(array('ds' => 'test', 'testing' => true));
 67:         }
 68:         if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) {
 69:             $test->db = $this->_db;
 70:             return;
 71:         }
 72:         $this->_initDb();
 73:         $test->db = $this->_db;
 74:         if (!is_array($test->fixtures)) {
 75:             $test->fixtures = array_map('trim', explode(',', $test->fixtures));
 76:         }
 77:         if (isset($test->fixtures)) {
 78:             $this->_loadFixtures($test->fixtures);
 79:         }
 80: 
 81:         $this->_processed[get_class($test)] = true;
 82:     }
 83: 
 84: /**
 85:  * Initializes this class with a DataSource object to use as default for all fixtures
 86:  *
 87:  * @return void
 88:  */
 89:     protected function _initDb() {
 90:         if ($this->_initialized) {
 91:             return;
 92:         }
 93:         $db = ConnectionManager::getDataSource('test');
 94:         $db->cacheSources = false;
 95:         $this->_db = $db;
 96:         $this->_initialized = true;
 97:     }
 98: 
 99: /**
100:  * Looks for fixture files and instantiates the classes accordingly
101:  *
102:  * @param array $fixtures the fixture names to load using the notation {type}.{name}
103:  * @return void
104:  */
105:     protected function _loadFixtures($fixtures) {
106:         foreach ($fixtures as $index => $fixture) {
107:             $fixtureFile = null;
108:             $fixtureIndex = $fixture;
109:             if (isset($this->_loaded[$fixture])) {
110:                 continue;
111:             }
112: 
113:             if (strpos($fixture, 'core.') === 0) {
114:                 $fixture = substr($fixture, strlen('core.'));
115:                 $fixturePaths[] = CAKE . 'Test' . DS . 'Fixture';
116:             } elseif (strpos($fixture, 'app.') === 0) {
117:                 $fixture = substr($fixture, strlen('app.'));
118:                 $fixturePaths = array(
119:                     TESTS . 'Fixture'
120:                 );
121:             } elseif (strpos($fixture, 'plugin.') === 0) {
122:                 $parts = explode('.', $fixture, 3);
123:                 $pluginName = $parts[1];
124:                 $fixture = $parts[2];
125:                 $fixturePaths = array(
126:                     CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture',
127:                     TESTS . 'Fixture'
128:                 );
129:             } else {
130:                 $fixturePaths = array(
131:                     TESTS . 'Fixture',
132:                     CAKE . 'Test' . DS . 'Fixture'
133:                 );
134:             }
135: 
136:             foreach ($fixturePaths as $path) {
137:                 $className = Inflector::camelize($fixture);
138:                 if (is_readable($path . DS . $className . 'Fixture.php')) {
139:                     $fixtureFile = $path . DS . $className . 'Fixture.php';
140:                     require_once $fixtureFile;
141:                     $fixtureClass = $className . 'Fixture';
142:                     $this->_loaded[$fixtureIndex] = new $fixtureClass();
143:                     $this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex];
144:                     break;
145:                 }
146:             }
147:         }
148:     }
149: 
150: /**
151:  * Runs the drop and create commands on the fixtures if necessary.
152:  *
153:  * @param CakeTestFixture $fixture the fixture object to create
154:  * @param DataSource $db the datasource instance to use
155:  * @param boolean $drop whether drop the fixture if it is already created or not
156:  * @return void
157:  */
158:     protected function _setupTable($fixture, $db = null, $drop = true) {
159:         if (!$db) {
160:             if (!empty($fixture->useDbConfig)) {
161:                 $db = ClassRegistry::getDataSource($fixture->useDbConfig);
162:             } else {
163:                 $db = $this->_db;
164:             }
165:         }
166:         if (!empty($fixture->created) && in_array($db->configKeyName, $fixture->created)) {
167:             return;
168:         }
169: 
170:         $sources = $db->listSources();
171:         $table = $db->config['prefix'] . $fixture->table;
172:         $exists = in_array($table, $sources);
173: 
174:         if ($drop && $exists) {
175:             $fixture->drop($db);
176:             $fixture->create($db);
177:         } elseif (!$exists) {
178:             $fixture->create($db);
179:         } else {
180:             $fixture->created[] = $db->configKeyName;
181:         }
182:     }
183: 
184: /**
185:  * Creates the fixtures tables and inserts data on them.
186:  *
187:  * @param CakeTestCase $test the test to inspect for fixture loading
188:  * @return void
189:  */
190:     public function load(CakeTestCase $test) {
191:         if (empty($test->fixtures)) {
192:             return;
193:         }
194:         $fixtures = $test->fixtures;
195:         if (empty($fixtures) || $test->autoFixtures == false) {
196:             return;
197:         }
198: 
199:         $test->db->begin();
200:         foreach ($fixtures as $f) {
201:             if (!empty($this->_loaded[$f])) {
202:                 $fixture = $this->_loaded[$f];
203:                 $db = ConnectionManager::getDataSource($fixture->useDbConfig);
204:                 $this->_setupTable($fixture, $db, $test->dropTables);
205:                 $fixture->insert($db);
206:             }
207:         }
208:         $test->db->commit();
209:     }
210: 
211: /**
212:  * Truncates the fixtures tables
213:  *
214:  * @param CakeTestCase $test the test to inspect for fixture unloading
215:  * @return void
216:  */
217:     public function unload(CakeTestCase $test) {
218:         $fixtures = !empty($test->fixtures) ? $test->fixtures : array();
219:         foreach (array_reverse($fixtures) as $f) {
220:             if (isset($this->_loaded[$f])) {
221:                 $fixture = $this->_loaded[$f];
222:                 if (!empty($fixture->created)) {
223:                     foreach ($fixture->created as $ds) {
224:                         $db = ConnectionManager::getDataSource($ds);
225:                         $fixture->truncate($db);
226:                     }
227:                 }
228:             }
229:         }
230:     }
231: 
232: /**
233:  * Creates a single fixture table and loads data into it.
234:  *
235:  * @param string $name of the fixture
236:  * @param DataSource $db DataSource instance or leave null to get DataSource from the fixture
237:  * @return void
238:  * @throws UnexpectedValueException if $name is not a previously loaded class
239:  */
240:     public function loadSingle($name, $db = null) {
241:         $name .= 'Fixture';
242:         if (isset($this->_fixtureMap[$name])) {
243:             $fixture = $this->_fixtureMap[$name];
244:             if (!$db) {
245:                 $db = ConnectionManager::getDataSource($fixture->useDbConfig);
246:             }
247:             $this->_setupTable($fixture, $db);
248:             $fixture->truncate($db);
249:             $fixture->insert($db);
250:         } else {
251:             throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s not found', $name));
252:         }
253:     }
254: 
255: /**
256:  * Drop all fixture tables loaded by this class
257:  *
258:  * @return void
259:  */
260:     public function shutDown() {
261:         foreach ($this->_loaded as $fixture) {
262:             if (!empty($fixture->created)) {
263:                 foreach ($fixture->created as $ds) {
264:                     $db = ConnectionManager::getDataSource($ds);
265:                     $fixture->drop($db);
266:                 }
267:             }
268:         }
269:     }
270: 
271: }
272: 
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