コード例 #1
0
ファイル: ModelTest.php プロジェクト: minwork/minwork
 public function testModelFlow()
 {
     $validatorFunction = function ($value, $arg1, $arg2) {
         return !empty($value) && $arg1 && !$arg2;
     };
     $data = ['name' => 'test', 'email' => '*****@*****.**', 'change_date' => DateHelper::now()];
     $newData = ['name' => 'test2', 'change_date' => DateHelper::addHours(DateHelper::now(), 2)->format('Y-m-d H:i:s')];
     $newId = 'unexisting';
     $database = new Database('mysql', 'localhost', 'test');
     $table = new Table($database, 'test');
     $table->create([new Column('id', 'INT', false, true, true), new Column('name', 'VARCHAR(255)'), new Column('email', 'VARCHAR(255)'), new Column('change_date', 'DATETIME')], true);
     $model = new Model($table);
     $validator = new Validator(['name' => [new Rule('isNotEmpty'), new Rule('isAlphabeticOnly'), new Rule('isInt', [], false)], 'email' => [new Rule('isEmail'), new Rule($validatorFunction, [true, false])]]);
     $this->assertTrue($model->execute(new Create(), [$data], $validator));
     $this->assertTrue($model->exists());
     $this->assertNotNull($model->getId());
     $this->assertEquals($data, $model->getData());
     $this->assertEquals($data['name'], $model->getData('name'));
     $this->assertEquals(array_intersect_key($data, array_flip(['name', 'email'])), $model->getData(['name', 'email']));
     $model->execute(new Update(), [$newData]);
     $this->assertTrue($model->exists());
     $this->assertEquals(array_merge($data, $newData), $model->getData());
     $id = $model->getId();
     $model->setId($newId);
     $this->assertFalse($model->exists());
     $model->setId($id);
     $this->assertTrue($model->exists());
     $model->execute(new Delete());
     $this->assertFalse($model->exists());
     $this->assertEmpty($model->getData());
     $this->assertNull($model->getId());
     // Clean up table
     $table->drop();
 }
コード例 #2
0
ファイル: DatabaseTest.php プロジェクト: minwork/minwork
 public function testOperations()
 {
     $database = new Database('mysql', 'localhost', 'test');
     $table = new Table($database, 'test');
     $table->create([new Column('id', 'INT', false, true, true), new Column('data', 'VARCHAR(255)'), new Column('date', 'DATETIME')], true);
     $data = ['data' => 'test', 'date' => DateHelper::now()];
     $data2 = ['data' => 'test2', 'date' => DateHelper::addDays(DateHelper::now(), 2)->format('Y-m-d H:i:s')];
     $table->delete();
     $this->assertEquals(false, $table->exists($data));
     $this->assertEquals(0, $table->countRows());
     $this->assertEquals(0, $table->countRows($data));
     $table->insert(array_keys($data), array_values($data));
     $id = $table->getDatabase()->lastInsertId();
     $this->assertEquals(true, $table->exists([Table::DEAULT_ID_COLUMN => $id]));
     $this->assertEquals(1, $table->countRows());
     $this->assertEquals(1, $table->countRows($data));
     $this->assertEquals($data, $table->select($data, Table::OPERATOR_AND, array_keys($data))->fetch(Database::FETCH_ASSOC));
     $table->update($data2, [Table::DEAULT_ID_COLUMN => $id]);
     $this->assertEquals($data2, $table->select([Table::DEAULT_ID_COLUMN => $id], Table::OPERATOR_AND, array_keys($data))->fetch(Database::FETCH_ASSOC));
     // Clean up table
     $table->drop();
 }