1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Tested composite keys on non cache-able entities

This commit is contained in:
Asmir Mustafic 2015-02-20 10:12:40 +01:00
parent d72ad9cc50
commit 4d43f5ca52
5 changed files with 161 additions and 17 deletions

View File

@ -0,0 +1,77 @@
<?php
namespace Doctrine\Tests\Models\Cache;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table("cache_complex_action")
*/
class ComplexAction
{
const CLASSNAME = __CLASS__;
/**
* @Column
*/
private $name;
/**
* @Id
* @OneToOne(targetEntity="Action", cascade={"persist", "remove"})
* @JoinColumn(name="action1_id", referencedColumnName="id")
*/
private $action1;
/**
* @Id
* @OneToOne(targetEntity="Action", cascade={"persist", "remove"})
* @JoinColumn(name="action2_id", referencedColumnName="id")
*/
private $action2;
/**
* @OneToMany(targetEntity="Token", cascade={"persist", "remove"}, mappedBy="complexAction")
*/
private $tokens;
public function __construct(Action $action1, Action $action2, $name)
{
$this->name = $name;
$this->action1 = $action1;
$this->action2 = $action2;
$this->tokens = new ArrayCollection();
}
public function getAction1()
{
return $this->action1;
}
public function getAction2()
{
return $this->action2;
}
public function addToken(Token $token)
{
$this->tokens[] = $token;
$token->setComplexAction($this);
}
public function getTokens()
{
return $this->tokens;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
}

View File

@ -41,14 +41,37 @@ class Token
*/
protected $action;
/**
* @ManyToOne(targetEntity="ComplexAction", cascade={"persist", "remove"}, inversedBy="tokens")
* @JoinColumns({
* @JoinColumn(name="complex_action1_id", referencedColumnName="action1_id"),
* @JoinColumn(name="complex_action2_id", referencedColumnName="action2_id")
* })
* @var ComplexAction
*/
protected $complexAction;
public function __construct($token, Client $client = null)
{
$this->token = $token;
$this->logins = new ArrayCollection();
$this->token = $token;
$this->client = $client;
$this->expiresAt = new \DateTime(date('Y-m-d H:i:s', strtotime("+7 day")));
}
/**
* @return ComplexAction
*/
public function getComplexAction()
{
return $this->complexAction;
}
public function setComplexAction(ComplexAction $complexAction)
{
$this->complexAction = $complexAction;
}
/**
* @return array
*/
@ -95,14 +118,4 @@ class Token
{
$this->token = $token;
}
public function setExpiresAt(DateTime $expiresAt)
{
$this->expiresAt = $expiresAt;
}
public function setClient(Client $client)
{
$this->client = $client;
}
}

View File

@ -2,6 +2,7 @@
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\Cache\ComplexAction;
use Doctrine\Tests\Models\Cache\Country;
use Doctrine\Tests\Models\Cache\State;
use Doctrine\ORM\Mapping\ClassMetadata;
@ -170,6 +171,58 @@ class SecondLevelCacheManyToOneTest extends SecondLevelCacheAbstractTest
$this->assertEquals('exec', $entity->getAction()->getName());
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
}
public function testPutAndLoadNonCacheableCompositeManyToOne()
{
$this->assertNull($this->cache->getEntityCacheRegion(Action::CLASSNAME));
$this->assertNull($this->cache->getEntityCacheRegion(ComplexAction::CLASSNAME));
$this->assertInstanceOf('Doctrine\ORM\Cache\Region', $this->cache->getEntityCacheRegion(Token::CLASSNAME));
$token = new Token('token-hash');
$action1 = new Action('login');
$action2 = new Action('logout');
$action3 = new Action('rememberme');
$complexAction = new ComplexAction($action1, $action3, 'login,rememberme');
$complexAction->addToken($token);
$token->setAction($action2);
$this->_em->persist($token);
$this->_em->flush();
$this->_em->clear();
$this->assertTrue($this->cache->containsEntity(Token::CLASSNAME, $token->getToken()));
$this->assertFalse($this->cache->containsEntity(Action::CLASSNAME, $action1->getId()));
$this->assertFalse($this->cache->containsEntity(Action::CLASSNAME, $action2->getId()));
$this->assertFalse($this->cache->containsEntity(Action::CLASSNAME, $action3->getId()));
$queryCount = $this->getCurrentQueryCount();
/**
* @var $entity Token
*/
$entity = $this->_em->find(Token::CLASSNAME, $token->getToken());
$this->assertInstanceOf(Token::CLASSNAME, $entity);
$this->assertEquals('token-hash', $entity->getToken());
$this->assertEquals($queryCount, $this->getCurrentQueryCount());
$this->assertInstanceOf(Action::CLASSNAME, $entity->getAction());
$this->assertInstanceOf(ComplexAction::CLASSNAME, $entity->getComplexAction());
$this->assertEquals($queryCount, $this->getCurrentQueryCount());
$this->assertInstanceOf(Action::CLASSNAME, $entity->getComplexAction()->getAction1());
$this->assertInstanceOf(Action::CLASSNAME, $entity->getComplexAction()->getAction2());
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
$this->assertEquals('login', $entity->getComplexAction()->getAction1()->getName());
$this->assertEquals($queryCount + 2, $this->getCurrentQueryCount());
$this->assertEquals('rememberme', $entity->getComplexAction()->getAction2()->getName());
$this->assertEquals($queryCount + 3, $this->getCurrentQueryCount());
}
}

View File

@ -392,18 +392,17 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
$l1 = new Login('session1');
$l2 = new Login('session2');
$token = new Token('token-hash');
$token->addLogin($l1);
$token->addLogin($l2);
$this->_em->persist($token);
$this->_em->flush();
$token->addLogin($l1);
$token->addLogin($l2);
$this->_em->flush();
$this->_em->clear();
$queryCount = $this->getCurrentQueryCount();
$this->assertTrue($this->cache->containsEntity(Token::CLASSNAME, $token->getToken()));
$queryCount = $this->getCurrentQueryCount();
$entity = $this->_em->find(Token::CLASSNAME, $token->getToken());
$this->assertInstanceOf(Token::CLASSNAME, $entity);
@ -411,6 +410,6 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
$this->assertEquals($queryCount, $this->getCurrentQueryCount());
$this->assertCount(2, $entity->getLogins());
$this->assertEquals($queryCount + 1 , $this->getCurrentQueryCount());
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
}
}

View File

@ -184,6 +184,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\Cache\Login',
'Doctrine\Tests\Models\Cache\Client',
'Doctrine\Tests\Models\Cache\Action',
'Doctrine\Tests\Models\Cache\ComplexAction',
'Doctrine\Tests\Models\Cache\AttractionInfo',
'Doctrine\Tests\Models\Cache\AttractionContactInfo',
'Doctrine\Tests\Models\Cache\AttractionLocationInfo'
@ -419,6 +420,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
$conn->executeUpdate('DELETE FROM cache_state');
$conn->executeUpdate('DELETE FROM cache_country');
$conn->executeUpdate('DELETE FROM cache_login');
$conn->executeUpdate('DELETE FROM cache_complex_action');
$conn->executeUpdate('DELETE FROM cache_token');
$conn->executeUpdate('DELETE FROM cache_action');
$conn->executeUpdate('DELETE FROM cache_client');