Beispiel #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.');
 }
Beispiel #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.');
 }
Beispiel #3
0
 /**
  * Test for `Field::isAutoIncrement()`.
  *
  * @since 1.0.0
  * @access public
  *
  * @requires function Freyja\Database\Schema\Field::__construct
  * @requires function Freyja\Database\Schema\Field::isAutoIncrement
  */
 public function testIsAutoIncrement()
 {
     $field = new Field('name');
     $this->assertFalse($field->isAutoIncrement(), 'Failed asserting that Field::isAutoIncrement() correctly state whether the field is auto increment or not.');
 }
Beispiel #4
0
 /**
  * Build single field.
  *
  * This method is used by `Freyja\Database\Schema\Table::buildCreate()` and
  * uses a property initialized in that method, therefore you SHOULD NOT use
  * this method alone. Using this method alone will be the same of casting the
  * Field to a string.
  *
  * @since 1.0.0
  * @access public
  *
  * @param Freyja\Database\Schema\Field $field
  * @return string
  *
  * @throws Freyja\Exceptions\LogicException if AUTO_INCREMENT is set on more
  * than one field, or in a non primary key field.
  */
 public function buildField(Field $field)
 {
     if (isset($this->autoinc_field)) {
         if ($field->isAutoIncrement()) {
             if ($this->autoinc_field == true) {
                 unset($this->autoinc_field);
                 throw new LogicException('AUTO_INCREMENT cannot be set on more than one field');
             }
             if (!in_array($field->getName(), $this->primary_keys)) {
                 unset($this->autoinc_field);
                 throw new LogicException('AUTO_INCREMENT cannot be set on a field that isn\'t primary key');
             }
             $this->autoinc_field = true;
         }
     }
     return (string) $field;
 }