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

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