<?php declare(strict_types = 1);
/**
* @testCase
* @dataProvider ../../../sections.ini
*/
namespace NextrasTestsOrmIntegrationRelationships;
use NextrasDbalConnection;
use NextrasDbalIConnection;
use NextrasOrmRelationshipsManyHasMany;
use NextrasTestsOrmAuthor;
use NextrasTestsOrmBook;
use NextrasTestsOrmDataTestCase;
use NextrasTestsOrmHelper;
use NextrasTestsOrmPublisher;
use NextrasTestsOrmTag;
use TesterAssert;
use TesterEnvironment;
$dic = require_once __DIR__ . '/../../../bootstrap.php';
class EntityRelationshipsTest extends DataTestCase
{
public function testBasics()
{
$author = new Author();
$author->name = 'Jon Snow';
$publisher = new Publisher();
$publisher->name = '7K';
$book = new Book();
$book->title = 'A new book';
$book->author = $author;
$book->publisher = $publisher;
$book->tags->set([new Tag('Awesome')]);
$this->orm->books->persistAndFlush($book);
assert::
true($author->
isAttached());
assert::
true($author->
isPersisted());
assert::
false($author->
isModified());
assert::
true($book->
isAttached());
assert::
true($book->
isPersisted());
assert::
false($book->
isModified());
assert::
same(1,
$book->
tags->
count());
assert::
same(1,
$book->
tags->
countStored());
assert::
same('Awesome',
$book->
tags->
get()->
fetch()->
name);
$book->tags = [];
assert::
type(ManyHasMany::
class,
$book->
tags);
assert::
same(0,
$book->
tags->
count());
}
public function testDeepTraversalHasOne()
{
if ($this->section === Helper::SECTION_ARRAY) {
Environment::skip();
}
$queries = [];
$connection = $this->container->getByType(Connection::class);
$connection->onQuery[] = function ($_, $query) use (& $queries) {
$queries[$query] = $queries[$query] ?? 1;
};
$authors = [];
foreach ($this->orm->tags->findAll() as $tag) {
foreach ($tag->books as $book) {
$authors[] = $book->author->id;
}
}
assert::
same([1,
1,
1,
1,
2],
$authors);
return $count != 1;
}));
}
public function testDeepTraversalManyHasMany()
{
if ($this->section === Helper::SECTION_ARRAY) {
Environment::skip();
}
$queries = [];
$connection = $this->container->getByType(IConnection::class);
$connection->onQuery[] = function ($_, $query) use (& $queries) {
$queries[$query] =
isset($queries[$query]) ?
$queries[$query] :
1;
};
$tags = [];
foreach ($this->orm->authors->findAll() as $author) {
foreach ($author->books as $book) {
foreach ($book->tags as $tag) {
$tags[] = $tag->id;
}
}
}
assert::
same([2,
3,
1,
2,
3],
$tags);
return $count != 1;
}));
}
}
$test = new EntityRelationshipsTest($dic);
$test->run();
?>