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
554adc32
Commit
554adc32
authored
Feb 17, 2009
by
jwage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] Testing Collection base class and removing some code
parent
15beb5e4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
131 additions
and
36 deletions
+131
-36
Collection.php
lib/Doctrine/Common/Collections/Collection.php
+13
-30
CollectionTest.php
tests/Doctrine/Tests/Common/Collections/CollectionTest.php
+118
-6
No files found.
lib/Doctrine/Common/Collections/Collection.php
View file @
554adc32
...
...
@@ -31,7 +31,7 @@ use \ArrayIterator;
* A Collection is a thin wrapper around a php array. Think of it as an OO version
* of a plain array.
*
* @author
robo
* @author
Roman S. Borschel
* @since 2.0
*/
class
Collection
implements
Countable
,
IteratorAggregate
,
ArrayAccess
...
...
@@ -45,8 +45,9 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
protected
$_elements
=
array
();
/**
* Constructor accepts an array of $elements
*
* @param
<type>
$elements
* @param
array
$elements
*/
public
function
__construct
(
array
$elements
=
array
())
{
...
...
@@ -210,23 +211,13 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* @param Closure $p The predicate.
* @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
*/
public
function
exists
(
Closure
$p
)
{
public
function
exists
(
Closure
$p
)
{
foreach
(
$this
->
_elements
as
$key
=>
$element
)
if
(
$p
(
$key
,
$element
))
return
true
;
return
false
;
}
/**
* TODO
*
* @param unknown_type $otherColl
* @todo Impl
*/
public
function
containsAll
(
$otherColl
)
{
throw
new
DoctrineException
(
"Not yet implemented."
);
}
/**
* Searches for a given element and, if found, returns the corresponding key/index
* of that element. The comparison of two elements is strict, that means not
...
...
@@ -313,16 +304,6 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
return
true
;
}
/**
* Adds all entities of the other collection to this collection.
*
* @param unknown_type $otherCollection
* @todo Impl
*/
public
function
addAll
(
$otherCollection
)
{
}
/**
* Checks whether the collection is empty.
* Note: This is preferrable over count() == 0.
...
...
@@ -332,7 +313,7 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
public
function
isEmpty
()
{
// Note: Little "trick". Empty arrays evaluate to FALSE. No need to count().
return
!
(
bool
)
$this
->
_elements
;
return
!
(
bool
)
$this
->
_elements
;
}
/**
...
...
@@ -375,10 +356,13 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
* @param Closure $p The predicate.
* @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
*/
public
function
for
a
ll
(
Closure
$p
)
public
function
for
A
ll
(
Closure
$p
)
{
foreach
(
$this
->
_elements
as
$key
=>
$element
)
if
(
!
$p
(
$key
,
$element
))
return
false
;
foreach
(
$this
->
_elements
as
$key
=>
$element
)
{
if
(
!
$p
(
$key
,
$element
))
{
return
false
;
}
}
return
true
;
}
...
...
@@ -420,4 +404,3 @@ class Collection implements Countable, IteratorAggregate, ArrayAccess
$this
->
_elements
=
array
();
}
}
\ No newline at end of file
tests/Doctrine/Tests/Common/Collections/CollectionTest.php
View file @
554adc32
...
...
@@ -12,14 +12,17 @@ require_once __DIR__ . '/../../TestInit.php';
* @author robo
* @since 2.0
*/
class
CollectionTest
extends
\Doctrine\Tests\DoctrineTestCase
{
class
CollectionTest
extends
\Doctrine\Tests\DoctrineTestCase
{
private
$_coll
;
protected
function
setUp
()
{
protected
function
setUp
()
{
$this
->
_coll
=
new
\Doctrine\Common\Collections\Collection
;
}
public
function
testExists
()
{
public
function
testExists
()
{
$this
->
_coll
->
add
(
"one"
);
$this
->
_coll
->
add
(
"two"
);
$exists
=
$this
->
_coll
->
exists
(
function
(
$key
,
$element
)
{
return
$element
==
"one"
;
});
...
...
@@ -28,19 +31,128 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase {
$this
->
assertFalse
(
$exists
);
}
public
function
testMap
()
{
public
function
testMap
()
{
$this
->
_coll
->
add
(
1
);
$this
->
_coll
->
add
(
2
);
$res
=
$this
->
_coll
->
map
(
function
(
$e
)
{
return
$e
*
2
;
});
$this
->
assertEquals
(
array
(
2
,
4
),
$res
->
unwrap
());
}
public
function
testFilter
()
{
public
function
testFilter
()
{
$this
->
_coll
->
add
(
1
);
$this
->
_coll
->
add
(
"foo"
);
$this
->
_coll
->
add
(
3
);
$res
=
$this
->
_coll
->
filter
(
function
(
$e
)
{
return
is_numeric
(
$e
);
});
$this
->
assertEquals
(
array
(
0
=>
1
,
2
=>
3
),
$res
->
unwrap
());
}
}
public
function
testFirstAndLast
()
{
$this
->
_coll
->
add
(
'one'
);
$this
->
_coll
->
add
(
'two'
);
$this
->
assertEquals
(
$this
->
_coll
->
first
(),
'one'
);
$this
->
assertEquals
(
$this
->
_coll
->
last
(),
'two'
);
}
public
function
testArrayAccess
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
assertEquals
(
$this
->
_coll
[
0
],
'one'
);
$this
->
assertEquals
(
$this
->
_coll
[
1
],
'two'
);
unset
(
$this
->
_coll
[
0
]);
$this
->
assertEquals
(
$this
->
_coll
->
count
(),
1
);
}
public
function
testContainsKey
()
{
$this
->
_coll
[
5
]
=
'five'
;
$this
->
assertEquals
(
$this
->
_coll
->
containsKey
(
5
),
true
);
}
public
function
testContains
()
{
$this
->
_coll
[
0
]
=
'test'
;
$this
->
assertEquals
(
$this
->
_coll
->
contains
(
'test'
),
true
);
}
public
function
testSearch
()
{
$this
->
_coll
[
0
]
=
'test'
;
$this
->
assertEquals
(
$this
->
_coll
->
search
(
'test'
),
0
);
}
public
function
testGet
()
{
$this
->
_coll
[
0
]
=
'test'
;
$this
->
assertEquals
(
$this
->
_coll
->
get
(
0
),
'test'
);
}
public
function
testGetKeys
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
assertEquals
(
$this
->
_coll
->
getKeys
(),
array
(
0
,
1
));
}
public
function
testGetElements
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
assertEquals
(
$this
->
_coll
->
getElements
(),
array
(
'one'
,
'two'
));
}
public
function
testCount
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
assertEquals
(
$this
->
_coll
->
count
(),
2
);
$this
->
assertEquals
(
count
(
$this
->
_coll
),
2
);
}
public
function
testForAll
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
assertEquals
(
$this
->
_coll
->
forAll
(
function
(
$key
,
$element
)
{
return
is_string
(
$element
);
}),
true
);
$this
->
assertEquals
(
$this
->
_coll
->
forAll
(
function
(
$key
,
$element
)
{
return
is_array
(
$element
);
}),
false
);
}
public
function
testPartition
()
{
$this
->
_coll
[]
=
true
;
$this
->
_coll
[]
=
false
;
$partition
=
$this
->
_coll
->
partition
(
function
(
$key
,
$element
)
{
return
$element
==
true
;
});
$this
->
assertEquals
(
$partition
[
0
][
0
],
true
);
$this
->
assertEquals
(
$partition
[
1
][
0
],
false
);
}
public
function
testClear
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
_coll
->
clear
();
$this
->
assertEquals
(
$this
->
_coll
->
isEmpty
(),
true
);
}
public
function
testRemove
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
_coll
->
remove
(
0
);
$this
->
assertEquals
(
$this
->
_coll
->
contains
(
'one'
),
false
);
}
public
function
testRemoveElement
()
{
$this
->
_coll
[]
=
'one'
;
$this
->
_coll
[]
=
'two'
;
$this
->
_coll
->
removeElement
(
'two'
);
$this
->
assertEquals
(
$this
->
_coll
->
contains
(
'two'
),
false
);
}
}
\ No newline at end of file
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