Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12741035
TraversableUtilitiesTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
TraversableUtilitiesTest.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
Keruald\OmniTools\Tests\Collections
;
use
Keruald\OmniTools\Collections\TraversableUtilities
;
use
PHPUnit\Framework\Attributes\DataProvider
;
use
PHPUnit\Framework\TestCase
;
use
Countable
;
use
InvalidArgumentException
;
use
IteratorAggregate
;
use
Traversable
;
class
TraversableUtilitiesTest
extends
TestCase
{
#[DataProvider('provideCountables')]
public
function
testCount
(
$expectedCount
,
$countable
)
{
$this
->
assertEquals
(
$expectedCount
,
TraversableUtilities
::
count
(
$countable
)
);
}
#[DataProvider('provideNotCountables')]
public
function
testCountWithNotCountables
(
$notCountable
)
{
$this
->
expectException
(
"TypeError"
);
TraversableUtilities
::
count
(
$notCountable
);
}
#[DataProvider('providePureCountables')]
public
function
testIsCountable
(
$countable
)
{
$this
->
assertTrue
(
TraversableUtilities
::
isCountable
(
$countable
));
}
#[DataProvider('provideIterableAndFirst')]
public
function
testIsFirst
(
$expected
,
$iterable
)
{
$this
->
assertEquals
(
$expected
,
TraversableUtilities
::
first
(
$iterable
));
}
public
function
testIsFirstWithEmptyCollection
()
{
$this
->
expectException
(
InvalidArgumentException
::
class
);
TraversableUtilities
::
first
([]);
}
#[DataProvider('provideIterableAndFirst')]
public
function
testIsFirstOr
(
$expected
,
$iterable
)
{
$actual
=
TraversableUtilities
::
firstOr
(
$iterable
,
666
);
$this
->
assertEquals
(
$expected
,
$actual
);
}
public
function
testIsFirstOrWithEmptyCollection
()
{
$actual
=
TraversableUtilities
::
firstOr
([],
666
);
$this
->
assertEquals
(
666
,
$actual
);
}
///
/// Data providers
///
public
static
function
provideCountables
()
:
iterable
{
yield
[
0
,
null
];
yield
[
0
,
false
];
yield
[
0
,
[]];
yield
[
3
,
[
"a"
,
"b"
,
"c"
]];
yield
[
42
,
new
class
implements
Countable
{
public
function
count
()
:
int
{
return
42
;
}
}
];
}
public
static
function
providePureCountables
()
:
iterable
{
yield
[[]];
yield
[[
"a"
,
"b"
,
"c"
]];
yield
[
new
class
implements
Countable
{
public
function
count
()
:
int
{
return
42
;
}
}
];
}
public
static
function
provideNotCountables
()
:
iterable
{
yield
[
true
];
yield
[
new
\stdClass
];
yield
[
0
];
yield
[
""
];
yield
[
"abc"
];
}
public
static
function
provideIterableAndFirst
()
:
iterable
{
yield
[
"a"
,
[
"a"
,
"b"
,
"c"
]];
yield
[
"apple"
,
[
"fruit"
=>
"apple"
,
"vegetable"
=>
"leeks"
]];
yield
[
42
,
new
class
implements
IteratorAggregate
{
public
function
getIterator
()
:
Traversable
{
yield
42
;
yield
100
;
}
}];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sun, Nov 16, 13:15 (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3089930
Default Alt Text
TraversableUtilitiesTest.php (2 KB)
Attached To
Mode
rKERUALD Keruald libraries development repository
Attached
Detach File
Event Timeline
Log In to Comment