<?php declare(strict_types = 1);
/**
* @testCase
* @dataProvider ../../../sections.ini
*/
namespace NextrasTestsOrmIntegrationRelationships;
use Mockery;
use NextrasOrmInvalidStateException;
use NextrasOrmModelIModel;
use NextrasTestsOrmAuthor;
use NextrasTestsOrmBook;
use NextrasTestsOrmDataTestCase;
use NextrasTestsOrmPhoto;
use NextrasTestsOrmPhotoAlbum;
use NextrasTestsOrmPublisher;
use TesterAssert;
$dic = require_once __DIR__ . '/../../../bootstrap.php';
class RelationshipCyclicTest extends DataTestCase
{
public function testNotCycle()
{
$publisher = new Publisher();
$publisher->name = 'Jupiter Mining Corporation';
$author = new Author();
$author->name = 'Arnold Judas Rimmer';
$translator = new Author();
$translator->name = 'Dave Lister';
$translator->favoredBy->add($author);
$book = new Book();
$book->title = 'Better Than Life';
$book->publisher = $publisher;
$book->author = $author;
$book->translator = $translator;
$this->orm->persist($author);
assert::
true($publisher->
isPersisted());
assert::
true($author->
isPersisted());
assert::
true($translator->
isPersisted());
assert::
true($book->
isPersisted());
}
public function testCycleCheck()
{
$album = new PhotoAlbum();
$album->title = 'album 1';
$photo1 = new Photo();
$photo1->title = 'photo 1';
$photo1->album = $album;
$photo2 = new Photo();
$photo2->title = 'photo 2';
$photo2->album = $album;
$photo3 = new Photo();
$photo3->title = 'photo 3';
$photo3->album = $album;
$album->preview = $photo2;
assert::
throws(function () use
($album) {
$this->orm->persist($album);
}, InvalidStateException::class, 'Persist cycle detected in NextrasTestsOrmPhoto::$album - NextrasTestsOrmPhotoAlbum::$preview. Use manual two phase persist.');
assert::
throws(function () use
($photo2) {
$this->orm->persist($photo2);
}, InvalidStateException::class, 'Persist cycle detected in NextrasTestsOrmPhotoAlbum::$preview - NextrasTestsOrmPhoto::$album. Use manual two phase persist.');
}
public function testCycleManualPersist()
{
$album = new PhotoAlbum();
$album->title = 'album 1';
$photo1 = new Photo();
$photo1->title = 'photo 1';
$photo1->album = $album;
$photo2 = new Photo();
$photo2->title = 'photo 2';
$photo2->album = $album;
$photo3 = new Photo();
$photo3->title = 'photo 3';
$photo3->album = $album;
$this->orm->persist($album);
$album->preview = $photo2;
$this->orm->persist($album);
assert::
true($album->
isPersisted());
assert::
true($photo1->
isPersisted());
assert::
true($photo2->
isPersisted());
assert::
true($photo3->
isPersisted());
}
}
$test = new RelationshipCyclicTest($dic);
$test->run();
?>