/** * 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); }
/** * @dataProvider entityManagerDataProvider * @param EntityManager $em */ public function testDeleteRelationships(EntityManager $em) { $article1 = new Article(); $article1->setId(301)->setTitle('Article 301'); $article2 = new Article(); $article2->setId(302)->setTitle('Article 302'); $category1 = new Category(); $category1->setId(351)->setName('Category 351'); $category1->addArticle($article1)->addArticle($article2); $em->persist($category1)->persist($article1)->persist($article2)->flush(); $this->assertTrue($this->exists($em, 'article', '301')); $key = $this->getRelKey($em, 'article', 'category', '301', 'canonical_category', RelationshipType::MANYTOONE()); $this->assertEquals('351', $em->getDriver()->getSingleValueIndex($key)); /** @var Article $article */ $article = $em->retrieve(Article::class, 301); $article->setId(399); $em->delete($article)->flush(); $this->assertFalse($this->exists($em, 'article', '301')); $this->setExpectedException(NotFoundException::class); $em->getDriver()->retrieve($key); }
/** * @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()); }