Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
test_helpers-3398264
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
test_helpers-3398264
Commits
24947d02
Commit
24947d02
authored
2 years ago
by
Alexey Korepov
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3326541
by Murz: Add function to simplify mocking Iterator for mocked objects
parent
3609b90e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/UnitTestHelpers.php
+54
-2
54 additions, 2 deletions
src/UnitTestHelpers.php
with
54 additions
and
2 deletions
src/UnitTestHelpers.php
+
54
−
2
View file @
24947d02
...
...
@@ -10,7 +10,6 @@ use Drupal\Core\Cache\MemoryBackendFactory;
use
Drupal\Core\Database\Query\ConditionInterface
as
DatabaseQueryConditionInterface
;
use
Drupal\Core\Database\Query\SelectInterface
as
DatabaseSelectInterface
;
use
Drupal\Core\DependencyInjection\ContainerBuilder
;
use
Drupal\Core\Entity\EntityTypeBundleInfo
;
use
Drupal\Core\Entity\Query\ConditionInterface
as
EntityQueryConditionInterface
;
use
Drupal\Core\Entity\Query\QueryInterface
as
EntityQueryInterface
;
use
Drupal\test_helpers
\Stub\ConfigFactoryStub
;
...
...
@@ -469,6 +468,8 @@ class UnitTestHelpers {
*
* @param string $entityTypeClassName
* The entity class.
* @param string $annotation
* The annotation class.
*
* @return \Drupal\test_helpers\Stub\EntityStorageStub
* The initialized stub of Entity Storage.
...
...
@@ -711,6 +712,58 @@ class UnitTestHelpers {
return
UnitTestCaseWrapper
::
getInstance
()
->
createPartialMockWithCustomMethods
(
$originalClassName
,
$methods
,
$addMethods
);
}
/**
* Sets an array as the iterator on a mocked object.
* @param array $array
* The array with data.
* @param \PHPUnit\Framework\MockObject\MockObject $mock
* The mocked object.
*
* @return \PHPUnit\Framework\MockObject\MockObject
* The mocked object.
*/
public
static
function
addIteratorToMock
(
array
$array
,
MockObject
$mock
):
MockObject
{
$iterator
=
new
\ArrayIterator
(
$array
);
$mock
->
method
(
'rewind'
)
->
willReturnCallback
(
function
()
use
(
$iterator
):
void
{
$iterator
->
rewind
();
});
$mock
->
method
(
'current'
)
->
willReturnCallback
(
function
()
use
(
$iterator
)
{
return
$iterator
->
current
();
});
$mock
->
method
(
'key'
)
->
willReturnCallback
(
function
()
use
(
$iterator
)
{
return
$iterator
->
key
();
});
$mock
->
method
(
'next'
)
->
willReturnCallback
(
function
()
use
(
$iterator
):
void
{
$iterator
->
next
();
});
$mock
->
method
(
'valid'
)
->
willReturnCallback
(
function
()
use
(
$iterator
):
bool
{
return
$iterator
->
valid
();
});
$mock
->
method
(
'offsetGet'
)
->
willReturnCallback
(
function
(
$key
)
use
(
$iterator
)
{
return
$iterator
[
$key
];
});
$mock
->
method
(
'offsetSet'
)
->
willReturnCallback
(
function
(
$key
,
$value
)
use
(
$iterator
)
{
return
$iterator
[
$key
]
=
$value
;
});
// @todo Check if the method getIterator is defined and mock it too.
return
$mock
;
}
/* ************************************************************************ *
* Internal functions.
* ************************************************************************ */
...
...
@@ -757,7 +810,6 @@ class UnitTestHelpers {
return
$services
[
$serviceName
];
}
/**
* Converts a condition in Search API format to the associative array.
*/
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment