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
1b9b6c68
Commit
1b9b6c68
authored
Jul 02, 2009
by
piccoloprincipe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] added many-to-many uni and bi directional tests (affects #2276)
parent
2c508102
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
366 additions
and
6 deletions
+366
-6
ECommerceCart.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php
+4
-0
ECommerceCategory.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceCategory.php
+72
-0
ECommerceProduct.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php
+30
-5
AbstractManyToManyAssociationTestCase.php
.../ORM/Functional/AbstractManyToManyAssociationTestCase.php
+50
-0
AllTests.php
tests/Doctrine/Tests/ORM/Functional/AllTests.php
+2
-0
ManyToManyBidirectionalAssociationTest.php
...ORM/Functional/ManyToManyBidirectionalAssociationTest.php
+111
-0
ManyToManyUnidirectionalAssociationTest.php
...RM/Functional/ManyToManyUnidirectionalAssociationTest.php
+94
-0
OrmFunctionalTestCase.php
tests/Doctrine/Tests/OrmFunctionalTestCase.php
+3
-1
No files found.
tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php
View file @
1b9b6c68
...
@@ -82,4 +82,8 @@ class ECommerceCart
...
@@ -82,4 +82,8 @@ class ECommerceCart
public
function
addProduct
(
ECommerceProduct
$product
)
{
public
function
addProduct
(
ECommerceProduct
$product
)
{
$this
->
products
[]
=
$product
;
$this
->
products
[]
=
$product
;
}
}
public
function
removeProduct
(
ECommerceProduct
$product
)
{
return
$this
->
products
->
removeElement
(
$product
);
}
}
}
tests/Doctrine/Tests/Models/ECommerce/ECommerceCategory.php
0 → 100644
View file @
1b9b6c68
<?php
namespace
Doctrine\Tests\Models\ECommerce
;
/**
* ECommerceCategory
* Represents a tag applied on particular products.
*
* @author Giorgio Sironi
* @Entity
* @Table(name="ecommerce_categories")
*/
class
ECommerceCategory
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
public
$id
;
/**
* @Column(type="string", length=50)
*/
public
$name
;
/**
* @ManyToMany(targetEntity="ECommerceProduct", mappedBy="categories")
*/
public
$products
;
public
function
__construct
()
{
$this
->
products
=
new
\Doctrine\Common\Collections\Collection
();
}
public
function
getId
()
{
return
$this
->
id
;
}
public
function
getName
()
{
return
$this
->
name
;
}
public
function
setName
(
$name
)
{
$this
->
name
=
$name
;
}
public
function
addProduct
(
ECommerceProduct
$product
)
{
if
(
!
$this
->
products
->
contains
(
$product
))
{
$this
->
products
[]
=
$product
;
$product
->
addCategory
(
$this
);
}
}
public
function
removeProduct
(
ECommerceProduct
$product
)
{
if
(
$this
->
products
->
contains
(
$product
))
{
$this
->
products
->
removeElement
(
$product
);
$product
->
removeCategory
(
$this
);
}
}
public
function
getProducts
()
{
return
$this
->
products
;
}
}
tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php
View file @
1b9b6c68
...
@@ -20,7 +20,7 @@ class ECommerceProduct
...
@@ -20,7 +20,7 @@ class ECommerceProduct
private
$id
;
private
$id
;
/**
/**
* @Column(type="string", length=50)
* @Column(type="string", length=50
, nullable="true"
)
*/
*/
private
$name
;
private
$name
;
...
@@ -40,12 +40,13 @@ class ECommerceProduct
...
@@ -40,12 +40,13 @@ class ECommerceProduct
* @JoinTable(name="ecommerce_products_categories",
* @JoinTable(name="ecommerce_products_categories",
joinColumns={{"name"="product_id", "referencedColumnName"="id"}},
joinColumns={{"name"="product_id", "referencedColumnName"="id"}},
inverseJoinColumns={{"name"="category_id", "referencedColumnName"="id"}})
inverseJoinColumns={{"name"="category_id", "referencedColumnName"="id"}})
private $categories;
*/
*/
private
$categories
;
public
function
__construct
()
public
function
__construct
()
{
{
$this
->
features
=
new
\Doctrine\Common\Collections\Collection
;
$this
->
features
=
new
\Doctrine\Common\Collections\Collection
;
$this
->
categories
=
new
\Doctrine\Common\Collections\Collection
;
}
}
public
function
getId
()
public
function
getId
()
...
@@ -83,17 +84,20 @@ class ECommerceProduct
...
@@ -83,17 +84,20 @@ class ECommerceProduct
return
$this
->
features
;
return
$this
->
features
;
}
}
public
function
addFeature
(
ECommerceFeature
$feature
)
{
public
function
addFeature
(
ECommerceFeature
$feature
)
{
$this
->
features
[]
=
$feature
;
$this
->
features
[]
=
$feature
;
$feature
->
setProduct
(
$this
);
$feature
->
setProduct
(
$this
);
}
}
/** does not set the owning side */
/** does not set the owning side */
public
function
brokenAddFeature
(
ECommerceFeature
$feature
)
{
public
function
brokenAddFeature
(
ECommerceFeature
$feature
)
{
$this
->
features
[]
=
$feature
;
$this
->
features
[]
=
$feature
;
}
}
public
function
removeFeature
(
ECommerceFeature
$feature
)
{
public
function
removeFeature
(
ECommerceFeature
$feature
)
{
if
(
$this
->
features
->
contains
(
$feature
))
{
if
(
$this
->
features
->
contains
(
$feature
))
{
$removed
=
$this
->
features
->
removeElement
(
$feature
);
$removed
=
$this
->
features
->
removeElement
(
$feature
);
if
(
$removed
)
{
if
(
$removed
)
{
...
@@ -103,4 +107,25 @@ class ECommerceProduct
...
@@ -103,4 +107,25 @@ class ECommerceProduct
}
}
return
false
;
return
false
;
}
}
public
function
addCategory
(
ECommerceCategory
$category
)
{
if
(
!
$this
->
categories
->
contains
(
$category
))
{
$this
->
categories
[]
=
$category
;
$category
->
addProduct
(
$this
);
}
}
public
function
removeCategory
(
ECommerceCategory
$category
)
{
if
(
$this
->
categories
->
contains
(
$category
))
{
$this
->
categories
->
removeElement
(
$category
);
$category
->
removeProduct
(
$this
);
}
}
public
function
getCategories
()
{
return
$this
->
categories
;
}
}
}
tests/Doctrine/Tests/ORM/Functional/AbstractManyToManyAssociationTestCase.php
0 → 100644
View file @
1b9b6c68
<?php
namespace
Doctrine\Tests\ORM\Functional
;
use
Doctrine\Common\Collections\Collection
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
* Base class for testing a many-to-many association mapping (without inheritance).
*/
class
AbstractManyToManyAssociationTestCase
extends
\Doctrine\Tests\OrmFunctionalTestCase
{
protected
$_firstField
;
protected
$_secondField
;
protected
$_table
;
public
function
assertForeignKeysContain
(
$firstId
,
$secondId
)
{
$this
->
assertEquals
(
1
,
$this
->
_countForeignKeys
(
$firstId
,
$secondId
));
}
public
function
assertForeignKeysNotContain
(
$firstId
,
$secondId
)
{
$this
->
assertEquals
(
0
,
$this
->
_countForeignKeys
(
$firstId
,
$secondId
));
}
protected
function
_countForeignKeys
(
$firstId
,
$secondId
)
{
return
count
(
$this
->
_em
->
getConnection
()
->
execute
(
"SELECT
{
$this
->
_firstField
}
FROM
{
$this
->
_table
}
WHERE
{
$this
->
_firstField
}
=?
AND
{
$this
->
_secondField
}
=?"
,
array
(
$firstId
,
$secondId
)));
}
public
function
assertCollectionEquals
(
Collection
$first
,
Collection
$second
)
{
if
(
count
(
$first
)
!=
count
(
$second
))
{
return
false
;
}
foreach
(
$first
as
$element
)
{
if
(
!
$second
->
contains
(
$element
))
{
return
false
;
}
}
return
true
;
}
}
tests/Doctrine/Tests/ORM/Functional/AllTests.php
View file @
1b9b6c68
...
@@ -29,6 +29,8 @@ class AllTests
...
@@ -29,6 +29,8 @@ class AllTests
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToOneUnidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToOneUnidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToManyBidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\OneToManyBidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\ManyToManyUnidirectionalAssociationTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Functional\ManyToManyBidirectionalAssociationTest'
);
return
$suite
;
return
$suite
;
}
}
...
...
tests/Doctrine/Tests/ORM/Functional/ManyToManyBidirectionalAssociationTest.php
0 → 100644
View file @
1b9b6c68
<?php
namespace
Doctrine\Tests\ORM\Functional
;
use
Doctrine\Common\Collections\Collection
;
use
Doctrine\Tests\Models\ECommerce\ECommerceProduct
;
use
Doctrine\Tests\Models\ECommerce\ECommerceCategory
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
* Tests a bidirectional many-to-many association mapping (without inheritance).
* Inverse side is not present.
*/
class
ManyToManyBidirectionalAssociationTest
extends
AbstractManyToManyAssociationTestCase
{
protected
$_firstField
=
'cart_id'
;
protected
$_secondField
=
'product_id'
;
protected
$_table
=
'ecommerce_carts_products'
;
private
$firstProduct
;
private
$secondProduct
;
private
$firstCategory
;
private
$secondCategory
;
protected
function
setUp
()
{
$this
->
useModelSet
(
'ecommerce'
);
parent
::
setUp
();
$this
->
firstProduct
=
new
ECommerceProduct
();
$this
->
secondProduct
=
new
ECommerceProduct
();
$this
->
firstCategory
=
new
ECommerceCategory
();
$this
->
firstCategory
->
setName
(
"Business"
);
$this
->
secondCategory
=
new
ECommerceCategory
();
$this
->
secondCategory
->
setName
(
"Home"
);
}
public
function
testSavesAManyToManyAssociationWithCascadeSaveSet
()
{
$this
->
firstProduct
->
addCategory
(
$this
->
firstCategory
);
$this
->
firstProduct
->
addCategory
(
$this
->
secondCategory
);
$this
->
_em
->
save
(
$this
->
firstProduct
);
$this
->
_em
->
flush
();
$this
->
assertForeignKeysContain
(
$this
->
firstProduct
->
getId
(),
$this
->
firstCategory
->
getId
());
$this
->
assertForeignKeysContain
(
$this
->
firstProduct
->
getId
(),
$this
->
secondCategory
->
getId
());
}
public
function
testRemovesAManyToManyAssociation
()
{
$this
->
firstProduct
->
addCategory
(
$this
->
firstCategory
);
$this
->
firstProduct
->
addCategory
(
$this
->
secondCategory
);
$this
->
_em
->
save
(
$this
->
firstProduct
);
$this
->
firstProduct
->
removeCategory
(
$this
->
firstCategory
);
$this
->
_em
->
flush
();
$this
->
assertForeignKeysNotContain
(
$this
->
firstProduct
->
getId
(),
$this
->
firstCategory
->
getId
());
$this
->
assertForeignKeysContain
(
$this
->
firstProduct
->
getId
(),
$this
->
secondCategory
->
getId
());
}
public
function
testEagerLoadsInverseSide
()
{
$this
->
_createLoadingFixture
();
list
(
$firstProduct
,
$secondProduct
)
=
$this
->
_findProducts
();
$categories
=
$firstProduct
->
getCategories
();
$this
->
assertTrue
(
$categories
[
0
]
instanceof
ECommerceCategory
);
$this
->
assertTrue
(
$categories
[
1
]
instanceof
ECommerceCategory
);
$this
->
assertCollectionEquals
(
$categories
,
$secondProduct
->
getCategories
());
}
public
function
testEagerLoadsOwningSide
()
{
$this
->
_createLoadingFixture
();
list
(
$firstProduct
,
$secondProduct
)
=
$this
->
_findProducts
();
$categories
=
$firstProduct
->
getCategories
();
$products
=
$categories
[
0
]
->
getProducts
();
$this
->
assertTrue
(
$products
[
0
]
instanceof
ECommerceProduct
);
$this
->
assertTrue
(
$products
[
1
]
instanceof
ECommerceProduct
);
$this
->
assertCollectionEquals
(
$products
,
$categories
[
1
]
->
getProducts
());
}
protected
function
_createLoadingFixture
()
{
$this
->
firstProduct
->
addCategory
(
$this
->
firstCategory
);
$this
->
firstProduct
->
addCategory
(
$this
->
secondCategory
);
$this
->
secondProduct
->
addCategory
(
$this
->
firstCategory
);
$this
->
secondProduct
->
addCategory
(
$this
->
secondCategory
);
$this
->
_em
->
save
(
$this
->
firstProduct
);
$this
->
_em
->
save
(
$this
->
secondProduct
);
$this
->
_em
->
flush
();
$this
->
_em
->
clear
();
}
protected
function
_findProducts
()
{
$query
=
$this
->
_em
->
createQuery
(
'SELECT p, c FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p LEFT JOIN p.categories c ORDER BY p.id, c.id'
);
return
$query
->
getResultList
();
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
}
tests/Doctrine/Tests/ORM/Functional/ManyToManyUnidirectionalAssociationTest.php
0 → 100644
View file @
1b9b6c68
<?php
namespace
Doctrine\Tests\ORM\Functional
;
use
Doctrine\Common\Collections\Collection
;
use
Doctrine\Tests\Models\ECommerce\ECommerceCart
;
use
Doctrine\Tests\Models\ECommerce\ECommerceProduct
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
* Tests a unidirectional many-to-many association mapping (without inheritance).
* Inverse side is not present.
*/
class
ManyToManyUnidirectionalAssociationTest
extends
AbstractManyToManyAssociationTestCase
{
protected
$_firstField
=
'cart_id'
;
protected
$_secondField
=
'product_id'
;
protected
$_table
=
'ecommerce_carts_products'
;
private
$firstProduct
;
private
$secondProduct
;
private
$firstCart
;
private
$secondCart
;
protected
function
setUp
()
{
$this
->
useModelSet
(
'ecommerce'
);
parent
::
setUp
();
$this
->
firstProduct
=
new
ECommerceProduct
();
$this
->
firstProduct
->
setName
(
'Doctrine 1.x Manual'
);
$this
->
secondProduct
=
new
ECommerceProduct
();
$this
->
secondProduct
->
setName
(
'Doctrine 2.x Manual'
);
$this
->
firstCart
=
new
ECommerceCart
();
$this
->
secondCart
=
new
ECommerceCart
();
}
public
function
testSavesAManyToManyAssociationWithCascadeSaveSet
()
{
$this
->
firstCart
->
addProduct
(
$this
->
firstProduct
);
$this
->
firstCart
->
addProduct
(
$this
->
secondProduct
);
$this
->
_em
->
save
(
$this
->
firstCart
);
$this
->
_em
->
flush
();
$this
->
assertForeignKeysContain
(
$this
->
firstCart
->
getId
(),
$this
->
firstProduct
->
getId
());
$this
->
assertForeignKeysContain
(
$this
->
firstCart
->
getId
(),
$this
->
secondProduct
->
getId
());
}
public
function
testRemovesAManyToManyAssociation
()
{
$this
->
firstCart
->
addProduct
(
$this
->
firstProduct
);
$this
->
firstCart
->
addProduct
(
$this
->
secondProduct
);
$this
->
_em
->
save
(
$this
->
firstCart
);
$this
->
firstCart
->
removeProduct
(
$this
->
firstProduct
);
$this
->
_em
->
flush
();
$this
->
assertForeignKeysNotContain
(
$this
->
firstCart
->
getId
(),
$this
->
firstProduct
->
getId
());
$this
->
assertForeignKeysContain
(
$this
->
firstCart
->
getId
(),
$this
->
secondProduct
->
getId
());
}
public
function
testEagerLoad
()
{
$this
->
firstCart
->
addProduct
(
$this
->
firstProduct
);
$this
->
firstCart
->
addProduct
(
$this
->
secondProduct
);
$this
->
secondCart
->
addProduct
(
$this
->
firstProduct
);
$this
->
secondCart
->
addProduct
(
$this
->
secondProduct
);
$this
->
_em
->
save
(
$this
->
firstCart
);
$this
->
_em
->
save
(
$this
->
secondCart
);
$this
->
_em
->
flush
();
$this
->
_em
->
clear
();
$query
=
$this
->
_em
->
createQuery
(
'SELECT c, p FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c LEFT JOIN c.products p ORDER BY c.id, p.id'
);
$result
=
$query
->
getResultList
();
$firstCart
=
$result
[
0
];
$products
=
$firstCart
->
getProducts
();
$secondCart
=
$result
[
1
];
$this
->
assertTrue
(
$products
[
0
]
instanceof
ECommerceProduct
);
$this
->
assertTrue
(
$products
[
1
]
instanceof
ECommerceProduct
);
$this
->
assertCollectionEquals
(
$products
,
$secondCart
->
getProducts
());
//$this->assertEquals("Doctrine 1.x Manual", $products[0]->getName());
//$this->assertEquals("Doctrine 2.x Manual", $products[1]->getName());
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
}
tests/Doctrine/Tests/OrmFunctionalTestCase.php
View file @
1b9b6c68
...
@@ -49,7 +49,8 @@ class OrmFunctionalTestCase extends OrmTestCase
...
@@ -49,7 +49,8 @@ class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\ECommerce\ECommerceCustomer'
,
'Doctrine\Tests\Models\ECommerce\ECommerceCustomer'
,
'Doctrine\Tests\Models\ECommerce\ECommerceProduct'
,
'Doctrine\Tests\Models\ECommerce\ECommerceProduct'
,
'Doctrine\Tests\Models\ECommerce\ECommerceShipping'
,
'Doctrine\Tests\Models\ECommerce\ECommerceShipping'
,
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
,
'Doctrine\Tests\Models\ECommerce\ECommerceCategory'
),
),
'generic'
=>
array
(
'generic'
=>
array
(
'Doctrine\Tests\Models\Generic\DateTimeModel'
'Doctrine\Tests\Models\Generic\DateTimeModel'
...
@@ -81,6 +82,7 @@ class OrmFunctionalTestCase extends OrmTestCase
...
@@ -81,6 +82,7 @@ class OrmFunctionalTestCase extends OrmTestCase
$conn
->
exec
(
'DELETE FROM ecommerce_products'
);
$conn
->
exec
(
'DELETE FROM ecommerce_products'
);
$conn
->
exec
(
'DELETE FROM ecommerce_shippings'
);
$conn
->
exec
(
'DELETE FROM ecommerce_shippings'
);
$conn
->
exec
(
'DELETE FROM ecommerce_features'
);
$conn
->
exec
(
'DELETE FROM ecommerce_features'
);
$conn
->
exec
(
'DELETE FROM ecommerce_categories'
);
}
}
if
(
isset
(
$this
->
_usedModelSets
[
'company'
]))
{
if
(
isset
(
$this
->
_usedModelSets
[
'company'
]))
{
$conn
->
exec
(
'DELETE FROM company_persons_friends'
);
$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