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
49103096
Commit
49103096
authored
Jul 06, 2009
by
piccoloprincipe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added one-one self referential functional tests (closes #2276)
parent
ff115efb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
0 deletions
+104
-0
ECommerceCustomer.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php
+24
-0
AllTests.php
tests/Doctrine/Tests/ORM/Functional/AllTests.php
+1
-0
OneToOneSelfReferentialAssociationTest.php
...ORM/Functional/OneToOneSelfReferentialAssociationTest.php
+79
-0
No files found.
tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php
View file @
49103096
...
...
@@ -28,6 +28,15 @@ class ECommerceCustomer
* @OneToOne(targetEntity="ECommerceCart", mappedBy="customer", cascade={"save"})
*/
private
$cart
;
/**
* Example of a one-one self referential association. A mentor can follow
* only one customer at the time, while a customer can choose only one
* mentor. Not properly appropriate but it works.
* @OneToOne(targetEntity="ECommerceCustomer", cascade={"save"})
* @JoinColumn(name="mentor_id", referencedColumnName="id")
*/
private
$mentor
;
public
function
getId
()
{
return
$this
->
id
;
...
...
@@ -66,4 +75,19 @@ class ECommerceCustomer
$cart
->
removeCustomer
();
}
}
public
function
setMentor
(
ECommerceCustomer
$mentor
)
{
$this
->
mentor
=
$mentor
;
}
public
function
removeMentor
()
{
$this
->
mentor
=
null
;
}
public
function
getMentor
()
{
return
$this
->
mentor
;
}
}
tests/Doctrine/Tests/ORM/Functional/AllTests.php
View file @
49103096
...
...
@@ -31,6 +31,7 @@ class AllTests
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToManyBidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\ManyToManyUnidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\ManyToManyBidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToOneSelfReferentialAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToManySelfReferentialAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\ManyToManySelfReferentialAssociationTest'
);
...
...
tests/Doctrine/Tests/ORM/Functional/OneToOneSelfReferentialAssociationTest.php
0 → 100644
View file @
49103096
<?php
namespace
Doctrine\Tests\ORM\Functional
;
use
Doctrine\Tests\Models\ECommerce\ECommerceCustomer
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
* Tests a self referential one-to-one association mapping (without inheritance).
* Relation is defined as the mentor that a customer choose. The mentor could
* help only one other customer, while a customer can choose only one mentor
* for receiving support.
* Inverse side is not present.
*/
class
OneToOneSelfReferentialAssociationTest
extends
\Doctrine\Tests\OrmFunctionalTestCase
{
private
$customer
;
private
$mentor
;
protected
function
setUp
()
{
$this
->
useModelSet
(
'ecommerce'
);
parent
::
setUp
();
$this
->
customer
=
new
ECommerceCustomer
();
$this
->
customer
->
setName
(
'Anakin Skywalker'
);
$this
->
mentor
=
new
ECommerceCustomer
();
$this
->
mentor
->
setName
(
'Obi-wan Kenobi'
);
}
public
function
testSavesAOneToOneAssociationWithCascadeSaveSet
()
{
$this
->
customer
->
setMentor
(
$this
->
mentor
);
$this
->
_em
->
save
(
$this
->
customer
);
$this
->
assertForeignKeyIs
(
$this
->
mentor
->
getId
());
}
public
function
testRemovesOneToOneAssociation
()
{
$this
->
customer
->
setMentor
(
$this
->
mentor
);
$this
->
_em
->
save
(
$this
->
customer
);
$this
->
customer
->
removeMentor
();
$this
->
_em
->
flush
();
$this
->
assertForeignKeyIs
(
null
);
}
public
function
testEagerLoad
()
{
$customer
=
new
ECommerceCustomer
;
$customer
->
setName
(
'Luke Skywalker'
);
$mentor
=
new
ECommerceCustomer
;
$mentor
->
setName
(
'Obi-wan Kenobi'
);
$customer
->
setMentor
(
$mentor
);
$this
->
_em
->
save
(
$customer
);
$this
->
_em
->
flush
();
$this
->
_em
->
clear
();
$query
=
$this
->
_em
->
createQuery
(
'select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m'
);
$result
=
$query
->
getResultList
();
$customer
=
$result
[
0
];
$this
->
assertTrue
(
$customer
->
getMentor
()
instanceof
ECommerceCustomer
);
$this
->
assertEquals
(
'Obi-wan Kenobi'
,
$customer
->
getMentor
()
->
getName
());
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
public
function
assertForeignKeyIs
(
$value
)
{
$foreignKey
=
$this
->
_em
->
getConnection
()
->
execute
(
'SELECT mentor_id FROM ecommerce_customers WHERE id=?'
,
array
(
$this
->
customer
->
getId
()))
->
fetchColumn
();
$this
->
assertEquals
(
$value
,
$foreignKey
);
}
}
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