/** * Atomic - 2 transaction, concurrent update, aborted transaction shouldn't save any data */ public function testTransactionWDeleteAllCommit_atomic1() { $obj = new TestModel2($this->adapter); $obj->field1 = 'foo'; $obj->save(); $id1 = $obj->id; $obj = new TestModel2($this->adapter); $obj->field1 = 'bar'; $obj->save(); $id2 = $obj->id; $txn1 = new Transaction(); usleep(100); //sleep 100ms to ensure txn2 is benind txn1 $txn2 = new Transaction(); $this->assertTrue($txn2->getTimestamp() > $txn1->getTimestamp(), 'Transaction 2 is not newer than transaction 1'); //transaction 1 $obj_txn1 = new TestModel2($this->adapter); $obj_txn2 = new TestModel2($this->adapter); $obj_txn1->joinTransaction($txn1); $obj_txn2->joinTransaction($txn2); $obj_txn1->getById($id1); $obj_txn1->field1 = 'foo1'; $obj_txn1->save(); $obj_txn2->getById($id1); $obj_txn2->field1 = 'foo2'; $obj_txn2->save(); $obj_txn1->getById($id2); $obj_txn2->getById($id2); $obj_txn2->field1 = 'bar2'; $obj_txn2->save(); $obj_txn1->field1 = 'bar2'; try { $obj_txn1->delete(); $this->fail('Late delete did not throw exception'); } catch (OptimisticLockException $ex) { } $txn2->commit(); $objTest = new TestModel2($this->adapter); $objTest->getById($id1); $this->assertEquals('foo2', $objTest->field1); $objTest->getById($id2); $this->assertEquals('bar2', $objTest->field1); }
public function testRemoveIdx() { $objMaster = new TestModel2($this->adapter); $objMaster->field1 = 'master'; $objs = array(); for ($i = 0; $i < 5; $i++) { $objs[$i] = new TestModel2($this->adapter); $objs[$i]->field1 = 'reference-' . $i; $objs[$i]->save(); } for ($i = 0; $i < 5; $i++) { $objMaster->refArray->add($objs[$i]); } $objMaster->save(); $id = $objMaster->id; $this->assertEquals(5, $objMaster->refArray->counter); $testObj = new TestModel2($this->adapter); $testObj->getById($id); $this->assertEquals(5, $testObj->refArray->count()); $testObj->refArray->remove(3); //remove the 3rd idx $testObj = new TestModel2($this->adapter); //retrieve again $testObj->getById($id); $this->assertEquals(4, $testObj->refArray->count()); //should be 4 now $i = 0; $refArray = $testObj->refArray; foreach ($refArray as $obj) { $value = $obj->field1; $this->assertEquals('reference-' . $i, $value); $i++; if ($i == 3) { $i = 4; //skip 3rd index } } }