コード例 #1
0
 public function testWorkingWithCache()
 {
     foreach (DBTestPool::me()->getPool() as $db) {
         DBPool::me()->setDefault($db);
         $item = TestItem::create()->setName('testItem1');
         TestItem::dao()->add($item);
         $encapsulant = TestEncapsulant::create()->setName('testEncapsulant1');
         TestEncapsulant::dao()->add($encapsulant);
         $subItem1 = TestSubItem::create()->setName('testSubItem1')->setEncapsulant($encapsulant)->setItem($item);
         $subItem2 = TestSubItem::create()->setName('testSubItem2')->setEncapsulant($encapsulant)->setItem($item);
         TestSubItem::dao()->add($subItem1);
         TestSubItem::dao()->add($subItem2);
         $items = Criteria::create(TestItem::dao())->getList();
         foreach ($items as $item) {
             foreach ($item->getSubItems()->getList() as $subItem) {
                 $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1');
             }
         }
         $encapsulant = TestEncapsulant::dao()->getById(1);
         $encapsulant->setName('testEncapsulant1_changed');
         TestEncapsulant::dao()->save($encapsulant);
         // drop identityMap
         TestEncapsulant::dao()->dropIdentityMap();
         TestSubItem::dao()->dropIdentityMap();
         TestItem::dao()->dropIdentityMap();
         $items = Criteria::create(TestItem::dao())->getList();
         foreach ($items as $item) {
             foreach ($item->getSubItems()->getList() as $subItem) {
                 $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1_changed');
             }
         }
         // drop identityMap
         TestEncapsulant::dao()->dropIdentityMap();
         TestSubItem::dao()->dropIdentityMap();
         TestItem::dao()->dropIdentityMap();
         $subItem = TestSubItem::dao()->getById(1);
         $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1_changed');
         // drop identityMap
         TestEncapsulant::dao()->dropIdentityMap();
         TestSubItem::dao()->dropIdentityMap();
         TestItem::dao()->dropIdentityMap();
         $subItems = Criteria::create(TestSubItem::dao())->getList();
         foreach ($subItems as $subItem) {
             $this->assertEquals($subItem->getEncapsulant()->getName(), 'testEncapsulant1_changed');
         }
     }
 }
コード例 #2
0
 /**
  * @param TestCase $test
  * @return DBTestCreator
  */
 public function fillDB(TestCase $test = null)
 {
     $moscow = TestCity::create()->setName('Moscow');
     $piter = TestCity::create()->setName('Saint-Peterburg');
     $mysqler = TestUser::create()->setCity($moscow)->setCredentials(Credentials::create()->setNickname('mysqler')->setPassword(sha1('mysqler')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day'));
     $postgreser = clone $mysqler;
     $postgreser->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setCity($piter)->setUrl(HttpUrl::create()->parse('http://postgresql.org/'));
     $piter = TestCity::dao()->add($piter);
     $moscow = TestCity::dao()->add($moscow);
     if ($test) {
         $test->assertEquals($piter->getId(), 1);
         $test->assertEquals($moscow->getId(), 2);
     }
     $postgreser = TestUser::dao()->add($postgreser);
     for ($i = 0; $i < 10; $i++) {
         $encapsulant = TestEncapsulant::dao()->add(TestEncapsulant::create()->setName($i));
         $encapsulant->getCities()->fetch()->setList(array($piter, $moscow))->save();
     }
     $mysqler = TestUser::dao()->add($mysqler);
     if ($test) {
         $test->assertEquals($postgreser->getId(), 1);
         $test->assertEquals($mysqler->getId(), 2);
     }
     if ($test) {
         // put them in cache now
         TestUser::dao()->dropIdentityMap();
         TestUser::dao()->getById(1);
         TestUser::dao()->getById(2);
         if ($test instanceof DBDataTest) {
             $test->getListByIdsTest();
         }
         Cache::me()->clean();
         $test->assertTrue($postgreser == TestUser::dao()->getById(1));
         $test->assertTrue($mysqler == TestUser::dao()->getById(2));
     }
     $firstClone = clone $postgreser;
     $secondClone = clone $mysqler;
     $firstCount = TestUser::dao()->dropById($postgreser->getId());
     $secondCount = TestUser::dao()->dropByIds(array($mysqler->getId()));
     if ($test) {
         $test->assertEquals($firstCount, 1);
         $test->assertEquals($secondCount, 1);
         try {
             TestUser::dao()->getById(1);
             $test->fail();
         } catch (ObjectNotFoundException $e) {
             /* pass */
         }
         $result = Criteria::create(TestUser::dao())->add(Expression::eq(1, 2))->getResult();
         $test->assertEquals($result->getCount(), 0);
         $test->assertEquals($result->getList(), array());
     }
     TestUser::dao()->import($firstClone);
     TestUser::dao()->import($secondClone);
     if ($test && $test instanceof DBDataTest) {
         // cache multi-get
         $test->getListByIdsTest();
         $test->getListByIdsTest();
     }
     return $this;
 }