/**
  * test find('list') method
  *
  * @return void
  */
 public function testGenerateFindList()
 {
     $this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User', 'Comment');
     $TestModel = new Article();
     $TestModel->displayField = 'title';
     $result = $TestModel->find('list', array('order' => 'Article.title ASC'));
     $expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article');
     $this->assertEquals($expected, $result);
     $db = ConnectionManager::getDataSource('test');
     if ($db instanceof Mysql) {
         $result = $TestModel->find('list', array('order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC')));
         $expected = array(1 => 'First Article', 3 => 'Third Article', 2 => 'Second Article');
         $this->assertEquals($expected, $result);
     }
     $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC', 'fields' => array('id', 'title'))), '{n}.Article.id', '{n}.Article.title');
     $expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article');
     $this->assertEquals($expected, $result);
     $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC')), '{n}.Article.id', '{n}.Article');
     $expected = array(1 => array('id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'), 2 => array('id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'), 3 => array('id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'));
     $this->assertEquals($expected, $result);
     $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC')), '{n}.Article.id', '{n}.Article', '{n}.Article.user_id');
     $expected = array(1 => array(1 => array('id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'), 3 => array('id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')), 3 => array(2 => array('id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')));
     $this->assertEquals($expected, $result);
     $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC', 'fields' => array('id', 'title', 'user_id'))), '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id');
     $expected = array(1 => array(1 => 'First Article', 3 => 'Third Article'), 3 => array(2 => 'Second Article'));
     $this->assertEquals($expected, $result);
     $TestModel = new Apple();
     $expected = array(1 => 'Red Apple 1', 2 => 'Bright Red Apple', 3 => 'green blue', 4 => 'Test Name', 5 => 'Blue Green', 6 => 'My new apple', 7 => 'Some odd color');
     $this->assertEquals($expected, $TestModel->find('list'));
     $this->assertEquals($expected, $TestModel->Parent->find('list'));
     $TestModel = new Post();
     $result = $TestModel->find('list', array('fields' => 'Post.title'));
     $expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post');
     $this->assertEquals($expected, $result);
     $result = $TestModel->find('list', array('fields' => 'title'));
     $expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post');
     $this->assertEquals($expected, $result);
     $result = $TestModel->find('list', array('fields' => array('title', 'id')));
     $expected = array('First Post' => '1', 'Second Post' => '2', 'Third Post' => '3');
     $this->assertEquals($expected, $result);
     $result = $TestModel->find('list', array('fields' => array('title', 'id', 'created')));
     $expected = array('2007-03-18 10:39:23' => array('First Post' => '1'), '2007-03-18 10:41:23' => array('Second Post' => '2'), '2007-03-18 10:43:23' => array('Third Post' => '3'));
     $this->assertEquals($expected, $result);
     $result = $TestModel->find('list', array('fields' => array('Post.body')));
     $expected = array(1 => 'First Post Body', 2 => 'Second Post Body', 3 => 'Third Post Body');
     $this->assertEquals($expected, $result);
     $result = $TestModel->find('list', array('fields' => array('Post.title', 'Post.body')));
     $expected = array('First Post' => 'First Post Body', 'Second Post' => 'Second Post Body', 'Third Post' => 'Third Post Body');
     $this->assertEquals($expected, $result);
     $result = $TestModel->find('list', array('fields' => array('Post.id', 'Post.title', 'Author.user'), 'recursive' => 1));
     $expected = array('mariano' => array(1 => 'First Post', 3 => 'Third Post'), 'larry' => array(2 => 'Second Post'));
     $this->assertEquals($expected, $result);
     $TestModel = new User();
     $result = $TestModel->find('list', array('fields' => array('User.user', 'User.password')));
     $expected = array('mariano' => '5f4dcc3b5aa765d61d8327deb882cf99', 'nate' => '5f4dcc3b5aa765d61d8327deb882cf99', 'larry' => '5f4dcc3b5aa765d61d8327deb882cf99', 'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99');
     $this->assertEquals($expected, $result);
     $TestModel = new ModifiedAuthor();
     $result = $TestModel->find('list', array('fields' => array('Author.id', 'Author.user')));
     $expected = array(1 => 'mariano (CakePHP)', 2 => 'nate (CakePHP)', 3 => 'larry (CakePHP)', 4 => 'garrett (CakePHP)');
     $this->assertEquals($expected, $result);
     $TestModel = new Article();
     $TestModel->displayField = 'title';
     $result = $TestModel->find('list', array('conditions' => array('User.user' => 'mariano'), 'recursive' => 0));
     $expected = array(1 => 'First Article', 3 => 'Third Article');
     $this->assertEquals($expected, $result);
 }
Exemplo n.º 2
0
 /**
  * testBehaviorMethodDispatchingWithData method
  *
  * @return void
  */
 public function testBehaviorMethodDispatchingWithData()
 {
     $Apple = new Apple();
     $Apple->Behaviors->attach('Test');
     $Apple->set('field', 'value');
     $this->assertTrue($Apple->testData());
     $this->assertTrue($Apple->data['Apple']['field_2']);
     $this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six'));
 }
Exemplo n.º 3
0
 /**
  * testTablePrefixSwitching method
  *
  * @access public
  * @return void
  */
 function testTablePrefixSwitching()
 {
     ConnectionManager::create('database1', array_merge($this->db->config, array('prefix' => 'aaa_')));
     ConnectionManager::create('database2', array_merge($this->db->config, array('prefix' => 'bbb_')));
     $db1 = ConnectionManager::getDataSource('database1');
     $db2 = ConnectionManager::getDataSource('database2');
     $TestModel = new Apple();
     $TestModel->setDataSource('database1');
     $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
     $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
     $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
     $TestModel->setDataSource('database2');
     $this->assertEqual($this->db->fullTableName($TestModel, false), 'bbb_apples');
     $this->assertEqual($db1->fullTableName($TestModel, false), 'bbb_apples');
     $this->assertEqual($db2->fullTableName($TestModel, false), 'bbb_apples');
     $TestModel = new Apple();
     $TestModel->tablePrefix = 'custom_';
     $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
     $TestModel->setDataSource('database1');
     $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
     $this->assertEqual($db1->fullTableName($TestModel, false), 'custom_apples');
     $TestModel = new Apple();
     $TestModel->setDataSource('database1');
     $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
     $TestModel->tablePrefix = '';
     $TestModel->setDataSource('database2');
     $this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
     $this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
     $TestModel->tablePrefix = null;
     $TestModel->setDataSource('database1');
     $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
     $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
     $TestModel->tablePrefix = false;
     $TestModel->setDataSource('database2');
     $this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
     $this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
 }
 /**
  * testBehaviorMethodDispatchingWithData method
  *
  * @access public
  * @return void
  */
 function testBehaviorMethodDispatchingWithData()
 {
     $Apple = new Apple();
     $Apple->Behaviors->attach('Test');
     $Apple->set('field', 'value');
     $this->assertTrue($Apple->testData());
     $this->assertTrue($Apple->data['Apple']['field_2']);
 }
Exemplo n.º 5
0
 public function testCallingParentOnStatics()
 {
     $this->assertEquals('Fruit', Fruit::getName());
     $this->assertEquals('Fruit: Apple', Apple::getName());
 }
Exemplo n.º 6
0
 /**
  * testTablePrefixSwitching method
  *
  * @return void
  */
 public function testTablePrefixSwitching()
 {
     ConnectionManager::create('database1', array_merge($this->db->config, array('prefix' => 'aaa_')));
     ConnectionManager::create('database2', array_merge($this->db->config, array('prefix' => 'bbb_')));
     $db1 = ConnectionManager::getDataSource('database1');
     $db2 = ConnectionManager::getDataSource('database2');
     $TestModel = new Apple();
     $TestModel->setDataSource('database1');
     $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
     $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
     $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
     $TestModel->setDataSource('database2');
     $this->assertContains('bbb_apples', $this->db->fullTableName($TestModel));
     $this->assertContains('bbb_apples', $db1->fullTableName($TestModel));
     $this->assertContains('bbb_apples', $db2->fullTableName($TestModel));
     $TestModel = new Apple();
     $TestModel->tablePrefix = 'custom_';
     $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
     $TestModel->setDataSource('database1');
     $this->assertContains('custom_apples', $this->db->fullTableName($TestModel));
     $this->assertContains('custom_apples', $db1->fullTableName($TestModel));
     $TestModel = new Apple();
     $TestModel->setDataSource('database1');
     $this->assertContains('aaa_apples', $this->db->fullTableName($TestModel));
     $TestModel->tablePrefix = '';
     $TestModel->setDataSource('database2');
     $this->assertContains('apples', $db2->fullTableName($TestModel));
     $this->assertContains('apples', $db1->fullTableName($TestModel));
     $TestModel->tablePrefix = NULL;
     $TestModel->setDataSource('database1');
     $this->assertContains('aaa_apples', $db2->fullTableName($TestModel));
     $this->assertContains('aaa_apples', $db1->fullTableName($TestModel));
     $TestModel->tablePrefix = FALSE;
     $TestModel->setDataSource('database2');
     $this->assertContains('apples', $db2->fullTableName($TestModel));
     $this->assertContains('apples', $db1->fullTableName($TestModel));
 }
Exemplo n.º 7
0
		public function remove($appleID = null)
		{
			// Confirm and Delete an Tofu
			$this->apple = Apple::find($appleID);
			$this->apple->remove($_POST['apple']);
		}
Exemplo n.º 8
0
<?php

declare (strict_types=1);
class Apple
{
    function getMyApple() : self
    {
        return new Apple();
    }
}
$apple = new Apple();
var_dump($apple->getMyApple());
// This will fail
function thisFunctionWillFail() : self
{
}
var_dump(thisFunctionWillFail());