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
1e058992
Commit
1e058992
authored
Jul 01, 2009
by
piccoloprincipe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] added new bidirectional one-many association test (affects #2276)
parent
01147039
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
192 additions
and
17 deletions
+192
-17
ECommerceFeature.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceFeature.php
+62
-0
ECommerceProduct.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php
+37
-16
OneToManyBidirectionalAssociationTest.php
.../ORM/Functional/OneToManyBidirectionalAssociationTest.php
+90
-0
OrmFunctionalTestCase.php
tests/Doctrine/Tests/OrmFunctionalTestCase.php
+3
-1
No files found.
tests/Doctrine/Tests/Models/ECommerce/ECommerceFeature.php
0 → 100644
View file @
1e058992
<?php
namespace
Doctrine\Tests\Models\ECommerce
;
/**
* Describes a product feature.
*
* @author Giorgio Sironi
* @Entity
* @Table(name="ecommerce_features")
*/
class
ECommerceFeature
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private
$id
;
/**
* @Column(type="string", length=50)
*/
private
$description
;
/**
* @ManyToOne(targetEntity="ECommerceProduct")
* @JoinColumn(name="product_id", referencedColumnName="id")
*/
private
$product
;
public
function
getId
()
{
return
$this
->
id
;
}
public
function
getDescription
()
{
return
$this
->
description
;
}
public
function
setDescription
(
$description
)
{
$this
->
description
=
$description
;
}
public
function
setProduct
(
ECommerceProduct
$product
)
{
if
(
$this
->
product
!==
$product
)
{
$this
->
product
=
$product
;
$product
->
addFeature
(
$this
);
}
}
public
function
removeProduct
()
{
if
(
$this
->
product
!==
null
)
{
$product
=
$this
->
product
;
$this
->
product
=
null
;
$product
->
removeFeature
(
$this
);
}
}
public
function
getProduct
()
{
return
$this
->
product
;
}
}
tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php
View file @
1e058992
...
...
@@ -24,6 +24,17 @@ class ECommerceProduct
*/
private
$name
;
/**
* @OneToOne(targetEntity="ECommerceShipping", cascade={"save"})
* @JoinColumn(name="shipping_id", referencedColumnName="id")
*/
private
$shipping
;
/**
* @OneToMany(targetEntity="ECommerceFeature", mappedBy="product", cascade={"save"})
*/
private
$features
;
/**
* @ManyToMany(targetEntity="ECommerceCategory", cascade={"save"})
* @JoinTable(name="ecommerce_products_categories",
...
...
@@ -32,12 +43,6 @@ class ECommerceProduct
private $categories;
*/
/**
* @OneToOne(targetEntity="ECommerceShipping", cascade={"save"})
* @JoinColumn(name="shipping_id", referencedColumnName="id")
*/
private
$shipping
;
public
function
getId
()
{
return
$this
->
id
;
...
...
@@ -53,16 +58,6 @@ class ECommerceProduct
$this
->
name
=
$name
;
}
public
function
getPrice
()
{
return
$this
->
price
;
}
public
function
setPrice
(
$price
)
{
$this
->
price
=
$price
;
}
public
function
getShipping
()
{
return
$this
->
shipping
;
...
...
@@ -77,4 +72,30 @@ class ECommerceProduct
{
$this
->
shipping
=
null
;
}
public
function
getFeatures
()
{
return
$this
->
features
;
}
public
function
addFeature
(
ECommerceFeature
$feature
)
{
$this
->
features
[]
=
$feature
;
$feature
->
setProduct
(
$this
);
}
/** does not set the owning side */
public
function
brokenAddFeature
(
ECommerceFeature
$feature
)
{
$this
->
features
[]
=
$feature
;
}
public
function
removeFeature
(
ECommerceFeature
$feature
)
{
foreach
(
$this
->
features
as
$index
=>
$current
)
{
if
(
$current
===
$feature
)
{
unset
(
$this
->
features
[
$index
]);
$current
->
removeProduct
();
return
true
;
}
}
return
false
;
}
}
tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php
0 → 100644
View file @
1e058992
<?php
namespace
Doctrine\Tests\ORM\Functional
;
use
Doctrine\Tests\Models\ECommerce\ECommerceProduct
;
use
Doctrine\Tests\Models\ECommerce\ECommerceFeature
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
* Tests a bidirectional one-to-one association mapping (without inheritance).
*/
class
OneToManyBidirectionalAssociationTest
extends
\Doctrine\Tests\OrmFunctionalTestCase
{
private
$product
;
private
$firstFeature
;
protected
function
setUp
()
{
$this
->
useModelSet
(
'ecommerce'
);
parent
::
setUp
();
$this
->
product
=
new
ECommerceProduct
();
$this
->
product
->
setName
(
'Doctrine Cookbook'
);
$this
->
firstFeature
=
new
ECommerceFeature
();
$this
->
firstFeature
->
setDescription
(
'Model writing tutorial'
);
$this
->
secondFeature
=
new
ECommerceFeature
();
$this
->
secondFeature
->
setDescription
(
'Annotations examples'
);
}
public
function
testSavesAOneToManyAssociationWithCascadeSaveSet
()
{
$this
->
product
->
addFeature
(
$this
->
firstFeature
);
$this
->
product
->
addFeature
(
$this
->
secondFeature
);
$this
->
_em
->
save
(
$this
->
product
);
$this
->
assertFeatureForeignKeyIs
(
$this
->
product
->
getId
(),
$this
->
firstFeature
);
$this
->
assertFeatureForeignKeyIs
(
$this
->
product
->
getId
(),
$this
->
secondFeature
);
}
public
function
testDoesNotSaveAnInverseSideSet
()
{
$this
->
product
->
brokenAddFeature
(
$this
->
firstFeature
);
$this
->
_em
->
save
(
$this
->
product
);
$this
->
assertFeatureForeignKeyIs
(
null
,
$this
->
firstFeature
);
}
public
function
testRemovesOneToOneAssociation
()
{
$this
->
product
->
addFeature
(
$this
->
firstFeature
);
$this
->
product
->
addFeature
(
$this
->
secondFeature
);
$this
->
_em
->
save
(
$this
->
product
);
$this
->
product
->
removeFeature
(
$this
->
firstFeature
);
$this
->
_em
->
flush
();
$this
->
assertFeatureForeignKeyIs
(
null
,
$this
->
firstFeature
);
$this
->
assertFeatureForeignKeyIs
(
$this
->
product
->
getId
(),
$this
->
secondFeature
);
}
public
function
testEagerLoadsOneToManyAssociation
()
{
$this
->
product
->
addFeature
(
$this
->
firstFeature
);
$this
->
product
->
addFeature
(
$this
->
secondFeature
);
$this
->
_em
->
save
(
$this
->
product
);
$this
->
_em
->
flush
();
$this
->
_em
->
clear
();
$query
=
$this
->
_em
->
createQuery
(
'select p, f from Doctrine\Tests\Models\ECommerce\ECommerceProduct p join p.features f'
);
$result
=
$query
->
getResultList
();
$product
=
$result
[
0
];
$features
=
$product
->
getFeatures
();
$this
->
assertTrue
(
$features
[
0
]
instanceof
ECommerceFeature
);
$this
->
assertSame
(
$product
,
$features
[
0
]
->
getProduct
());
$this
->
assertEquals
(
'Model writing tutorial'
,
$features
[
0
]
->
getDescription
());
$this
->
assertTrue
(
$features
[
1
]
instanceof
ECommerceFeature
);
$this
->
assertSame
(
$product
,
$features
[
1
]
->
getProduct
());
$this
->
assertEquals
(
'Annotations examples'
,
$features
[
1
]
->
getDescription
());
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
public
function
assertFeatureForeignKeyIs
(
$value
,
ECommerceFeature
$feature
)
{
$foreignKey
=
$this
->
_em
->
getConnection
()
->
execute
(
'SELECT product_id FROM ecommerce_features WHERE id=?'
,
array
(
$feature
->
getId
()))
->
fetchColumn
();
$this
->
assertEquals
(
$value
,
$foreignKey
);
}
}
tests/Doctrine/Tests/OrmFunctionalTestCase.php
View file @
1e058992
...
...
@@ -48,7 +48,8 @@ class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\ECommerce\ECommerceCart'
,
'Doctrine\Tests\Models\ECommerce\ECommerceCustomer'
,
'Doctrine\Tests\Models\ECommerce\ECommerceProduct'
,
'Doctrine\Tests\Models\ECommerce\ECommerceShipping'
'Doctrine\Tests\Models\ECommerce\ECommerceShipping'
,
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
),
'generic'
=>
array
(
'Doctrine\Tests\Models\Generic\DateTimeModel'
...
...
@@ -79,6 +80,7 @@ class OrmFunctionalTestCase extends OrmTestCase
$conn
->
exec
(
'DELETE FROM ecommerce_customers'
);
$conn
->
exec
(
'DELETE FROM ecommerce_products'
);
$conn
->
exec
(
'DELETE FROM ecommerce_shippings'
);
$conn
->
exec
(
'DELETE FROM ecommerce_features'
);
}
if
(
isset
(
$this
->
_usedModelSets
[
'company'
]))
{
$conn
->
exec
(
'DELETE FROM company_persons_friends'
);
...
...
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