/**
	 * Initialize the fixture.
	 *
	 * @param object	Cake's DBO driver (e.g: DboMysql).
	 * @access public
	 *
	 */
	function init() {
		if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
			$import = array_merge(
			array('connection' => 'default', 'records' => false),
			is_array($this->import) ? $this->import : array('model' => $this->import)
			);

			if (isset($import['model']) && App::import('Model', $import['model'])) {
				ClassRegistry::config(array('ds' => $import['connection']));
				$model =& ClassRegistry::init($import['model']);
				$db =& ConnectionManager::getDataSource($model->useDbConfig);
				$db->cacheSources = false;
				$this->fields = $model->schema(true);
				$this->fields[$model->primaryKey]['key'] = 'primary';
				$this->table = $db->fullTableName($model, false);
				ClassRegistry::config(array('ds' => 'test_suite'));
				ClassRegistry::flush();
			} elseif (isset($import['table'])) {
				$model =& new Model(null, $import['table'], $import['connection']);
				$db =& ConnectionManager::getDataSource($import['connection']);
				$db->cacheSources = false;
				$model->useDbConfig = $import['connection'];
				$model->name = Inflector::camelize(Inflector::singularize($import['table']));
				$model->table = $import['table'];
				$model->tablePrefix = $db->config['prefix'];
				$this->fields = $model->schema(true);
				ClassRegistry::flush();
			}

			if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
				$this->table = str_replace($db->config['prefix'], '', $this->table);
			}

			if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
				$this->records = array();
				$query = array(
					'fields' => $db->fields($model, null, array_keys($this->fields)),
					'table' => $db->fullTableName($model),
					'alias' => $model->alias,
					'conditions' => array(),
					'order' => null,
					'limit' => null,
					'group' => null
				);
				$records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);

				if ($records !== false && !empty($records)) {
					$this->records = Set::extract($records, '{n}.' . $model->alias);
				}
			}
		}

		if (!isset($this->table)) {
			$this->table = Inflector::underscore(Inflector::pluralize($this->name));
		}

		if (!isset($this->primaryKey) && isset($this->fields['id'])) {
			$this->primaryKey = 'id';
		}
	}
Beispiel #2
0
 static function configure()
 {
     if (empty($_COOKIE['selenium'])) {
         return;
     }
     $cookie = $_COOKIE['selenium'];
     App::import('Model', 'ConnectionManager', false);
     ClassRegistry::flush();
     Configure::write('Cache.disable', true);
     $testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
     $_prefix = null;
     if ($testDbAvailable) {
         // Try for test DB
         restore_error_handler();
         @($db =& ConnectionManager::getDataSource('test'));
         set_error_handler('simpleTestErrorHandler');
         $testDbAvailable = $db->isConnected();
     }
     // Try for default DB
     if (!$testDbAvailable) {
         $db =& ConnectionManager::getDataSource('default');
     }
     $_prefix = $db->config['prefix'];
     $db->config['prefix'] = $cookie . '_';
     ConnectionManager::create('test_suite', $db->config);
     $db->config['prefix'] = $_prefix;
     // Get db connection
     $db =& ConnectionManager::getDataSource('test_suite');
     $db->cacheSources = false;
     ClassRegistry::config(array('ds' => 'test_suite'));
 }
Beispiel #3
0
 /**
  * Initializes this class with a DataSource object to use as default for all fixtures
  *
  * @return void
  */
 protected function _initDb()
 {
     if ($this->_initialized) {
         return;
     }
     $db = ConnectionManager::getDataSource('test');
     $this->_db = $db;
     ClassRegistry::config(array('ds' => 'test'));
     $this->_initialized = true;
 }
 /**
  * @return CakeTestSuite
  */
 public static function suite()
 {
     ClassRegistry::config(['ds' => 'test']);
     $suite = new CakeTestSuite('Working Tests');
     $suite->addTestFile(TESTS . 'Case/WorldPayTest.php');
     //        $suite->addTestFile(TESTS.'Case/NetSuiteTest.php');
     //        $suite->addTestFile(TESTS.'Case/BackOrderRestockTest.php');
     $suite->addTestFile(TESTS . 'Case/QueueTasksTest.php');
     //        $suite->addTestFile(TESTS.'Case/PresenterRollupTest.php');
     $suite->addTestFile(TESTS . 'Case/OrderNotificationsTest.php');
     $suite->addTestFile(TESTS . 'Case/ShoppingCartTest.php');
     $suite->addTestFile(TESTS . 'Case/OrdersTest.php');
     $suite->addTestFile(TESTS . 'Case/SnapDirectTest.php');
     $suite->addTestFile(TESTS . 'Case/InventoryTest.php');
     return $suite;
 }
 /**
  * Initialize the fixture.
  *
  * @param object	Cake's DBO driver (e.g: DboMysql).
  * @access public
  *
  */
 function init()
 {
     if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
         $import = array();
         if (is_string($this->import) || is_array($this->import) && isset($this->import['model'])) {
             $import = array_merge(array('records' => false), ife(is_array($this->import), $this->import, array()));
             $import['model'] = ife(is_array($this->import), $this->import['model'], $this->import);
         } elseif (isset($this->import['table'])) {
             $import = array_merge(array('connection' => 'default', 'records' => false), $this->import);
         }
         if (isset($import['model']) && (class_exists($import['model']) || App::import('Model', $import['model']))) {
             $connection = isset($import['connection']) ? $import['connection'] : 'test_suite';
             ClassRegistry::config(array('ds' => $connection));
             $model =& ClassRegistry::init($import['model']);
             $db =& ConnectionManager::getDataSource($model->useDbConfig);
             $db->cacheSources = false;
             $this->fields = $model->schema(true);
             $this->fields[$model->primaryKey]['key'] = 'primary';
         } elseif (isset($import['table'])) {
             $model =& new Model(null, $import['table'], $import['connection']);
             $db =& ConnectionManager::getDataSource($import['connection']);
             $db->cacheSources = false;
             $model->name = Inflector::camelize(Inflector::singularize($import['table']));
             $model->table = $import['table'];
             $model->tablePrefix = $db->config['prefix'];
             $this->fields = $model->schema(true);
         }
         if ($import['records'] !== false && isset($model) && isset($db)) {
             $this->records = array();
             $query = array('fields' => array_keys($this->fields), 'table' => $db->name($model->table), 'alias' => $model->alias, 'conditions' => array(), 'order' => null, 'limit' => null, 'group' => null);
             foreach ($query['fields'] as $index => $field) {
                 $query['fields'][$index] = $db->name($query['alias']) . '.' . $db->name($field);
             }
             $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
             if ($records !== false && !empty($records)) {
                 $this->records = Set::extract($records, '{n}.' . $model->alias);
             }
         }
     }
     if (!isset($this->table)) {
         $this->table = Inflector::underscore(Inflector::pluralize($this->name));
     }
     if (!isset($this->primaryKey) && isset($this->fields['id'])) {
         $this->primaryKey = 'id';
     }
 }
Beispiel #6
0
 protected function _initDb()
 {
     $testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
     if ($testDbAvailable) {
         // Try for test DB
         restore_error_handler();
         $db = ConnectionManager::getDataSource('test');
         $testDbAvailable = $db->isConnected();
     }
     // Try for default DB
     if (!$testDbAvailable) {
         $db = ConnectionManager::getDataSource('default');
         $db->config['prefix'] = 'test_suite_';
     }
     ConnectionManager::create('test_suite', $db->config);
     ClassRegistry::config(array('ds' => 'test_suite'));
 }
 /**
  * Inspects the test to look for unloaded fixtures and loads them
  *
  * @param CakeTestCase $test the test case to inspect
  * @return void
  */
 public function fixturize($test)
 {
     if (!$this->_initialized) {
         ClassRegistry::config(array('ds' => 'test', 'testing' => true));
     }
     if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) {
         $test->db = $this->_db;
         return;
     }
     $this->_initDb();
     $test->db = $this->_db;
     if (!is_array($test->fixtures)) {
         $test->fixtures = array_map('trim', explode(',', $test->fixtures));
     }
     if (isset($test->fixtures)) {
         $this->_loadFixtures($test->fixtures);
     }
     $this->_processed[get_class($test)] = true;
 }
 /**
  * Bake All the controllers at once. Will only bake controllers for models that exist.
  *
  * @return void
  */
 public function all()
 {
     $this->interactive = false;
     $this->listAll($this->connection, false);
     ClassRegistry::config('Model', array('ds' => $this->connection));
     $unitTestExists = $this->_checkUnitTest();
     $admin = false;
     if (!empty($this->params['admin'])) {
         $admin = $this->Project->getPrefix();
     }
     $controllersCreated = 0;
     foreach ($this->__tables as $table) {
         $model = $this->_modelName($table);
         $controller = $this->_controllerName($model);
         App::uses($model, 'Model');
         if (class_exists($model)) {
             $actions = $this->bakeActions($controller);
             if ($admin) {
                 $this->out(__d('cake_console', 'Adding %s methods', $admin));
                 $actions .= "\n" . $this->bakeActions($controller, $admin);
             }
             if ($this->bake($controller, $actions) && $unitTestExists) {
                 $this->bakeTest($controller);
             }
             $controllersCreated++;
         }
     }
     if (!$controllersCreated) {
         $this->out(__d('cake_console', 'No Controllers were baked, Models need to exist before Controllers can be baked.'));
     }
 }
Beispiel #9
0
 /**
  * Mock a model, maintain fixtures and table association
  *
  * @param string $model
  * @param mixed $methods
  * @param array $config
  * @throws MissingModelException
  * @return Model
  */
 public function getMockForModel($model, $methods = array(), $config = array())
 {
     $config += ClassRegistry::config('Model');
     list($plugin, $name) = pluginSplit($model, true);
     App::uses($name, $plugin . 'Model');
     $config = array_merge((array) $config, array('name' => $name));
     if (!class_exists($name)) {
         throw new MissingModelException(array($model));
     }
     $mock = $this->getMock($name, $methods, array($config));
     ClassRegistry::removeObject($name);
     ClassRegistry::addObject($name, $mock);
     return $mock;
 }
 /**
  * Bake All the controllers at once.  Will only bake controllers for models that exist.
  *
  * @return void
  */
 public function all()
 {
     $this->interactive = false;
     $this->listAll($this->connection, false);
     ClassRegistry::config('Model', array('ds' => $this->connection));
     $unitTestExists = $this->_checkUnitTest();
     foreach ($this->__tables as $table) {
         $model = $this->_modelName($table);
         $controller = $this->_controllerName($model);
         App::uses($model, 'Model');
         if (class_exists($model)) {
             $actions = $this->bakeActions($controller);
             if ($this->bake($controller, $actions) && $unitTestExists) {
                 $this->bakeTest($controller);
             }
         }
     }
 }
Beispiel #11
0
 /**
  * Bake All the controllers at once.  Will only bake controllers for models that exist.
  *
  * @access public
  * @return void
  */
 function all()
 {
     $this->interactive = false;
     $this->listAll($this->connection, false);
     ClassRegistry::config('Model', array('ds' => $this->connection));
     $unitTestExists = $this->_checkUnitTest();
     foreach ($this->__tables as $table) {
         $model = $this->_modelName($table);
         $admin = $this->Project->getPrefix();
         $controller = $this->_controllerName($model);
         if (App::import('Model', $model)) {
             $actions = $this->bakeActions($controller, $admin);
             //                                $actions .= "\n" . $this->bakeActions($controller, 'co_');
             //                                $actions .= "\n" . $this->bakeActions($controller, 'eo_');
             if ($this->bake($controller, $actions) && $unitTestExists) {
                 $this->bakeTest($controller);
             }
         }
     }
 }
Beispiel #12
0
 /**
  * testArrayToArrayHasAndBelongsToMany
  *
  * @return void
  * @access public
  */
 function testArrayToArrayHasAndBelongsToMany()
 {
     ClassRegistry::config(array());
     $model = ClassRegistry::init('ArrayModel');
     $model->unBindModel(array('hasOne' => array('Relate')), false);
     $model->bindModel(array('hasAndBelongsToMany' => array('Relate' => array('className' => 'ArrayModel', 'with' => 'ArraysRelateModel', 'associationForeignKey' => 'relate_id'))), false);
     $result = $model->find('all', array('recursive' => 1));
     $expected = array(array('ArrayModel' => array('id' => 1, 'name' => 'USA', 'relate_id' => 1), 'Relate' => array(array('id' => 1, 'name' => 'USA', 'relate_id' => 1), array('id' => 2, 'name' => 'Brazil', 'relate_id' => 1), array('id' => 3, 'name' => 'Germany', 'relate_id' => 2))), array('ArrayModel' => array('id' => 2, 'name' => 'Brazil', 'relate_id' => 1), 'Relate' => array(array('id' => 1, 'name' => 'USA', 'relate_id' => 1), array('id' => 3, 'name' => 'Germany', 'relate_id' => 2))), array('ArrayModel' => array('id' => 3, 'name' => 'Germany', 'relate_id' => 2), 'Relate' => array(array('id' => 1, 'name' => 'USA', 'relate_id' => 1), array('id' => 2, 'name' => 'Brazil', 'relate_id' => 1))));
     $this->assertEqual($result, $expected);
 }
Beispiel #13
0
 /**
  * バージョンコントロールをします。
  * クライアントはヘッダにAPIVersionを含めることで使用するバージョンを指定できます。
  * 指定されなかった場合、ApiComponent::$recetnVersionが使用されます。
  *
  * @param Controller $controller
  * @return void
  */
 protected function _handleVersion($controller)
 {
     $modelConfig = ClassRegistry::config('Model');
     if (!empty($modelConfig['testing'])) {
         $this->version = $this->recentVersion;
         if ($version = Configure::read('TEST_API_VERSION')) {
             $this->version = $version;
         }
         return;
     }
     $this->version = $this->currentVersion;
     if ($version = $controller->request->header('APIVersion')) {
         $this->version = $version;
     }
 }
Beispiel #14
0
 /**
  * Mock a model, maintain fixtures and table association
  *
  * @param string $model   The model to get a mock for.
  * @param mixed  $methods The list of methods to mock
  * @param array  $config  The config data for the mock's constructor.
  *
  * @throws MissingModelException
  * @return Model
  */
 public function getMockForModel($model, $methods = array(), $config = array())
 {
     $config += ClassRegistry::config('Model');
     list($plugin, $name) = pluginSplit($model, TRUE);
     App::uses($name, $plugin . 'Model');
     $config = array_merge((array) $config, array('name' => $name));
     unset($config['ds']);
     if (!class_exists($name)) {
         throw new MissingModelException(array($model));
     }
     $mock = $this->getMock($name, $methods, array($config));
     $availableDs = array_keys(ConnectionManager::enumConnectionObjects());
     if ($mock->useDbConfig !== 'test' && in_array('test_' . $mock->useDbConfig, $availableDs)) {
         $mock->setDataSource('test_' . $mock->useDbConfig);
     } else {
         $mock->useDbConfig = 'test';
         $mock->setDataSource('test');
     }
     ClassRegistry::removeObject($name);
     ClassRegistry::addObject($name, $mock);
     return $mock;
 }
 /**
  * Generates a mocked controller and mocks any classes passed to `$mocks`. By
  * default, `_stop()` is stubbed as is sending the response headers, so to not
  * interfere with testing.
  *
  * ### Mocks:
  *
  * - `methods` Methods to mock on the controller. `_stop()` is mocked by default
  * - `models` Models to mock. Models are added to the ClassRegistry so they any
  *   time they are instantiated the mock will be created. Pass as key value pairs
  *   with the value being specific methods on the model to mock. If `true` or
  *   no value is passed, the entire model will be mocked.
  * - `components` Components to mock. Components are only mocked on this controller
  *   and not within each other (i.e., components on components)
  *
  * @param string $controller Controller name
  * @param array $mocks List of classes and methods to mock
  * @return Controller Mocked controller
  * @throws MissingControllerException When controllers could not be created.
  * @throws MissingComponentException When components could not be created.
  */
 public function generate($controller, $mocks = array())
 {
     list($plugin, $controller) = pluginSplit($controller);
     if ($plugin) {
         App::uses($plugin . 'AppController', $plugin . '.Controller');
         $plugin .= '.';
     }
     App::uses($controller . 'Controller', $plugin . 'Controller');
     if (!class_exists($controller . 'Controller')) {
         throw new MissingControllerException(array('class' => $controller . 'Controller', 'plugin' => substr($plugin, 0, -1)));
     }
     ClassRegistry::flush();
     $mocks = array_merge_recursive(array('methods' => array('_stop'), 'models' => array(), 'components' => array()), (array) $mocks);
     list($plugin, $name) = pluginSplit($controller);
     $controllerObj = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
     $controllerObj->name = $name;
     $request = $this->getMock('CakeRequest');
     $response = $this->getMock('CakeResponse', array('_sendHeader'));
     $controllerObj->__construct($request, $response);
     $controllerObj->Components->setController($controllerObj);
     $config = ClassRegistry::config('Model');
     foreach ($mocks['models'] as $model => $methods) {
         if (is_string($methods)) {
             $model = $methods;
             $methods = true;
         }
         if ($methods === true) {
             $methods = array();
         }
         $this->getMockForModel($model, $methods, $config);
     }
     foreach ($mocks['components'] as $component => $methods) {
         if (is_string($methods)) {
             $component = $methods;
             $methods = true;
         }
         if ($methods === true) {
             $methods = array();
         }
         list($plugin, $name) = pluginSplit($component, true);
         $componentClass = $name . 'Component';
         App::uses($componentClass, $plugin . 'Controller/Component');
         if (!class_exists($componentClass)) {
             throw new MissingComponentException(array('class' => $componentClass));
         }
         $config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
         $componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
         $controllerObj->Components->set($name, $componentObj);
         $controllerObj->Components->enable($name);
     }
     $controllerObj->constructClasses();
     $this->_dirtyController = false;
     $this->controller = $controllerObj;
     return $this->controller;
 }
Beispiel #16
0
 /**
  * Tests prefixed datasource names for test purposes
  *
  */
 public function testPrefixedTestDatasource()
 {
     ClassRegistry::config(array('testing' => true));
     $Model = ClassRegistry::init('RegisterPrefixedDs');
     $this->assertEquals('test', $Model->useDbConfig);
     ClassRegistry::removeObject('RegisterPrefixedDs');
     $testConfig = ConnectionManager::getDataSource('test')->config;
     ConnectionManager::create('test_doesnotexist', $testConfig);
     $Model = ClassRegistry::init('RegisterArticle');
     $this->assertEquals('test', $Model->useDbConfig);
     $Model = ClassRegistry::init('RegisterPrefixedDs');
     $this->assertEquals('test_doesnotexist', $Model->useDbConfig);
 }
 /**
  * teardown
  *
  * @access public
  * @return void
  */
 public function teardown()
 {
     App::build();
     ClassRegistry::config('Model', $this->_CRConfig);
     $_SERVER = $this->_server;
 }
Beispiel #18
0
 /**
  * test _handleVersion() method
  *
  * @test
  */
 public function _handleVersion()
 {
     $this->generateComponent();
     $this->Api->dispatchMethod('_handleVersion', [$this->controller]);
     $this->assertSame('1.0', $this->Api->version);
     $this->generateComponent();
     Configure::write('TEST_API_VERSION', '2.1');
     $this->Api->dispatchMethod('_handleVersion', [$this->controller]);
     $this->assertSame('2.1', $this->Api->version);
     $modelConfig = ClassRegistry::config('Model');
     ClassRegistry::config('Model', null);
     $this->generateComponent();
     $this->Api->dispatchMethod('_handleVersion', [$this->controller]);
     $this->assertSame('1.0', $this->Api->version);
     $this->generateComponent(['mocks' => ['request' => ['header']]]);
     $this->request->staticExpects($this->once())->method('header')->with('APIVersion')->will($this->returnValue('3.5'));
     $this->Api->dispatchMethod('_handleVersion', [$this->controller]);
     $this->assertSame('3.5', $this->Api->version);
     ClassRegistry::config('Model', $modelConfig);
     Configure::delete('TEST_API_VERSION');
 }
Beispiel #19
0
 /**
  * Initializes this class with a DataSource object to use as default for all fixtures
  *
  * @return void
  */
 protected function _initDb()
 {
     if ($this->_initialized) {
         return;
     }
     $testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
     $_prefix = null;
     if ($testDbAvailable) {
         // Try for test DB
         @($db = ConnectionManager::getDataSource('test'));
         $testDbAvailable = $db->isConnected();
     } else {
         throw new MissingConnectionException(__('You need to create a $test datasource connection to start using fixtures'));
     }
     if (!$testDbAvailable) {
         throw new MissingConnectionException(__('Unable to connect to the $test datasource'));
     }
     $this->_db = $db;
     ClassRegistry::config(array('ds' => 'test'));
     $this->_initialized = true;
 }
 public function setUp()
 {
     parent::setUp();
     ClassRegistry::config(array('ds' => null));
     $this->Twim = ClassRegistry::init('TestTwimAppModel');
 }
 function start()
 {
     parent::start();
     ClassRegistry::config(array('table' => false));
     $this->__loadController();
 }
 /**
  * testBelongsToWithoutForeignKey
  *
  * @return void
  * @access public
  */
 function testBelongsToWithoutForeignKey()
 {
     ClassRegistry::config(array());
     $model = ClassRegistry::init('UserModel');
     $result = $model->find('all', array('fields' => array('UserModel.id', 'UserModel.name'), 'recursive' => 0));
     $expected = array(array('UserModel' => array('id' => 1, 'name' => 'User 1')), array('UserModel' => array('id' => 2, 'name' => 'User 2')), array('UserModel' => array('id' => 3, 'name' => 'User 3')), array('UserModel' => array('id' => 4, 'name' => 'User 4')));
     $this->assertEqual($result, $expected);
 }
Beispiel #23
0
 /**
  * Initialize the fixture.
  *
  * @return void
  * @throws MissingModelException Whe importing from a model that does not exist.
  */
 public function init()
 {
     if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
         $import = array_merge(array('connection' => 'default', 'records' => false), is_array($this->import) ? $this->import : array('model' => $this->import));
         $this->Schema->connection = $import['connection'];
         if (isset($import['model'])) {
             list($plugin, $modelClass) = pluginSplit($import['model'], true);
             App::uses($modelClass, $plugin . 'Model');
             if (!class_exists($modelClass)) {
                 throw new MissingModelException(array('class' => $modelClass));
             }
             $model = new $modelClass(null, null, $import['connection']);
             $db = $model->getDataSource();
             if (empty($model->tablePrefix)) {
                 $model->tablePrefix = $db->config['prefix'];
             }
             $this->fields = $model->schema(true);
             $this->fields[$model->primaryKey]['key'] = 'primary';
             $this->table = $db->fullTableName($model, false, false);
             $this->primaryKey = $model->primaryKey;
             ClassRegistry::config(array('ds' => 'test'));
             ClassRegistry::flush();
         } elseif (isset($import['table'])) {
             $model = new Model(null, $import['table'], $import['connection']);
             $db = ConnectionManager::getDataSource($import['connection']);
             $db->cacheSources = false;
             $model->useDbConfig = $import['connection'];
             $model->name = Inflector::camelize(Inflector::singularize($import['table']));
             $model->table = $import['table'];
             $model->tablePrefix = $db->config['prefix'];
             $this->fields = $model->schema(true);
             $this->primaryKey = $model->primaryKey;
             ClassRegistry::flush();
             // Start Zuha Edit //
         } elseif (isset($import['config'])) {
             list($plugin, $modelClass) = pluginSplit($import['config'], false);
             $fileName = !empty($import['file']) ? $import['file'] : 'schema';
             $filePath = 'Config' . DS . 'Schema' . DS . $fileName . '.php';
             $file = !empty($plugin) ? App::pluginPath($plugin) . $filePath : ROOT . DS . APP_DIR . DS . $filePath;
             $className = !empty($plugin) ? $plugin . 'Schema' : 'AppSchema';
             $className = !empty($import['class']) ? $import['class'] : $className;
             $this->table = !empty($import['uses']) ? $import['uses'] : Inflector::tableize($modelClass);
             if (!file_exists($file)) {
                 throw new Exception(__('Schema file %s missing', $file));
             }
             require_once $file;
             $Schema = new $className();
             $this->fields = $Schema->tables[$this->table];
             ClassRegistry::config(array('ds' => 'test'));
             ClassRegistry::flush();
             $this->primaryKey = $model->primaryKey;
             // End Zuha Edit //
         }
         if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
             $this->table = str_replace($db->config['prefix'], '', $this->table);
         }
         if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
             $this->records = array();
             $query = array('fields' => $db->fields($model, null, array_keys($this->fields)), 'table' => $db->fullTableName($model), 'alias' => $model->alias, 'conditions' => array(), 'order' => null, 'limit' => null, 'group' => null);
             $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
             if ($records !== false && !empty($records)) {
                 $this->records = Hash::extract($records, '{n}.' . $model->alias);
             }
         }
     }
     if (!isset($this->table)) {
         $this->table = Inflector::underscore(Inflector::pluralize($this->name));
     }
     if (!isset($this->primaryKey) && isset($this->fields['id'])) {
         $this->primaryKey = 'id';
     }
 }
Beispiel #24
0
 /**
  * Mock a model, maintain fixtures and table association
  *
  * @param string $model
  * @param mixed $methods
  * @param mixed $config
  * @return Model
  */
 public function getMockForModel($model, $methods = array(), $config = null)
 {
     if (is_null($config)) {
         $config = ClassRegistry::config('Model');
     }
     list($plugin, $name) = pluginSplit($model, true);
     App::uses($name, $plugin . 'Model');
     $config = array_merge((array) $config, array('name' => $name));
     $mock = $this->getMock($name, $methods, array($config));
     ClassRegistry::removeObject($name);
     ClassRegistry::addObject($name, $mock);
     return $mock;
 }
Beispiel #25
0
 /**
  * Generates a mocked controller and mocks any classes passed to `$mocks`. By
  * default, `_stop()` is stubbed as is sending the response headers, so to not
  * interfere with testing.
  * 
  * ### Mocks:
  *
  * - `methods` Methods to mock on the controller. `_stop()` is mocked by default
  * - `models` Models to mock. Models are added to the ClassRegistry so they any
  *   time they are instatiated the mock will be created. Pass as key value pairs
  *   with the value being specific methods on the model to mock. If `true` or
  *   no value is passed, the entire model will be mocked.
  * - `components` Components to mock. Components are only mocked on this controller
  *   and not within each other (i.e., components on components)
  *
  * @param string $controller Controller name
  * @param array $mocks List of classes and methods to mock
  * @return Controller Mocked controller
  */
 public function generate($controller, $mocks = array())
 {
     if (!class_exists($controller . 'Controller') && App::import('Controller', $controller) === false) {
         throw new MissingControllerException(array('controller' => $controller . 'Controller'));
     }
     ClassRegistry::flush();
     $mocks = array_merge_recursive(array('methods' => array('_stop'), 'models' => array(), 'components' => array()), (array) $mocks);
     list($plugin, $name) = pluginSplit($controller);
     $_controller = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
     $_controller->name = $name;
     $_controller->__construct();
     $config = ClassRegistry::config('Model');
     foreach ($mocks['models'] as $model => $methods) {
         if (is_string($methods)) {
             $model = $methods;
             $methods = true;
         }
         if ($methods === true) {
             $methods = array();
         }
         ClassRegistry::init($model);
         list($plugin, $name) = pluginSplit($model);
         $config = array_merge((array) $config, array('name' => $model));
         $_model = $this->getMock($name, $methods, array($config));
         ClassRegistry::removeObject($name);
         ClassRegistry::addObject($name, $_model);
     }
     foreach ($mocks['components'] as $component => $methods) {
         if (is_string($methods)) {
             $component = $methods;
             $methods = true;
         }
         if ($methods === true) {
             $methods = array();
         }
         list($plugin, $name) = pluginSplit($component);
         if (!App::import('Component', $component)) {
             throw new MissingComponentFileException(array('file' => Inflector::underscore($name) . '.php', 'class' => $name . 'Component'));
         }
         $_component = $this->getMock($name . 'Component', $methods, array(), '', false);
         $_controller->Components->set($name, $_component);
     }
     $_controller->constructClasses();
     $this->controller = $_controller;
     return $this->controller;
 }
Beispiel #26
0
 /**
  * Initialize DB connection.
  *
  * @return void
  * @access protected
  */
 function _initDb()
 {
     $testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
     $_prefix = null;
     if ($testDbAvailable) {
         // Try for test DB
         restore_error_handler();
         @($db =& ConnectionManager::getDataSource('test'));
         set_error_handler('simpleTestErrorHandler');
         $testDbAvailable = $db->isConnected();
     }
     // Try for default DB
     if (!$testDbAvailable) {
         $db =& ConnectionManager::getDataSource('default');
         $_prefix = $db->config['prefix'];
         $db->config['prefix'] = 'test_suite_';
     }
     ConnectionManager::create('test_suite', $db->config);
     $db->config['prefix'] = $_prefix;
     // Get db connection
     $this->db =& ConnectionManager::getDataSource('test_suite');
     $this->db->cacheSources = false;
     ClassRegistry::config(array('ds' => 'test_suite'));
 }
 /**
  * Initialize the fixture.
  *
  * @return void
  * @throws MissingModelException Whe importing from a model that does not exist.
  */
 public function init()
 {
     if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
         $import = array_merge(array('connection' => 'default', 'records' => FALSE), is_array($this->import) ? $this->import : array('model' => $this->import));
         $this->Schema->connection = $import['connection'];
         if (isset($import['model'])) {
             list($plugin, $modelClass) = pluginSplit($import['model'], TRUE);
             App::uses($modelClass, $plugin . 'Model');
             if (!class_exists($modelClass)) {
                 throw new MissingModelException(array('class' => $modelClass));
             }
             $model = new $modelClass(NULL, NULL, $import['connection']);
             $db = $model->getDataSource();
             if (empty($model->tablePrefix)) {
                 $model->tablePrefix = $db->config['prefix'];
             }
             $this->fields = $model->schema(TRUE);
             $this->fields[$model->primaryKey]['key'] = 'primary';
             $this->table = $db->fullTableName($model, FALSE, FALSE);
             $this->primaryKey = $model->primaryKey;
             ClassRegistry::config(array('ds' => 'test'));
             ClassRegistry::flush();
         } elseif (isset($import['table'])) {
             $model = new Model(NULL, $import['table'], $import['connection']);
             $db = ConnectionManager::getDataSource($import['connection']);
             $db->cacheSources = FALSE;
             $model->useDbConfig = $import['connection'];
             $model->name = Inflector::camelize(Inflector::singularize($import['table']));
             $model->table = $import['table'];
             $model->tablePrefix = $db->config['prefix'];
             $this->fields = $model->schema(TRUE);
             $this->primaryKey = $model->primaryKey;
             ClassRegistry::flush();
         }
         if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
             $this->table = str_replace($db->config['prefix'], '', $this->table);
         }
         if (isset($import['records']) && $import['records'] !== FALSE && isset($model) && isset($db)) {
             $this->records = array();
             $query = array('fields' => $db->fields($model, NULL, array_keys($this->fields)), 'table' => $db->fullTableName($model), 'alias' => $model->alias, 'conditions' => array(), 'order' => NULL, 'limit' => NULL, 'group' => NULL);
             $records = $db->fetchAll($db->buildStatement($query, $model), FALSE, $model->alias);
             if ($records !== FALSE && !empty($records)) {
                 $this->records = Hash::extract($records, '{n}.' . $model->alias);
             }
         }
     }
     if (!isset($this->table)) {
         $this->table = Inflector::underscore(Inflector::pluralize($this->name));
     }
     if (!isset($this->primaryKey) && isset($this->fields['id'])) {
         $this->primaryKey = 'id';
     }
 }