<?php declare(strict_types = 1);
/**
* @testCase
* @dataProvider ../../../sections.ini
*/
namespace NextrasTestsOrmIntegrationRelationships;
use NextrasOrmCollectionICollection;
use NextrasTestsOrmBook;
use NextrasTestsOrmDataTestCase;
use NextrasTestsOrmHelper;
use NextrasTestsOrmTag;
use NextrasTestsOrmUser;
use TesterAssert;
use TesterEnvironment;
$dic = require_once __DIR__ . '/../../../bootstrap.php';
class RelationshipManyHasManyTest extends DataTestCase
{
public function testCache()
{
$book = $this->orm->books->getById(1);
$collection = $book->tags->get()->findBy(['name!=' => 'Tag 1'])->orderBy('id');
assert::
equal(1,
$collection->
count());
assert::
equal(1,
$collection->
countStored());
assert::
equal('Tag 2',
$collection->
fetch()->
name);
$collection = $book->tags->get()->findBy(['name!=' => 'Tag 3'])->orderBy('id');
assert::
equal(2,
$collection->
count());
assert::
equal(2,
$collection->
countStored());
assert::
equal('Tag 1',
$collection->
fetch()->
name);
assert::
equal('Tag 2',
$collection->
fetch()->
name);
}
public function testLimit()
{
$book = $this->orm->books->getById(1);
$book->tags->add(3);
$this->orm->books->persistAndFlush($book);
/** @var Book[] $books */
$books = $this->orm->books->findAll()->orderBy('id');
$tags = [];
$counts = [];
$countsStored = [];
foreach ($books as $book) {
$limitedTags = $book->tags->get()->limitBy(2)->orderBy('name', ICollection::DESC);
foreach ($limitedTags as $tag) {
$tags[] = $tag->id;
}
$counts[] = $limitedTags->count();
$countsStored[] = $limitedTags->countStored();
}
assert::
same([3,
2,
3,
2,
3],
$tags);
assert::
same([2,
2,
1,
0],
$counts);
assert::
same([2,
2,
1,
0],
$countsStored);
}
public function testEmptyPreloadContainer()
{
/** @var Book[] $books */
$books = $this->orm->books->findAll()->orderBy('id');
$tags = [];
foreach ($books as $book) {
$book->setPreloadContainer(null);
foreach ($book->tags->get()->orderBy('name') as $tag) {
$tags[] = $tag->id;
}
}
assert::
same([1,
2,
2,
3,
3],
$tags);
}
public function testRemove()
{
$book = $this->orm->books->getById(1);
$tag = $this->orm->tags->getById(1);
$book->tags->remove($tag);
$this->orm->books->persistAndFlush($book);
assert::
same(1,
$book->
tags->
count());
assert::
same(1,
$book->
tags->
countStored());
}
public function testCollectionCountWithLimit()
{
$book = $this->orm->books->getById(1);
$collection = $book->tags->get();
$collection = $collection->orderBy('id')->limitBy(1, 1);
assert::
same(1,
$collection->
count());
}
public function testRawValue()
{
$book = $this->orm->books->getById(1);
assert::
same([1,
2],
$book->
tags->
getRawValue());
$book->tags->remove(1);
assert::
same([2],
$book->
tags->
getRawValue());
$tag = new Tag();
$tag->name = 'Test tag';
$tag->books->add($book);
assert::
same([2],
$book->
tags->
getRawValue());
$this->orm->tags->persistAndFlush($tag);
assert::
same([2,
4],
$book->
tags->
getRawValue());
$book->tags->setRawValue([]);
assert::
same([],
$book->
tags->
getRawValue());
$this->orm->tags->persistAndFlush($tag);
assert::
same([],
$book->
tags->
getRawValue());
}
public function testCaching()
{
$book = $this->orm->books->getById(1);
$tags = $book->tags->get()->findBy(['name' => 'Tag 1']);
assert::
same(1,
$tags->
count());
$tag = $tags->fetch();
$tag->name = 'XXX';
$this->orm->tags->persistAndFlush($tag);
$tags = $book->tags->get()->findBy(['name' => 'Tag 1']);
assert::
same(0,
$tags->
count());
}
public function testCachingPreload()
{
// init caches
$books = $this->orm->books->findAll();
foreach ($books as $book) {
iterator_to_array($book->tags);
}
$book = $this->orm->books->getById(2);
assert::
false($book->
tags->
has(1));
assert::
true($book->
tags->
has(2));
assert::
true($book->
tags->
has(3));
assert::
false($book->
tags->
has(4));
}
public function testIsModified()
{
$tag = new Tag('A');
$book = $this->orm->books->getById(1);
$book->tags->add($tag);
assert::
true($book->
tags->
isModified());
assert::
true($tag->
books->
isModified());
$tag = $this->orm->tags->getById(1);
$book->tags->remove($tag);
assert::
true($book->
tags->
isModified());
assert::
true($tag->
books->
isModified());
}
public function testSelfReferencing()
{
if ($this->section === Helper::SECTION_MSSQL) {
// An explicit value for the identity column in table 'users' can only be specified when a column list is used and IDENTITY_INSERT is ON.
// http://stackoverflow.com/questions/2148091/syntax-for-inserting-into-a-table-with-no-values
Environment::skip('Inserting dummy rows when no arguments are passed is not supported.');
}
$userA = new User();
$this->orm->persistAndFlush($userA);
$userB = new User();
$userB->myFriends->add($userA);
$this->orm->persistAndFlush($userB);
assert::
same(1,
$userA->
friendsWithMe->
count());
assert::
same(0,
$userA->
myFriends->
count());
}
public function testRepeatedPersisting()
{
$tagA = new Tag('A');
$tagB = new Tag('B');
$book = $this->orm->books->getById(1);
$book->tags->add($tagA);
$book->tags->add($tagB);
$this->orm->persistAndFlush($book);
assert::
false($tagA->
isModified());
assert::
false($tagB->
isModified());
$tagA->name = 'X';
$this->orm->persistAndFlush($book);
assert::
false($tagA->
isModified());
assert::
false($tagB->
isModified());
}
public function testCountStoredOnManyToManyCondition()
{
$books = $this->orm->books->findBy(['this->tags->name' => 'Tag 2']);
assert::
same(2,
$books->
countStored());
}
}
$test = new RelationshipManyHasManyTest($dic);
$test->run();
?>