Esempio n. 1
0
 public function testSaveUpdate()
 {
     // Create new resource
     $data = array('id' => 1, 'name' => 'Mr. Foo', 'username' => 'foo', 'email' => '*****@*****.**');
     Model\User::make($data)->save();
     // Get the new created resource
     $foo = Model\User::find(1);
     // At this moment, resource should contain all Mr. Foo values
     $this->assertEquals($foo->name, 'Mr. Foo');
     $this->assertEquals($foo->email, '*****@*****.**');
     $this->assertEquals($foo->username, 'foo');
     // Change all Foo's attribute into Bar
     $foo->name = 'Mr. Bar';
     $foo->email = '*****@*****.**';
     $foo->username = '******';
     // Update
     $foo->save();
     // Retrive back the user using old id
     $bar = Model\User::find(1);
     // Consist
     $this->assertInstanceOf('Gas\\ORM', $bar);
     $this->assertInstanceOf('Gas\\Data', $bar->record);
     // Check result
     $this->assertEquals($bar->id, '1');
     $this->assertEquals($bar->name, 'Mr. Bar');
     $this->assertEquals($bar->email, '*****@*****.**');
     $this->assertEquals($bar->username, 'bar');
 }
Esempio n. 2
0
 public function testApiMake()
 {
     // A model could be either instantiated or use `make` method
     $user_instantiated = new Model\User();
     $user_non_instantiated = Model\User::make();
     // Both should be instance of Gas ORM
     $this->assertInstanceOf('Gas\\ORM', $user_instantiated);
     $this->assertInstanceOf('Gas\\ORM', $user_non_instantiated);
     // Both should be equally same
     $this->assertEquals($user_instantiated, $user_non_instantiated);
 }
 public function testCallbackSave()
 {
     // Create new resource with preserved value for username
     $data = array('id' => 10, 'name' => 'Mr. Foo', 'username' => 'administrator', 'email' => '*****@*****.**');
     $foo = Model\User::make($data);
     // In corresponding model, there is `_before_save` callback
     // to prevent new users register using `administrator` as their username
     $foo->save();
     unset($foo);
     // Get the last created entry
     $foo = Model\User::find(10);
     // Consist
     $this->assertInstanceOf('Gas\\ORM', $foo);
     $this->assertInstanceOf('Gas\\Data', $foo->record);
     // Since the before_save already make sure that username can not contain
     // `administrator`, it turn into `member`
     $this->assertEquals($foo->username, 'member');
 }