Skip to content

Instantly share code, notes, and snippets.

@benbor benbor/GH6344Test.php
Last active Mar 16, 2017

Embed
What would you like to do?
<?php
/**
* Test case for https://github.com/doctrine/doctrine2/issues/6344 which doesn't work for last stable
* branch (2.5 at now) but works for master.
*
* My environment:
* PHP 7.0.14-2+deb.sury.org~trusty+1 (cli) ( NTS )
* Copyright (c) 1997-2016 The PHP Group
* Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
* with Zend OPcache v7.0.14-2+deb.sury.org~trusty+1, Copyright (c) 1999-2016, by Zend Technologies
* with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans
*
* run test: ./vendor/bin/phpunit -c sqlite.phpunit.xml tests/Doctrine/Tests/ORM/Functional/Ticket/GH6344Test.php
*/
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\OrmFunctionalTestCase;
class GH6344Test extends OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(Post::class),
$this->_em->getClassMetadata(Tag::class),
]
);
}
/**
*/
public function testOrphalRemove()
{
$post = new Post();
$post->tags->add(new Tag($post, 'a'));
$post->tags->add(new Tag($post, 'b'));
$this->_em->persist($post);
$this->_em->flush();
$this->_em->clear();
$tags = $this->_em->getRepository(Tag::class)->findAll();
self::assertCount(2, $tags);
self::assertEquals('a', $tags[0]->name);
self::assertEquals('b', $tags[1]->name);
$post = $this->_em->find(Post::class, 1);
$tagA = $this->_em->find(Tag::class, 1);
$post->tags->clear();
$post->tags->add($tagA);
$post->tags->add(new Tag($post, 'c'));
$this->_em->persist($post);
$this->_em->flush();
$this->_em->clear();
$tags = $this->_em->getRepository(Tag::class)->findAll();
self::assertCount(2, $tags);
self::assertEquals('a', $tags[0]->name);
self::assertEquals('c', $tags[1]->name);
}
}
/**
* @Entity
* @Table(name="ticket_benbor_post_objects")
*/
class Post
{
/**
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*
* @Type("ArrayCollection<Tag>")
* @OneToMany(targetEntity="Tag", mappedBy="post", cascade={"all"}, orphanRemoval=true)
*/
public $tags;
/**
* Post constructor.
*/
public function __construct()
{
$this->tags = new ArrayCollection();
}
}
/**
* @Entity
* @Table(name="ticket_benbor_tag_objects")
*/
class Tag
{
/**
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ManyToOne(targetEntity="Post", inversedBy="tags")
* @JoinColumn(name="post_id", referencedColumnName="id", nullable=true)
*
*/
public $post;
/**
* @var
* @Column(name="name", type="string")
*/
public $name;
/**
* Tag constructor.
* @param $post
* @param $name
*/
public function __construct($post, $name)
{
$this->post = $post;
$this->name = $name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.