Inheritance: use trait Bravo3\Orm\Traits\ProxyAwareTrait
Exemple #1
0
 /**
  * Testing race conditions of new entities, without a flush between persist calls
  *
  * @see: docs/RaceConditions.md
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testOneToManyRaceNoFlush(EntityManager $em)
 {
     $time = new \DateTime();
     $article1 = new Article();
     $article1->setId(201)->setTitle('Article 201')->setTimeCreated($time)->setLastModified($time);
     $category1 = new Category();
     $category1->setId(201)->setName('Category 201');
     $category1->addArticle($article1);
     $em->persist($category1)->persist($article1)->flush();
     /** @var Article|OrmProxyInterface $r_article */
     $r_article = $em->retrieve(Article::class, 201);
     $this->assertEquals('Article 201', $r_article->getTitle());
     // Should make DB query here
     $r_category = $r_article->getCanonicalCategory();
     $this->assertTrue($r_category instanceof Category);
     // Check inverse side too -
     /** @var Category|OrmProxyInterface $ir_category */
     $ir_category = $em->retrieve(Category::class, 201);
     $this->assertEquals('Category 201', $ir_category->getName());
     // Should make DB query here
     $ir_articles = $ir_category->getArticles();
     $this->assertCount(1, $ir_articles);
     $ir_article = $ir_articles[0];
     $this->assertTrue($ir_article instanceof Article);
 }
Exemple #2
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testIdQuery(EntityManager $em)
 {
     $a = new IndexedEntity();
     $a->setId1('1');
     $a->setId2('first');
     $a->setAlpha('alpha1');
     $a->setBravo(1);
     $a->setCharlie(true);
     $b = new IndexedEntity();
     $b->setId1('2');
     $b->setId2('second');
     $b->setAlpha('alpha2');
     $b->setBravo(2);
     $b->setCharlie(false);
     $em->persist($a)->persist($b)->flush();
     $result = $em->indexedQuery(new IndexedQuery(IndexedEntity::class, ['@id' => '1.fir*']));
     $this->assertCount(1, $result);
     /** @var IndexedEntity $entity */
     $entity = $result->current();
     $this->assertEquals('1', $entity->getId1());
     $this->assertEquals('first', $entity->getId2());
     $this->assertEquals('alpha1', $entity->getAlpha());
     $result = $em->indexedQuery(new IndexedQuery(IndexedEntity::class, ['@id' => '*']));
     $this->assertGreaterThanOrEqual(2, count($result));
     $result = $em->indexedQuery(new IndexedQuery(IndexedEntity::class, ['@id' => '*', 'ab' => 'alpha1*']));
     $this->assertCount(1, $result);
     /** @var IndexedEntity $entity */
     $entity = $result->current();
     $this->assertEquals('1', $entity->getId1());
     $this->assertEquals('first', $entity->getId2());
     $this->assertEquals('alpha1', $entity->getAlpha());
 }
Exemple #3
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testComplexSerialisation(EntityManager $em)
 {
     $serialiser = new JsonSerialiser();
     $time = new \DateTime();
     $product = new Product();
     $product->setCreateTime($time);
     $metadata = $em->getMapper()->getEntityMetadata($product);
     $data = $serialiser->serialise($metadata, $product);
     $new_product = new Product();
     $serialiser->deserialise($metadata, $data, $new_product);
     $this->assertEquals($time->format(self::DATE_FORMAT), $new_product->getCreateTime()->format(self::DATE_FORMAT));
 }
Exemple #4
0
 /**
  * Re-persist an array of records
  *
  * @param object[] $records
  * @param Entity   $metadata
  * @param int      $batch_size
  */
 private function rebuildRecords($records, Entity $metadata, $batch_size)
 {
     $count = 0;
     /** @var OrmProxyInterface $record */
     foreach ($records as $record) {
         $this->entity_manager->persist($record);
         if (++$count == $batch_size) {
             $this->entity_manager->flush();
             $count = 0;
         }
     }
     if ($count) {
         $this->entity_manager->flush();
     }
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @param bool            $export
  * @return void
  */
 protected function port(InputInterface $input, OutputInterface $output, $export = true)
 {
     $entities = $this->getEntities($input->getOption('list'));
     $em = $this->getContainer()->get('orm.em');
     $porter = new Porter(new OutputLogger($output));
     $porter->registerManager('PRIMARY', $em);
     $io = new PharIoDriver($input->getOption($export ? 'output' : 'input'), ArchiveType::memberByKey(strtoupper($input->getOption('format'))));
     $driver = new FilesystemDriver($io);
     $aux = EntityManager::build($driver, $em->getMapper(), $em->getSerialiserMap());
     $porter->registerManager('AUX', $aux);
     $batch_size = max(1, min(1000, (int) $input->getOption('batch')));
     $term = $export ? 'Exporting' : 'Importing';
     foreach ($entities as $class_name) {
         $output->writeln($term . " <info>" . $class_name . "</info>..");
         try {
             if ($export) {
                 $porter->portTable($class_name, 'PRIMARY', 'AUX', $batch_size);
             } else {
                 $porter->portTable($class_name, 'AUX', 'PRIMARY', $batch_size);
             }
         } catch (\Exception $e) {
             $output->writeln("<error>ERROR:</error> " . $e->getMessage());
         }
     }
     if ($export) {
         $output->writeln("<comment>EXPORT COMPLETE</comment>");
     } else {
         $output->writeln("<comment>IMPORT COMPLETE</comment>");
     }
 }
Exemple #6
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRelatedIndexDeletion(EntityManager $em)
 {
     $home = new Address();
     $home->setId(44)->setStreet('Oxford St');
     $work = new Address();
     $work->setId(45)->setStreet('George St');
     $user = new User();
     $user->setId(23)->setName('Barry')->setAddress($home);
     $em->persist($user)->persist($home)->persist($work)->flush();
     /** @var User|OrmProxyInterface $user_home */
     $user_home = $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $home->getId());
     $this->assertEquals(23, $user_home->getId());
     $this->assertEquals('Oxford St', $user_home->getAddress()->getStreet());
     $slug = $user_home->getIndexOriginalValue('slug');
     $this->assertEquals('23.44', $slug);
     $user_home->setName('Other Barry');
     $user_home->setAddress($work);
     $em->persist($user_home)->flush();
     /** @var User $user_work */
     $user_work = $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $work->getId());
     $this->assertEquals(23, $user_work->getId());
     $this->assertEquals('George St', $user_work->getAddress()->getStreet());
     $this->setExpectedException(NotFoundException::class);
     $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $home->getId());
 }
 /**
  * Destroys a session.
  *
  * If the session does not exist this function will return true anyway.
  *
  * @param string $sessionId Session ID, see http://php.net/function.session-id
  * @return bool true on success, false on failure
  */
 public function destroy($sessionId)
 {
     /** @var SessionInterface $session */
     $session = new $this->session_class();
     $session->setId($sessionId);
     $this->entity_manager->delete($session)->flush();
     return true;
 }
Exemple #8
0
 /**
  * Hydrate an entity
  *
  * @param string $id
  * @return $this
  */
 private function hydrateEntity($id)
 {
     try {
         $this->entities[$id] = $this->entity_manager->retrieve($this->query->getClassName(), $id, $this->use_cache);
     } catch (NotFoundException $e) {
         throw new CorruptedEntityException("Entity in sorted index not found: " . $e->getMessage(), 0, $e);
     }
     return $this;
 }
Exemple #9
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testTableSorting(EntityManager $em)
 {
     $user1 = new SortedUser();
     $user1->setId(1)->setName('User 1')->setActive(true);
     $user2 = new SortedUser();
     $user2->setId(2)->setName('User 2')->setActive(false);
     $user3 = new SortedUser();
     $user3->setId(3)->setName('User 3')->setActive(true);
     $em->persist($user1)->persist($user2)->persist($user3)->flush();
     $query = $em->sortedQuery(new SortedTableQuery(SortedUser::class, 'name_all'));
     $this->assertEquals(3, $query->count());
     $this->assertEquals('User 1', $query[0]->getName());
     $this->assertEquals('User 2', $query[1]->getName());
     $this->assertEquals('User 3', $query[2]->getName());
     $query = $em->sortedQuery(new SortedTableQuery(SortedUser::class, 'name_active'));
     $this->assertEquals(2, $query->count());
     $this->assertEquals('User 1', $query[0]->getName());
     $this->assertEquals('User 3', $query[1]->getName());
 }
Exemple #10
0
 /**
  * Update the users password and/or salt
  *
  * @param User   $user
  * @param string $password New password
  * @param string $salt     If Ommitted, the salt will not be updated
  * @param bool   $flush    Flush the entity manager on completion
  */
 public function updateUserCredentials(User $user, $password, $salt = null, $flush = true)
 {
     if (!$salt) {
         $salt = $user->getSalt() ?: $this->generateSalt();
     }
     $encoder = $this->encoder_factory->getEncoder($user);
     $password = $encoder->encodePassword($password, $salt);
     $user->setPassword($password);
     $user->setSalt($salt);
     $this->entity_manager->persist($user);
     if ($flush) {
         $this->entity_manager->flush();
     }
 }
Exemple #11
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testReverseConditionalRelationship(EntityManager $em)
 {
     $category = new Category();
     $category->setId(2000)->setName('Conditional Category');
     $em->persist($category)->flush();
     for ($i = 65; $i < 76; $i++) {
         $article = new Article();
         $article->setId($i)->setTitle('Conditional Article #' . $i);
         if ($i == 73) {
             $article->setPublished(false);
         } else {
             $article->setPublished(true);
         }
         $article->setCategory($category);
         $em->persist($article);
     }
     for ($i = 65; $i < 76; $i++) {
         $asset = new Asset();
         $asset->setId($i)->setTitle('Conditional Asset #' . $i);
         if ($i == 73) {
             $asset->setPublished(false);
         } else {
             $asset->setPublished(true);
         }
         $asset->setCategory($category);
         $em->persist($asset);
     }
     $em->flush();
     $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'last_modified'));
     $this->assertCount(10, $articles);
     $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'id'));
     $this->assertCount(11, $articles);
     $assets = $em->sortedQuery(new SortedQuery($category, 'assets', 'last_modified'));
     $this->assertCount(10, $assets);
     $assets = $em->sortedQuery(new SortedQuery($category, 'assets', 'id'));
     $this->assertCount(11, $assets);
 }
Exemple #12
0
 /**
  * Hydrate a relationship
  *
  * @param Relationship $relative
  * @return $this
  */
 public function hydrateRelative(Relationship $relative)
 {
     $this->entity_manager->getDriver()->debugLog("Hydrating relative for " . $this->metadata->getTableName() . "[" . $this->getReader()->getId() . "]::" . $relative->getName());
     $setter = $relative->getSetter();
     $key = $this->entity_manager->getKeyScheme()->getRelationshipKey($relative, $this->entity_manager->getMapper()->getEntityMetadata($relative->getSource())->getTableName(), $this->entity_manager->getMapper()->getEntityMetadata($relative->getTarget())->getTableName(), $this->getReader()->getId());
     if (RelationshipType::isMultiIndex($relative->getRelationshipType())) {
         $items = [];
         $ids = $this->entity_manager->getDriver()->getMultiValueIndex($key);
         foreach ($ids as $id) {
             $items[] = $this->entity_manager->retrieveEntityOrNew($relative->getTarget(), $id);
         }
         $this->proxy->{$setter}($items);
     } else {
         $id = $this->entity_manager->getDriver()->getSingleValueIndex($key);
         if ($id) {
             $this->proxy->{$setter}($this->entity_manager->retrieve($relative->getTarget(), $id));
         }
     }
     return $this;
 }
Exemple #13
0
 /**
  * Deletes all items in the pool.
  *
  * @return PoolInterface
  */
 public function clear()
 {
     $items = $this->em->sortedQuery(new SortedTableQuery($this->entity_class, 'key'), false, false);
     $index = 0;
     $items->rewind();
     while ($items->valid()) {
         try {
             $item = $items->current();
             $this->em->delete($item);
             if (++$index % 100 == 0) {
                 $this->em->flush();
             }
         } catch (NotFoundException $e) {
             // Assume item expired, skip
         }
         $items->next();
     }
     $this->em->flush();
     return $this;
 }
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  * @expectedException \Bravo3\Orm\Exceptions\CorruptedEntityException
  */
 public function testSortedQueryThrowsNotFoundException(EntityManager $em)
 {
     $category = (new Category())->setId(5000);
     $article1 = (new Article())->setId(5001)->setTitle('A');
     $article2 = (new Article())->setId(5002)->setTitle('B');
     $article3 = (new Article())->setId(5003)->setTitle('C');
     $category->addArticle($article1);
     $category->addArticle($article2);
     $category->addArticle($article3);
     $em->persist($article1)->persist($article2)->persist($article3)->persist($category)->flush();
     // Forcefully break the relationship via the driver manually
     $em->getDriver()->delete($em->getKeyScheme()->getEntityKey('article', '5001'));
     $em->getDriver()->flush();
     $category = $em->retrieve(Category::class, 5000, false);
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date'));
     // Iterating through these results should trigger an exception
     foreach ($results as $result) {
     }
 }
Exemple #15
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testOneToOneBreaking(EntityManager $em)
 {
     $ed = new User();
     $ed->setId(900)->setName('Ed');
     $jane = new User();
     $jane->setId(901)->setName('Jane');
     $address = new Address();
     $address->setId(951)->setStreet('Howzer-Black St');
     $ed->setAddress($address);
     $em->persist($address)->persist($ed)->persist($jane)->flush();
     $em->refresh($ed);
     $em->refresh($jane);
     $em->refresh($address);
     $jane->setAddress($address);
     $em->persist($jane)->flush();
     $em->refresh($ed);
     $em->refresh($jane);
     $em->refresh($address);
     $this->assertNull($ed->getAddress());
     $this->assertEquals('Howzer-Black St', $jane->getAddress()->getStreet());
     $this->assertEquals('Jane', $address->getUser()->getName());
 }
Exemple #16
0
 /**
  * Get the mapper belonging to the entity manager
  *
  * @return MapperInterface
  */
 protected function getMapper()
 {
     return $this->entity_manager->getMapper();
 }
Exemple #17
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testNullObjectSerialisation(EntityManager $em)
 {
     $product = new Product();
     $product->setId(701)->setEnum(null);
     $em->persist($product)->flush();
     /** @var Product $r_product */
     $r_product = $em->retrieve(Product::class, '701');
     $this->assertNull($r_product->getEnum());
 }
Exemple #18
0
 /**
  * Same as above test, except we'll delete the category
  *
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testDeleteIndicesAndRelationshipsAlt(EntityManager $em)
 {
     $article1 = new RefArticle();
     $article1->setId(502)->setTitle('Ref Article 502');
     $category1 = new RefCategory();
     $category1->setId(533)->setName('Ref Category 533');
     $article1->setCanonicalCategory($category1);
     $em->persist($category1)->persist($article1)->flush();
     $this->assertTrue($this->exists($em, 'article', '502'));
     $mto = $this->getRelKey($em, 'article', 'category', '502', 'canonical_category', RelationshipType::MANYTOONE());
     $this->assertEquals('533', $em->getDriver()->getSingleValueIndex($mto));
     // Not inversed:
     $otm = $this->getRelKey($em, 'category', 'article', '533', 'articles', RelationshipType::ONETOMANY());
     $this->assertNotContains('502', $em->getDriver()->getMultiValueIndex($otm));
     // Ref exists:
     $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '533'));
     $ref = (string) new Ref(RefArticle::class, '502', 'canonical_category');
     $this->assertCount(1, $refs);
     $this->assertEquals($ref, (string) $refs[0]);
     /** @var RefCategory $category */
     $category = $em->retrieve(RefCategory::class, 533);
     $em->delete($category)->flush();
     $this->assertFalse($this->exists($em, 'category', '533'));
     $this->assertNull($em->getDriver()->getSingleValueIndex($mto));
     $this->assertNotContains('502', $em->getDriver()->getMultiValueIndex($otm));
     // Ref no longer needed:
     $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '533'));
     $this->assertNotContains($ref, $refs);
 }
Exemple #19
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testSortOrder(EntityManager $em)
 {
     $category = new Category();
     $category->setId(600);
     $em->persist($category);
     for ($i = 0; $i < 15; $i++) {
         $article = new Article();
         $article->setId(601 + $i);
         $article->setTitle('Art ' . (601 + $i));
         $time = new \DateTime();
         $time->modify('+' . ($i + 1) . ' minutes');
         $article->setSortDate($time);
         $article->setCanonicalCategory($category);
         $em->persist($article);
     }
     $em->flush();
     /** @var Article $article */
     // Date sorting -
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC()));
     $this->assertCount(15, $results);
     $this->assertEquals(15, $results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 615', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC(), 5, -6), true);
     $this->assertCount(5, $results);
     $this->assertEquals(15, $results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 610', $article->getTitle());
     $article = $results[4];
     $this->assertEquals('Art 606', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 2, 5));
     $this->assertCount(4, $results);
     $this->assertNull($results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 603', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 20, 29));
     $this->assertCount(0, $results);
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     // Lexicographic sorting -
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title', Direction::DESC()));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 615', $article->getTitle());
     // Modify an entity's sort-by column
     $article = $em->retrieve(Article::class, 609);
     $time = $article->getSortDate();
     $time->modify('+1 day');
     $article->setSortDate($time);
     $em->persist($article)->flush();
     $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC()));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 609', $article->getTitle());
 }
Exemple #20
0
<?php

use Bravo3\Orm\Drivers\Filesystem\FilesystemDriver;
use Bravo3\Orm\Drivers\Filesystem\Io\NativeIoDriver;
use Bravo3\Orm\Mappers\Annotation\AnnotationMapper;
use Bravo3\Orm\Mappers\Portation\MapWriterInterface;
use Bravo3\Orm\Mappers\Yaml\YamlMapWriter;
use Bravo3\Orm\Services\EntityLocator;
use Bravo3\Orm\Services\EntityManager;
use Bravo3\Properties\Conf;
use Predis\Client;
require __DIR__ . '/../vendor/autoload.php';
Conf::init(__DIR__ . '/config/', 'parameters.yml');
$redis = new Client(['host' => Conf::get('parameters.redis_host'), 'port' => Conf::get('parameters.redis_port'), 'database' => Conf::get('parameters.redis_database')]);
$redis->flushdb();
// Solely for portation (reading metadata) - will never do anything
$em = EntityManager::build(new FilesystemDriver(new NativeIoDriver('/dev/null')), new AnnotationMapper());
$locator = new EntityLocator($em);
$entities = $locator->locateEntities(__DIR__ . '/Bravo3/Orm/Tests/Entities', 'Bravo3\\Orm\\Tests\\Entities');
/** @var MapWriterInterface[] $porters */
$porters = [new YamlMapWriter(__DIR__ . '/Bravo3/Orm/Tests/Resources/mappings.yml')];
foreach ($porters as $porter) {
    $porter->setInputManager($em);
    foreach ($entities as $class_name) {
        $porter->compileMetadataForEntity($class_name);
    }
    $porter->flush();
}
Exemple #21
0
 public function testPortation()
 {
     $mapper = new AnnotationMapper();
     $src = EntityManager::build($this->getRedisDriver(), $mapper);
     $dest = EntityManager::build($this->getFsDriver('porter-db'), $mapper);
     $porter = new Porter();
     $porter->registerManager('redis', $src);
     $porter->registerManager('tar', $dest);
     // Create some dummy data
     $category = new Category();
     $category->setId(600);
     $src->persist($category);
     for ($i = 0; $i < 15; $i++) {
         $article = new Article();
         $article->setId(601 + $i);
         $article->setTitle('Art ' . (601 + $i));
         $time = new \DateTime();
         $time->modify('+' . ($i + 1) . ' minutes');
         $article->setSortDate($time);
         $article->setCanonicalCategory($category);
         $src->persist($article);
     }
     $src->flush();
     // Port the database
     $porter->portTable(Article::class, 'redis', 'tar');
     $porter->portTable(Category::class, 'redis', 'tar');
     // Check entity counts
     $articles = $dest->indexedQuery(new IndexedQuery(Article::class, ['@id' => '*']), false);
     $this->assertCount(15, $articles);
     $categories = $dest->indexedQuery(new IndexedQuery(Category::class, ['@id' => '*']), false);
     $this->assertCount(1, $categories);
     // Test indices were preserved
     /** @var Article $article */
     // Date sorting -
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'sort_date'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC()));
     $this->assertCount(15, $results);
     $this->assertEquals(15, $results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 615', $article->getTitle());
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC(), 5, -6), true);
     $this->assertCount(5, $results);
     $this->assertEquals(15, $results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 610', $article->getTitle());
     $article = $results[4];
     $this->assertEquals('Art 606', $article->getTitle());
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 2, 5));
     $this->assertCount(4, $results);
     $this->assertNull($results->getFullSize());
     $article = $results[0];
     $this->assertEquals('Art 603', $article->getTitle());
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 20, 29));
     $this->assertCount(0, $results);
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'title'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     // Lexicographic sorting -
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'title'));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 601', $article->getTitle());
     $results = $dest->sortedQuery(new SortedQuery($category, 'articles', 'title', Direction::DESC()));
     $this->assertCount(15, $results);
     $article = $results[0];
     $this->assertEquals('Art 615', $article->getTitle());
 }
Exemple #22
0
 /**
  * Provides all pool implementations
  *
  * @return array
  */
 public function poolProvider()
 {
     $em = EntityManager::build(new FilesystemDriver(new NativeIoDriver(sys_get_temp_dir() . '/bravo3-cache/')), new AnnotationMapper());
     return [[new EphemeralCachePool()], [new RedisCachePool()], [new OrmCachePool($em)], [new OrmCachePool($em, null, true)]];
 }
Exemple #23
0
 protected function getRelKey(EntityManager $em, $from, $to, $id, $property, RelationshipType $type)
 {
     $rel = new Relationship($property, $type);
     $rel->setSourceTable($from)->setTargetTable($to);
     return $em->getKeyScheme()->getRelationshipKey($rel, $id);
 }
Exemple #24
0
 /**
  * Build an entity manager
  *
  * @param DriverInterface $driver
  * @param MapperInterface $mapper
  * @param string          $cache_dir
  * @return EntityManager
  */
 public static function createEntityManager(DriverInterface $driver, MapperInterface $mapper, $cache_dir)
 {
     $config = new Configuration();
     $config->setCacheDir($cache_dir);
     return EntityManager::build($driver, $mapper, null, null, $config);
 }
Exemple #25
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRebuildIndex(EntityManager $em)
 {
     // Create an article with a slug
     $article = new SluggedArticle();
     $article->setId(8343)->setName('foo')->setSlug('bar');
     $em->persist($article)->flush();
     // Confirm slug works
     /** @var SluggedArticle $article */
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
     $this->assertEquals('foo', $article->getName());
     $index = $em->getMapper()->getEntityMetadata($article)->getIndexByName('slug');
     // Corrupt the slug, two steps required:
     // 1. Set a new slug
     $em->getDriver()->setSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'evil'), $article->getId());
     // 2. Remove the correct slug
     $em->getDriver()->clearSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'bar'));
     $em->getDriver()->flush();
     // Confirm old slug no longer works
     try {
         $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
         $this->fail('Old index succeeded');
     } catch (NotFoundException $e) {
     }
     // Confirm new slug does work
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false);
     $this->assertEquals('foo', $article->getName());
     // Run maintenance over the table, this should correct the slug
     $maintenance = new Maintenance($em);
     $maintenance->rebuild(SluggedArticle::class);
     // Confirm correct slug works
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
     $this->assertEquals('foo', $article->getName());
     // The corrupted slug should still work, this is unideal, but there is no reference to it for the maintenance
     // service to know to remove it
     $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false);
     $this->assertEquals('foo', $article->getName());
 }
Exemple #26
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testDereferencing(EntityManager $em)
 {
     $mapper = $em->getMapper();
     if ($mapper instanceof DereferencingMapperInterface) {
         $class_name = $mapper->getClassFromTable('slugged_article');
         $this->assertEquals(SluggedArticle::class, $class_name);
     }
 }