/** * 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.'); }
/** * Test for methods: `Fields::char()`, `Field::varchar()`, `Field::text()`, * `Field::tinyText()`, `Field::mediumText()`, `Field::longText()`. * * @since 1.0.0 * @access public * * @requires function Freyja\Database\Schema\Field::__construct * @requires function Freyja\Database\Schema\Field::char * @requires function Freyja\Database\Schema\Field::varchar * @requires function Freyja\Database\Schema\Field::text * @requires function Freyja\Database\Schema\Field::tinyText * @requires function Freyja\Database\Schema\Field::mediumText * @requires function Freyja\Database\Schema\Field::longText * @requires function ReflectionProperty::setAccessible * @requires function ReflectionProperty::getValue */ public function testStringMethods() { // Set accessibility to object properities. $reflection_type = new ReflectionProperty('Freyja\\Database\\Schema\\Field', 'type'); $reflection_length = new ReflectionProperty('Freyja\\Database\\Schema\\Field', 'length'); $reflection_type->setAccessible(true); $reflection_length->setAccessible(true); $field = new Field('name'); $field->char(); $char_retrieved_type = $reflection_type->getValue($field); $char_retrieved_length = $reflection_length->getValue($field); $this->assertEquals('CHAR', $char_retrieved_type, 'Failed asserting that Field::char() correctly set field type.'); $this->assertEquals(1, $char_retrieved_length, 'Failed asserting that Field::char() correctly set field length.'); $field = new Field('name'); $field->varchar(254); $varchar_retrieved_type = $reflection_type->getValue($field); $varchar_retrieved_length = $reflection_length->getValue($field); $this->assertEquals('VARCHAR', $varchar_retrieved_type, 'Failed asserting that Field::varchar() correctly set field type.'); $this->assertEquals(254, $varchar_retrieved_length, 'Failed asserting that Field::varchar() correctly set field length.'); $field = new Field('name'); $field->text(); $text_retrieved_type = $reflection_type->getValue($field); $this->assertEquals('TEXT', $text_retrieved_type, 'Failed asserting that Field::text() correctly set field type.'); $field = new Field('name'); $field->tinyText(); $tinytext_retrieved_type = $reflection_type->getValue($field); $this->assertEquals('TINYTEXT', $tinytext_retrieved_type, 'Failed asserting that Field::tinyText() correctly set field type.'); $field = new Field('name'); $field->mediumText(); $mediumtext_retrieved_type = $reflection_type->getValue($field); $this->assertEquals('MEDIUMTEXT', $mediumtext_retrieved_type, 'Failed asserting that Field::mediumText() correctly set field type.'); $field = new Field('name'); $field->longText(); $longtext_retrieved_type = $reflection_type->getValue($field); $this->assertEquals('LONGTEXT', $longtext_retrieved_type, 'Failed asserting that Field::longText() correctly set field type.'); }