Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
66f377fb
Commit
66f377fb
authored
Jul 17, 2009
by
piccoloprincipe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] extended tests for proxy class generation
parent
8fbee579
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
30 deletions
+73
-30
ProxyClassGenerator.php
lib/Doctrine/ORM/Proxy/ProxyClassGenerator.php
+22
-22
ProxyClassGeneratorTest.php
tests/Doctrine/Tests/ORM/Proxy/ProxyClassGeneratorTest.php
+51
-8
No files found.
lib/Doctrine/ORM/Proxy/ProxyClassGenerator.php
View file @
66f377fb
...
...
@@ -58,24 +58,26 @@ class ProxyClassGenerator
* Generates a reference proxy class.
* This is a proxy for an object which we have the id for retrieval.
*
* @param string $className
* @param string $proxyClassName
* @param string $fileName
* @param string $originalClassName
* @return string name of the proxy class
*/
public
function
generateReferenceProxyClass
(
$
c
lassName
)
public
function
generateReferenceProxyClass
(
$
originalC
lassName
)
{
$class
=
$this
->
_em
->
getClassMetadata
(
$className
);
$proxyClassName
=
str_replace
(
'\\'
,
'_'
,
$className
)
.
'RProxy'
;
$proxyClassName
=
str_replace
(
'\\'
,
'_'
,
$originalClassName
)
.
'RProxy'
;
//$proxyClassName = $originalClassName . 'RProxy';
$proxyFullyQualifiedClassName
=
self
::
$_ns
.
$proxyClassName
;
if
(
!
class_exists
(
$proxyClassName
,
false
))
{
$this
->
_em
->
getMetadataFactory
()
->
setMetadataFor
(
self
::
$_ns
.
$proxyClassName
,
$class
);
$fileName
=
$this
->
_cacheDir
.
$proxyClassName
.
'.g.php'
;
if
(
class_exists
(
$proxyFullyQualifiedClassName
,
false
))
{
return
$proxyFullyQualifiedClassName
;
}
$class
=
$this
->
_em
->
getClassMetadata
(
$originalClassName
);
$this
->
_em
->
getMetadataFactory
()
->
setMetadataFor
(
$proxyFullyQualifiedClassName
,
$class
);
$fileName
=
$this
->
_cacheDir
.
$proxyClassName
.
'.g.php'
;
if
(
file_exists
(
$fileName
))
{
require
$fileName
;
$proxyClassName
=
'\\'
.
self
::
$_ns
.
$proxyClassName
;
return
$proxyClassName
;
}
if
(
file_exists
(
$fileName
))
{
require
$fileName
;
return
$proxyFullyQualifiedClassName
;
}
$file
=
self
::
$_proxyClassTemplate
;
...
...
@@ -87,7 +89,7 @@ class ProxyClassGenerator
'<methods>'
,
'<sleepImpl>'
);
$replacements
=
array
(
$proxyClassName
,
$
c
lassName
,
$methods
,
$sleepImpl
$proxyClassName
,
$
originalC
lassName
,
$methods
,
$sleepImpl
);
$file
=
str_replace
(
$placeholders
,
$replacements
,
$file
);
...
...
@@ -95,9 +97,7 @@ class ProxyClassGenerator
file_put_contents
(
$fileName
,
$file
);
require
$fileName
;
$proxyClassName
=
'\\'
.
self
::
$_ns
.
$proxyClassName
;
return
$proxyClassName
;
return
$proxyFullyQualifiedClassName
;
}
protected
function
_generateMethods
(
ClassMetadata
$class
)
...
...
@@ -172,12 +172,12 @@ class ProxyClassGenerator
* This is a proxy class for an object which we have the association where
* it is involved, but no primary key to retrieve it.
*
* @param string $
c
lassName
* @param string $
originalC
lassName
* @param string $proxyClassName
*/
public
function
generateAssociationProxyClass
(
$
c
lassName
,
$proxyClassName
)
public
function
generateAssociationProxyClass
(
$
originalC
lassName
,
$proxyClassName
)
{
$class
=
$this
->
_em
->
getClassMetadata
(
$
c
lassName
);
$class
=
$this
->
_em
->
getClassMetadata
(
$
originalC
lassName
);
$file
=
self
::
$_assocProxyClassTemplate
;
$methods
=
''
;
...
...
@@ -231,7 +231,7 @@ class ProxyClassGenerator
'<methods>'
,
'<sleepImpl>'
);
$replacements
=
array
(
$proxyClassName
,
$
c
lassName
,
$methods
,
$sleepImpl
$proxyClassName
,
$
originalC
lassName
,
$methods
,
$sleepImpl
);
$file
=
str_replace
(
$placeholders
,
$replacements
,
$file
);
...
...
tests/Doctrine/Tests/ORM/Proxy/ProxyClassGeneratorTest.php
View file @
66f377fb
...
...
@@ -9,6 +9,7 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use
Doctrine\Tests\Models\ECommerce\ECommerceCustomer
;
use
Doctrine\Tests\Models\ECommerce\ECommerceFeature
;
use
Doctrine\Tests\Models\ECommerce\ECommerceShipping
;
use
Doctrine\ORM\Persisters\StandardEntityPersister
;
require_once
__DIR__
.
'/../../TestInit.php'
;
...
...
@@ -59,15 +60,57 @@ class ProxyClassGeneratorTest extends \Doctrine\Tests\OrmTestCase
$this
->
assertTrue
(
is_subclass_of
(
$proxyClass
,
'\Doctrine\Tests\Models\ECommerce\ECommerceCart'
));
}
public
function
_testGenerateProxiesWhichForwardsToTheModelWithTheGivenIdentifier
()
public
function
testAllowsIdempotentCreationOfProxyClass
()
{
$feature
=
new
ECommerceFeature
;
$feature
->
setDescription
(
'An interesting feature'
);
$this
->
_emMock
->
save
(
$feature
);
$id
=
$feature
->
getId
();
$this
->
_emMock
->
clear
();
$proxyClass
=
$this
->
_generator
->
generateReferenceProxyClass
(
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
);
$theSameProxyClass
=
$this
->
_generator
->
generateReferenceProxyClass
(
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
);
$this
->
assertEquals
(
$proxyClass
,
$theSameProxyClass
);
}
public
function
testCreatesClassesThatRequirePersisterInTheConstructor
()
{
$proxyClass
=
$this
->
_generator
->
generateReferenceProxyClass
(
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
);
$proxy
=
new
$proxyClass
(
$this
->
_getMockPersister
(),
null
);
}
public
function
testCreatesClassesThatDelegateLoadingToThePersister
()
{
$identifier
=
array
(
'id'
=>
42
);
$proxyClass
=
$this
->
_generator
->
generateReferenceProxyClass
(
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
);
$persister
=
$this
->
_getMockPersister
();
$proxy
=
new
$proxyClass
(
$persister
,
$identifier
);
$persister
->
expects
(
$this
->
any
())
->
method
(
'load'
)
->
with
(
$this
->
equalTo
(
$identifier
),
$this
->
isInstanceOf
(
$proxyClass
));
$proxy
->
getDescription
();
}
$proxy
=
$this
->
_generator
->
generateReferenceProxyClass
(
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
,
1
);
$this
->
assertEquals
(
'An interesting feature'
,
$proxy
->
getDescription
());
public
function
testCreatesClassesThatExecutesLoadingOnlyOnce
()
{
$identifier
=
array
(
'id'
=>
42
);
$proxyClass
=
$this
->
_generator
->
generateReferenceProxyClass
(
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
);
$persister
=
$this
->
_getMockPersister
();
$proxy
=
new
$proxyClass
(
$persister
,
$identifier
);
$persister
->
expects
(
$this
->
once
())
->
method
(
'load'
)
->
with
(
$this
->
equalTo
(
$identifier
),
$this
->
isInstanceOf
(
$proxyClass
));
$proxy
->
getId
();
$proxy
->
getDescription
();
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public
function
testRespectsMethodsParametersTypeHinting
()
{
$proxyClass
=
$this
->
_generator
->
generateReferenceProxyClass
(
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
);
$proxy
=
new
$proxyClass
(
$this
->
_getMockPersister
(),
null
);
$proxy
->
setProduct
(
array
(
'invalid parameter'
));
}
protected
function
_getMockPersister
()
{
$persister
=
$this
->
getMock
(
'Doctrine\ORM\Persisters\StandardEntityPersister'
,
array
(
'load'
),
array
(),
''
,
false
);
return
$persister
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment