Example #1
0
 /**
  * Test for `Schema::alter()`.
  *
  * @since 1.0.0
  * @access public
  *
  * @requires function Freyja\Database\Schema\Schema::__construct
  * @requires function Freyja\Database\Driver\MySqlDriver::connect
  * @requires function Freyja\Database\Database::__construct
  * @requires function Freyja\Database\Database::connect
  * @requires function Freyja\Database\Schema\Table::__construct
  * @requires function ReflectionProperty::setAccessible
  * @requires function ReflectionProperty::getValue
  */
 public function testAlter()
 {
     // Set accessibility to object property.
     $reflection_schema = new ReflectionProperty('Freyja\\Database\\Schema\\Schema', 'schema');
     $reflection_schema->setAccessible(true);
     $field = new Field('new_field');
     $fields = array($field->varchar(200));
     $table = new Table('customers');
     $db = new Database(new MySqlDriver());
     $schema = new Schema($db->connect('localhost', 'test', 'travis', ''));
     $schema->alter($table->addFields($fields));
     $retr_schema = $reflection_schema->getValue($schema);
     $this->assertTrue(isset($retr_schema['tables']['customers']['fields']['new_field']), 'Failed asserting that Schema::alter() correctly add the new field to the database schema.');
     $expected_schema_field = array('type' => 'VARCHAR(200)', 'default' => null, 'NOT NULL' => false, 'UNSIGNED' => false, 'AUTO_INCREMENT' => false);
     $this->assertEquals($expected_schema_field, $retr_schema['tables']['customers']['fields']['new_field'], 'Failed asserting that Schema::alter() correctly add the new field information to the database schema.');
     $message = '';
     try {
         $result = $this->getConnection()->getConnection()->query('SELECT new_field FROM customers');
     } catch (\PDOException $e) {
         $message = $e->getMessage();
     }
     $this->assertFalse($message == 'SQLSTATE[42S22]: Column not found: 1054 Unknown column \'new_field\' in \'field list\'', 'Failed asserting that Schema::alter() correctly alter a table.');
 }
Example #2
0
 /**
  * Test for `Table::buildAlter()`.
  *
  * @since 1.0.0
  * @access public
  *
  * @requires function Freyja\Database\Schema\Table::__construct
  * @requires function Freyja\Database\Schema\Table::build
  * @requires function Freyja\Database\Schema\Table::buildAlter
  */
 public function testBuildAlter()
 {
     $table = new Table('table');
     $f1 = new Field('f1');
     $f2 = new Field('f2');
     $f3 = new Field('f3');
     $table->addFields(array($f1->integer(), $f2->integer()))->removeFields(array($f3->integer()));
     $expected = 'ALTER TABLE table ADD f1 INT(11), ADD f2 INT(11), DROP COLUMN f3;';
     $this->assertEquals($expected, $table->build(), 'Failed asserting that Table methods correctly build a DROP table.');
 }