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
f1885cb7
Commit
f1885cb7
authored
Jul 01, 2009
by
romanb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] adjustments to new bidirectional one-one association test from Giorgio Sironi.
parent
4e50792a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
30 deletions
+105
-30
ECommerceCart.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php
+36
-3
ECommerceCustomer.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php
+34
-6
AllTests.php
tests/Doctrine/Tests/ORM/Functional/AllTests.php
+1
-0
OneToOneBidirectionalAssociationTest.php
...s/ORM/Functional/OneToOneBidirectionalAssociationTest.php
+34
-21
No files found.
tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php
View file @
f1885cb7
...
@@ -17,16 +17,49 @@ class ECommerceCart
...
@@ -17,16 +17,49 @@ class ECommerceCart
* @Id
* @Id
* @GeneratedValue(strategy="AUTO")
* @GeneratedValue(strategy="AUTO")
*/
*/
p
ublic
$id
;
p
rivate
$id
;
/**
/**
* @Column(type="string", length=50)
* @Column(type="string", length=50)
*/
*/
p
ublic
$payment
;
p
rivate
$payment
;
/**
/**
* @OneToOne(targetEntity="ECommerceCustomer")
* @OneToOne(targetEntity="ECommerceCustomer")
* @JoinColumn(name="customer_id", referencedColumnName="id")
* @JoinColumn(name="customer_id", referencedColumnName="id")
*/
*/
public
$customer
;
private
$customer
;
public
function
getId
()
{
return
$this
->
id
;
}
public
function
getPayment
()
{
return
$this
->
payment
;
}
public
function
setPayment
(
$payment
)
{
$this
->
payment
=
$payment
;
}
public
function
setCustomer
(
ECommerceCustomer
$customer
)
{
if
(
$this
->
customer
!==
$customer
)
{
$this
->
customer
=
$customer
;
$customer
->
setCart
(
$this
);
}
}
public
function
removeCustomer
()
{
if
(
$this
->
customer
!==
null
)
{
$customer
=
$this
->
customer
;
$this
->
customer
=
null
;
if
(
$customer
->
getCart
()
!==
null
)
{
$customer
->
removeCart
();
}
}
}
public
function
getCustomer
()
{
return
$this
->
customer
;
}
}
}
tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php
View file @
f1885cb7
...
@@ -17,27 +17,55 @@ class ECommerceCustomer
...
@@ -17,27 +17,55 @@ class ECommerceCustomer
* @Id
* @Id
* @GeneratedValue(strategy="AUTO")
* @GeneratedValue(strategy="AUTO")
*/
*/
p
ublic
$id
;
p
rivate
$id
;
/**
/**
* @Column(type="string", length=50)
* @Column(type="string", length=50)
*/
*/
p
ublic
$name
;
p
rivate
$name
;
/**
/**
* @OneToOne(targetEntity="ECommerceCart", mappedBy="customer", cascade={"save"})
* @OneToOne(targetEntity="ECommerceCart", mappedBy="customer", cascade={"save"})
*/
*/
public
$cart
;
private
$cart
;
public
function
getId
()
{
return
$this
->
id
;
}
public
function
getName
()
{
return
$this
->
name
;
}
public
function
setName
(
$name
)
{
$this
->
name
=
$name
;
}
public
function
setCart
(
ECommerceCart
$cart
)
public
function
setCart
(
ECommerceCart
$cart
)
{
{
if
(
$this
->
cart
!==
$cart
)
{
$this
->
cart
=
$cart
;
$cart
->
setCustomer
(
$this
);
}
}
/* Does not properly maintain the bidirectional association! */
public
function
brokenSetCart
(
ECommerceCart
$cart
)
{
$this
->
cart
=
$cart
;
$this
->
cart
=
$cart
;
$cart
->
customer
=
$this
;
}
public
function
getCart
()
{
return
$this
->
cart
;
}
}
public
function
removeCart
()
public
function
removeCart
()
{
{
$this
->
cart
->
customer
=
null
;
if
(
$this
->
cart
!==
null
)
{
$this
->
cart
=
null
;
$cart
=
$this
->
cart
;
$this
->
cart
=
null
;
if
(
$cart
->
getCustomer
()
!==
null
)
{
$cart
->
removeCustomer
();
}
}
}
}
}
}
tests/Doctrine/Tests/ORM/Functional/AllTests.php
View file @
f1885cb7
...
@@ -26,6 +26,7 @@ class AllTests
...
@@ -26,6 +26,7 @@ class AllTests
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\DetachedEntityTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\DetachedEntityTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\QueryCacheTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\QueryCacheTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\QueryTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\QueryTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest'
);
return
$suite
;
return
$suite
;
}
}
...
...
tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php
→
tests/Doctrine/Tests/ORM/Functional/OneToOne
Bidirectional
AssociationTest.php
View file @
f1885cb7
...
@@ -8,35 +8,34 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
...
@@ -8,35 +8,34 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
require_once
__DIR__
.
'/../../TestInit.php'
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
/**
* Tests association mapping for ECommerceCustomer and ECommerceCart.
* Tests a bidirectional one-to-one association mapping (without inheritance).
* The latter is the owning side of the relation.
*/
*/
class
OneToOneAssociationTest
extends
\Doctrine\Tests\OrmFunctionalTestCase
class
OneToOne
Bidirectional
AssociationTest
extends
\Doctrine\Tests\OrmFunctionalTestCase
{
{
p
ublic
$customer
;
p
rivate
$customer
;
p
ublic
$cart
;
p
rivate
$cart
;
protected
function
setUp
()
protected
function
setUp
()
{
{
$this
->
useModelSet
(
'ecommerce'
);
$this
->
useModelSet
(
'ecommerce'
);
parent
::
setUp
();
parent
::
setUp
();
$this
->
customer
=
new
ECommerceCustomer
();
$this
->
customer
=
new
ECommerceCustomer
();
$this
->
customer
->
name
=
'John Doe'
;
$this
->
customer
->
setName
(
'John Doe'
)
;
$this
->
cart
=
new
ECommerceCart
();
$this
->
cart
=
new
ECommerceCart
();
$this
->
cart
->
payment
=
'Credit card'
;
$this
->
cart
->
setPayment
(
'Credit card'
)
;
}
}
public
function
testSavesAOneToOneAssociationWithCascadeSaveSet
()
{
public
function
testSavesAOneToOneAssociationWithCascadeSaveSet
()
{
$this
->
customer
->
setCart
(
$this
->
cart
);
$this
->
customer
->
setCart
(
$this
->
cart
);
$this
->
_em
->
save
(
$this
->
customer
);
$this
->
_em
->
save
(
$this
->
customer
);
$this
->
assertCartForeignKeyIs
(
$this
->
customer
->
id
);
$this
->
assertCartForeignKeyIs
(
$this
->
customer
->
getId
()
);
}
}
public
function
testDoesNotSaveAnInverseSideSet
()
{
public
function
testDoesNotSaveAnInverseSideSet
()
{
$this
->
customer
->
cart
=
$this
->
cart
;
$this
->
customer
->
brokenSetCart
(
$this
->
cart
)
;
$this
->
_em
->
save
(
$this
->
customer
);
$this
->
_em
->
save
(
$this
->
customer
);
$this
->
assertCartForeignKeyIs
(
null
);
$this
->
assertCartForeignKeyIs
(
null
);
}
}
...
@@ -51,20 +50,34 @@ class OneToOneAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
...
@@ -51,20 +50,34 @@ class OneToOneAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this
->
assertCartForeignKeyIs
(
null
);
$this
->
assertCartForeignKeyIs
(
null
);
}
}
public
function
test
LoadsAnAssociation
()
public
function
test
EagerLoad
()
{
{
$conn
=
$this
->
_em
->
getConnection
();
$customer
=
new
ECommerceCustomer
;
$conn
->
execute
(
'INSERT INTO ecommerce_customers (name) VALUES ("Giorgio")'
);
$customer
->
setName
(
'Giorgio'
);
$customerId
=
$conn
->
lastInsertId
();
$cart
=
new
ECommerceCart
;
$conn
->
execute
(
"INSERT INTO ecommerce_carts (customer_id, payment) VALUES ('
$customerId
', 'paypal')"
);
$cart
->
setPayment
(
'paypal'
);
$customer
->
setCart
(
$cart
);
$customer
=
$this
->
_em
->
find
(
"Doctrine\Tests\Models\ECommerce\ECommerceCustomer"
,
$customerId
);
$this
->
_em
->
save
(
$customer
);
$this
->
assertEquals
(
'paypal'
,
$customer
->
cart
->
payment
);
$this
->
_em
->
flush
();
$this
->
_em
->
clear
();
$query
=
$this
->
_em
->
createQuery
(
'select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca'
);
$result
=
$query
->
getResultList
();
$customer
=
$result
[
0
];
$this
->
assertTrue
(
$customer
->
getCart
()
instanceof
ECommerceCart
);
$this
->
assertEquals
(
'paypal'
,
$customer
->
getCart
()
->
getPayment
());
}
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
public
function
assertCartForeignKeyIs
(
$value
)
{
public
function
assertCartForeignKeyIs
(
$value
)
{
$foreignKey
=
$this
->
_em
->
getConnection
()
->
execute
(
'SELECT customer_id FROM ecommerce_carts WHERE id=?'
,
array
(
$this
->
cart
->
id
))
->
fetchColumn
();
$foreignKey
=
$this
->
_em
->
getConnection
()
->
execute
(
'SELECT customer_id FROM ecommerce_carts WHERE id=?'
,
array
(
$this
->
cart
->
getId
()
))
->
fetchColumn
();
$this
->
assertEquals
(
$value
,
$foreignKey
);
$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