Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
test_helpers-3380615
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-3380615
Commits
ed0b8011
Commit
ed0b8011
authored
1 year ago
by
Alexey Korepov
Browse files
Options
Downloads
Patches
Plain Diff
Add test for mocked entity fields
parent
9bab0f53
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/StubFactory/EntityStubFactory.php
+16
-9
16 additions, 9 deletions
src/StubFactory/EntityStubFactory.php
tests/src/Unit/TestHelpersApi/CreateEntityStubTest.php
+35
-1
35 additions, 1 deletion
tests/src/Unit/TestHelpersApi/CreateEntityStubTest.php
with
51 additions
and
10 deletions
src/StubFactory/EntityStubFactory.php
+
16
−
9
View file @
ed0b8011
...
...
@@ -219,27 +219,34 @@ class EntityStubFactory {
// Filling values to the entity array.
foreach
(
$values
as
$name
=>
$value
)
{
if
(
isset
(
$options
[
'definitions'
][
$name
]))
{
// Legacy start.
// @todo Deprecate this.
$options
[
'fields'
][
$name
]
=
$options
[
'definitions'
][
$name
];
// Legacy end.
}
$newDefinition
=
NULL
;
$fieldType
=
NULL
;
if
(
$fieldTypeConfiguration
=
$options
[
'fields'
][
$name
]
??
NULL
)
{
if
(
isset
(
$fieldTypeConfiguration
[
'#type'
]))
{
TestHelpers
::
throwUserError
(
'The "#type" key is deprecated to match the configuration naming, use "type" instead.'
);
$fieldTypeConfiguration
[
'type'
]
??=
$fieldTypeConfiguration
[
'#type'
];
}
if
(
isset
(
$fieldTypeConfiguration
[
'#settings'
]))
{
TestHelpers
::
throwUserError
(
'The "#settings" key is deprecated to match the configuration naming, use "settings" instead.'
);
$fieldTypeConfiguration
[
'settings'
]
??=
$fieldTypeConfiguration
[
'#settings'
];
}
// Legacy start.
// @todo Deprecate and remove this.
if
(
is_array
(
$fieldTypeConfiguration
))
{
if
(
isset
(
$fieldTypeConfiguration
[
'#type'
]))
{
TestHelpers
::
throwUserError
(
'The "#type" key is deprecated to match the configuration naming, use "type" instead.'
);
$fieldTypeConfiguration
[
'type'
]
??=
$fieldTypeConfiguration
[
'#type'
];
}
if
(
isset
(
$fieldTypeConfiguration
[
'#settings'
]))
{
TestHelpers
::
throwUserError
(
'The "#settings" key is deprecated to match the configuration naming, use "settings" instead.'
);
$fieldTypeConfiguration
[
'settings'
]
??=
$fieldTypeConfiguration
[
'#settings'
];
}
}
// Legacy end.
if
(
is_object
(
$fieldTypeConfiguration
))
{
$newDefinition
=
$fieldTypeConfiguration
;
$fieldTypeConfiguration
=
NULL
;
}
if
(
is_string
(
$fieldTypeConfiguration
))
{
else
if
(
is_string
(
$fieldTypeConfiguration
))
{
// Parsing value as a field type scalar value.
$fieldType
=
$fieldTypeConfiguration
;
if
(
$fieldType
==
'entity_reference'
)
{
...
...
This diff is collapsed.
Click to expand it.
tests/src/Unit/TestHelpersApi/CreateEntityStubTest.php
+
35
−
1
View file @
ed0b8011
...
...
@@ -2,6 +2,9 @@
namespace
Drupal\Tests\test_helpers\Unit\TestHelpersApi
;
use
Drupal\Core\Field\BaseFieldDefinition
;
use
Drupal\Core\Field\FieldItemList
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\node\Entity\Node
;
use
Drupal\node\Entity\NodeType
;
use
Drupal\Tests\UnitTestCase
;
...
...
@@ -154,7 +157,7 @@ class CreateEntityStubTest extends UnitTestCase {
}
/**
* Tests creating entities with
custom
methods.
* Tests creating entities with
mocked
methods.
*
* @covers ::createEntity
* @covers \Drupal\test_helpers\StubFactory\EntityStubFactory::create
...
...
@@ -184,4 +187,35 @@ class CreateEntityStubTest extends UnitTestCase {
$this
->
assertEquals
(
'Email successfully sent.'
,
$entitySendResult
);
}
/**
* Tests creating entities with mocked methods.
*
* @covers ::createEntity
* @covers \Drupal\test_helpers\StubFactory\EntityStubFactory::create
*/
public
function
testEntityWithMockedFields
()
{
$customField
=
$this
->
createMock
(
FieldItemListInterface
::
class
);
$customField
->
method
(
'getValue'
)
->
willReturn
(
'My custom value'
);
$customFieldDefinition
=
$this
->
createMock
(
BaseFieldDefinition
::
class
);
$customFieldDefinition
->
method
(
'getDataType'
)
->
willReturn
(
'string'
);
$customFieldDefinition
->
method
(
'getClass'
)
->
willReturn
(
FieldItemList
::
class
);
$customFieldDefinition
->
method
(
'getPropertyNames'
)
->
willReturn
(
[
'custom_property_1'
,
'custom_property_2'
]
);
$entity1
=
TestHelpers
::
saveEntity
(
Node
::
class
,
[
'title'
=>
'Article 1'
,
'field_custom'
=>
$customField
,
'field_very_custom'
=>
NULL
,
],
NULL
,
[
'fields'
=>
[
'field_very_custom'
=>
$customFieldDefinition
]],
);
$entity2
=
TestHelpers
::
saveEntity
(
Node
::
class
,
[
'title'
=>
'Article 2'
]);
$this
->
assertEquals
(
'My custom value'
,
$entity1
->
field_custom
->
getValue
());
$this
->
assertEquals
([
'custom_property_1'
,
'custom_property_2'
],
$entity2
->
field_very_custom
->
getFieldDefinition
()
->
getPropertyNames
()
);
}
}
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