[DDC-1301] Added tests for fetch="EXTRA_LAZY" count() on a "legacy" database
This commit is contained in:
parent
a0b7c3e76d
commit
b2951691e2
33
tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php
Normal file
33
tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\Models\Legacy;
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
* @Table(name="legacy_articles")
|
||||
*/
|
||||
class LegacyArticle
|
||||
{
|
||||
/**
|
||||
* @Id
|
||||
* @Column(name="iArticleId", type="integer")
|
||||
* @GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
public $id;
|
||||
/**
|
||||
* @Column(name="sTopic", type="string", length=255)
|
||||
*/
|
||||
public $topic;
|
||||
/**
|
||||
* @Column(name="sText", type="text")
|
||||
*/
|
||||
public $text;
|
||||
/**
|
||||
* @ManyToOne(targetEntity="LegacyUser", inversedBy="articles")
|
||||
* @JoinColumn(name="iUserId", referencedColumnName="iUserId")
|
||||
*/
|
||||
public $user;
|
||||
public function setAuthor(LegacyUser $author) {
|
||||
$this->user = $author;
|
||||
}
|
||||
}
|
41
tests/Doctrine/Tests/Models/Legacy/LegacyCar.php
Normal file
41
tests/Doctrine/Tests/Models/Legacy/LegacyCar.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\Models\Legacy;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
* @Table(name="legacy_cars")
|
||||
*/
|
||||
class LegacyCar
|
||||
{
|
||||
/**
|
||||
* @Id
|
||||
* @GeneratedValue
|
||||
* @Column(name="iCarId", type="integer", nullable=false)
|
||||
*/
|
||||
public $id;
|
||||
/**
|
||||
* @ManyToMany(targetEntity="LegacyUser", mappedBy="cars")
|
||||
*/
|
||||
public $users;
|
||||
|
||||
/**
|
||||
* @Column(name="sDescription", type="string", length=255, unique=true)
|
||||
*/
|
||||
public $description;
|
||||
|
||||
function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function addUser(LegacyUser $user) {
|
||||
$this->users[] = $user;
|
||||
}
|
||||
|
||||
public function getUsers() {
|
||||
return $this->users;
|
||||
}
|
||||
}
|
80
tests/Doctrine/Tests/Models/Legacy/LegacyUser.php
Normal file
80
tests/Doctrine/Tests/Models/Legacy/LegacyUser.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\Models\Legacy;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
* @Table(name="legacy_users")
|
||||
*/
|
||||
class LegacyUser
|
||||
{
|
||||
/**
|
||||
* @Id
|
||||
* @GeneratedValue
|
||||
* @Column(name="iUserId", type="integer", nullable=false)
|
||||
*/
|
||||
public $id;
|
||||
/**
|
||||
* @Column(name="sUsername", type="string", length=255, unique=true)
|
||||
*/
|
||||
public $username;
|
||||
/**
|
||||
* @Column(type="string", length=255)
|
||||
*/
|
||||
public $name;
|
||||
/**
|
||||
* @OneToMany(targetEntity="LegacyArticle", mappedBy="user")
|
||||
*/
|
||||
public $articles;
|
||||
/**
|
||||
* @OneToMany(targetEntity="LegacyUserReference", mappedBy="source", cascade={"remove"})
|
||||
*/
|
||||
public $references;
|
||||
/**
|
||||
* @ManyToMany(targetEntity="LegacyCar", inversedBy="users", cascade={"persist", "merge"})
|
||||
* @JoinTable(name="legace_users_cars",
|
||||
* joinColumns={@JoinColumn(name="iUserId", referencedColumnName="iUserId")},
|
||||
* inverseJoinColumns={@JoinColumn(name="iCarId", referencedColumnName="iCarId")}
|
||||
* )
|
||||
*/
|
||||
public $cars;
|
||||
public function __construct() {
|
||||
$this->articles = new ArrayCollection;
|
||||
$this->references = new ArrayCollection;
|
||||
$this->cars = new ArrayCollection;
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUsername() {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function addArticle(LegacyArticle $article) {
|
||||
$this->articles[] = $article;
|
||||
$article->setAuthor($this);
|
||||
}
|
||||
|
||||
public function addReference($reference)
|
||||
{
|
||||
$this->references[] = $reference;
|
||||
}
|
||||
|
||||
public function references()
|
||||
{
|
||||
return $this->references;
|
||||
}
|
||||
|
||||
public function addCar(LegacyCar $car) {
|
||||
$this->cars[] = $car;
|
||||
$car->addUser($this);
|
||||
}
|
||||
|
||||
public function getCars() {
|
||||
return $this->cars;
|
||||
}
|
||||
}
|
65
tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php
Normal file
65
tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\Models\Legacy;
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
* @Table(name="legacy_users_reference")
|
||||
*/
|
||||
class LegacyUserReference
|
||||
{
|
||||
/**
|
||||
* @Id
|
||||
* @ManyToOne(targetEntity="LegacyUser", inversedBy="references")
|
||||
* @JoinColumn(name="iUserIdSource", referencedColumnName="iUserId")
|
||||
*/
|
||||
private $source;
|
||||
|
||||
/**
|
||||
* @Id
|
||||
* @ManyToOne(targetEntity="LegacyUser", inversedBy="references")
|
||||
* @JoinColumn(name="iUserIdTarget", referencedColumnName="iUserId")
|
||||
*/
|
||||
private $target;
|
||||
|
||||
/**
|
||||
* @column(type="string")
|
||||
*/
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @column(type="datetime")
|
||||
*/
|
||||
private $created;
|
||||
|
||||
public function __construct($source, $target, $description)
|
||||
{
|
||||
$source->addReference($this);
|
||||
$target->addReference($this);
|
||||
|
||||
$this->source = $source;
|
||||
$this->target = $target;
|
||||
$this->description = $description;
|
||||
$this->created = new \DateTime("now");
|
||||
}
|
||||
|
||||
public function source()
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
public function target()
|
||||
{
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
public function setDescription($desc)
|
||||
{
|
||||
$this->description = $desc;
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
}
|
148
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php
Normal file
148
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
||||
|
||||
require_once __DIR__ . '/../../../TestInit.php';
|
||||
|
||||
/**
|
||||
* @author asm89
|
||||
*/
|
||||
class DDC1301Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||
{
|
||||
private $userId;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->useModelSet('legacy');
|
||||
parent::setUp();
|
||||
|
||||
$class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser');
|
||||
$class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
|
||||
$class->associationMappings['references']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
|
||||
$class->associationMappings['cars']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
|
||||
|
||||
$this->loadFixture();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser');
|
||||
$class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
|
||||
$class->associationMappings['references']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
|
||||
$class->associationMappings['cars']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
|
||||
}
|
||||
|
||||
public function testCountNotInitializesLegacyCollection()
|
||||
{
|
||||
$user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
|
||||
$this->assertFalse($user->articles->isInitialized());
|
||||
$this->assertEquals(2, count($user->articles));
|
||||
$this->assertFalse($user->articles->isInitialized());
|
||||
|
||||
foreach ($user->articles AS $article) { }
|
||||
|
||||
$this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
|
||||
}
|
||||
|
||||
public function testCountNotInitializesLegacyCollectionWithForeignIdentifier()
|
||||
{
|
||||
$user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
|
||||
$this->assertFalse($user->references->isInitialized());
|
||||
$this->assertEquals(2, count($user->references));
|
||||
$this->assertFalse($user->references->isInitialized());
|
||||
|
||||
foreach ($user->references AS $reference) { }
|
||||
|
||||
$this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
|
||||
}
|
||||
|
||||
public function testCountNotInitializesLegacyManyToManyCollection()
|
||||
{
|
||||
$user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
|
||||
$this->assertFalse($user->cars->isInitialized());
|
||||
$this->assertEquals(3, count($user->cars));
|
||||
$this->assertFalse($user->cars->isInitialized());
|
||||
|
||||
foreach ($user->cars AS $reference) { }
|
||||
|
||||
$this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
|
||||
}
|
||||
|
||||
public function loadFixture()
|
||||
{
|
||||
$user1 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
|
||||
$user1->username = "beberlei";
|
||||
$user1->name = "Benjamin";
|
||||
$user1->status = "active";
|
||||
|
||||
$user2 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
|
||||
$user2->username = "jwage";
|
||||
$user2->name = "Jonathan";
|
||||
$user2->status = "active";
|
||||
|
||||
$user3 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
|
||||
$user3->username = "romanb";
|
||||
$user3->name = "Roman";
|
||||
$user3->status = "active";
|
||||
|
||||
$this->_em->persist($user1);
|
||||
$this->_em->persist($user2);
|
||||
$this->_em->persist($user3);
|
||||
|
||||
$article1 = new \Doctrine\Tests\Models\Legacy\LegacyArticle();
|
||||
$article1->topic = "Test";
|
||||
$article1->text = "Test";
|
||||
$article1->setAuthor($user1);
|
||||
|
||||
$article2 = new \Doctrine\Tests\Models\Legacy\LegacyArticle();
|
||||
$article2->topic = "Test";
|
||||
$article2->text = "Test";
|
||||
$article2->setAuthor($user1);
|
||||
|
||||
$this->_em->persist($article1);
|
||||
$this->_em->persist($article2);
|
||||
|
||||
$car1 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
|
||||
$car1->description = "Test1";
|
||||
|
||||
$car2 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
|
||||
$car2->description = "Test2";
|
||||
|
||||
$car3 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
|
||||
$car3->description = "Test3";
|
||||
|
||||
$user1->addCar($car1);
|
||||
$user1->addCar($car2);
|
||||
$user1->addCar($car3);
|
||||
|
||||
$user2->addCar($car1);
|
||||
$user3->addCar($car1);
|
||||
|
||||
$this->_em->persist($car1);
|
||||
$this->_em->persist($car2);
|
||||
$this->_em->persist($car3);
|
||||
|
||||
$this->_em->flush();
|
||||
|
||||
$detail1 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user2, "foo");
|
||||
$detail2 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user3, "bar");
|
||||
|
||||
$this->_em->persist($detail1);
|
||||
$this->_em->persist($detail2);
|
||||
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$this->userId = $user1->getId();
|
||||
}
|
||||
}
|
@ -104,6 +104,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
'Doctrine\Tests\Models\StockExchange\Stock',
|
||||
'Doctrine\Tests\Models\StockExchange\Market',
|
||||
),
|
||||
'legacy' => array(
|
||||
'Doctrine\Tests\Models\Legacy\LegacyUser',
|
||||
'Doctrine\Tests\Models\Legacy\LegacyUserReference',
|
||||
'Doctrine\Tests\Models\Legacy\LegacyArticle',
|
||||
'Doctrine\Tests\Models\Legacy\LegacyCar',
|
||||
),
|
||||
);
|
||||
|
||||
protected function useModelSet($setName)
|
||||
@ -202,6 +208,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
$conn->executeUpdate('DELETE FROM exchange_stocks');
|
||||
$conn->executeUpdate('DELETE FROM exchange_markets');
|
||||
}
|
||||
if (isset($this->_usedModelSets['legacy'])) {
|
||||
$conn->executeUpdate('DELETE FROM legacy_articles');
|
||||
$conn->executeUpdate('DELETE FROM legacy_cars');
|
||||
$conn->executeUpdate('DELETE FROM legacy_users');
|
||||
$conn->executeUpdate('DELETE FROM legacy_users_reference');
|
||||
}
|
||||
|
||||
$this->_em->clear();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user