Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
test_helpers-3333515
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-3333515
Commits
ceb5ae9c
Commit
ceb5ae9c
authored
2 years ago
by
Alexey Korepov
Browse files
Options
Downloads
Plain Diff
Merge branch '1.0.x' of git.drupal.org:project/test_helpers into 1.0.x
parents
c3467a23
03ed87ba
No related branches found
Branches containing commit
Tags
1.0.0-alpha2
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/EntityStorageStubFactory.php
+9
-1
9 additions, 1 deletion
src/EntityStorageStubFactory.php
src/EntityStubFactory.php
+24
-12
24 additions, 12 deletions
src/EntityStubFactory.php
src/FieldItemListStubFactory.php
+18
-0
18 additions, 0 deletions
src/FieldItemListStubFactory.php
with
51 additions
and
13 deletions
src/EntityStorageStubFactory.php
+
9
−
1
View file @
ceb5ae9c
...
...
@@ -27,6 +27,7 @@ class EntityStorageStubFactory extends UnitTestCase {
'loadMultiple'
,
'loadByProperties'
,
'delete'
,
'invokeHook'
,
// Custom helper functions for the stub:
// Attaches an entity type object to the stub.
...
...
@@ -48,10 +49,17 @@ class EntityStorageStubFactory extends UnitTestCase {
// we've got an error "Indirect modification of overloaded property
// Mock_SqlContentEntityStorage_6202ec22::$stubStorage has no effect" if
// try to use a new own property for this.
$propertyToStoreEntities
=
'tab
leMapping
'
;
$propertyToStoreEntities
=
'
da
tab
ase
'
;
$entityStorageStub
->
stubEntityStorageById
=
[];
UnitTestHelpers
::
bindClosureToClassMethod
(
function
()
{
},
$entityStorageStub
,
'invokeHook'
);
UnitTestHelpers
::
bindClosureToClassMethod
(
function
(
EntityTypeInterface
$entityType
)
{
$this
->
entityType
=
$entityType
;
...
...
This diff is collapsed.
Click to expand it.
src/EntityStubFactory.php
+
24
−
12
View file @
ceb5ae9c
...
...
@@ -5,6 +5,7 @@ namespace Drupal\test_helpers;
use
Drupal\Component\Uuid\Php
as
PhpUuid
;
use
Drupal\Core\Entity\EntityFieldManagerInterface
;
use
Drupal\Core\Entity\EntityRepositoryInterface
;
use
Drupal\Core\Entity\Plugin\DataType\EntityAdapter
;
use
Drupal\Core\Field\Plugin\Field\FieldType\StringItem
;
use
Drupal\Core\Language\LanguageInterface
;
use
Drupal\Tests\UnitTestCase
;
...
...
@@ -82,11 +83,13 @@ class EntityStubFactory extends UnitTestCase {
$entityTypeId
=
$storageNew
->
getEntityTypeId
();
$storage
=
$this
->
entityTypeManager
->
stubGetOrCreateStorage
(
$entityTypeId
,
$storageNew
);
$bundle
=
$values
[
$entityTypeDefinition
->
getKey
(
'bundle'
)]
??
$entityTypeId
;
// Creating a stub of the entity.
// @todo Try to init with a real constructor.
/** @var \Drupal\Core\Entity\ContentEntityInterface|\PHPUnit\Framework\MockObject\MockObject $entity */
$entity
=
$this
->
createPartialMock
(
$entityClass
,
[
'getEntityTypeId'
,
//
'getEntityTypeId',
// 'getFieldDefinitions',
'save'
,
'delete'
,
...
...
@@ -104,9 +107,24 @@ class EntityStubFactory extends UnitTestCase {
// Filling values to the entity array.
$fieldItemListStubFactory
=
$this
->
fieldItemListStubFactory
;
UnitTestHelpers
::
bindClosureToClassMethod
(
function
(
array
$values
)
use
(
$fieldItemListStubFactory
,
$options
)
{
function
(
array
$values
)
use
(
$fieldItemListStubFactory
,
$options
,
$entityTypeId
,
$bundle
)
{
// Pre-filling entity keys.
$this
->
entityTypeId
=
$entityTypeId
;
$this
->
entityKeys
[
'bundle'
]
=
$bundle
?
$bundle
:
$this
->
entityTypeId
;
foreach
(
$this
->
getEntityType
()
->
getKeys
()
as
$key
=>
$field
)
{
if
(
isset
(
$values
[
$field
]))
{
$this
->
entityKeys
[
$key
]
=
$values
[
$field
];
}
}
$this
->
langcodeKey
=
$this
->
getEntityType
()
->
getKey
(
'langcode'
);
$this
->
defaultLangcodeKey
=
$this
->
getEntityType
()
->
getKey
(
'default_langcode'
);
$this
->
revisionTranslationAffectedKey
=
$this
->
getEntityType
()
->
getKey
(
'revision_translation_affected'
);
// Filling common values.
$this
->
translations
[
LanguageInterface
::
LANGCODE_DEFAULT
]
=
[
'status'
=>
TRUE
];
$this
->
translations
[
LanguageInterface
::
LANGCODE_DEFAULT
]
=
[
'status'
=>
TRUE
,
'entity'
=>
$this
,
];
// Filling values to the entity array.
foreach
(
$values
as
$name
=>
$value
)
{
...
...
@@ -115,7 +133,9 @@ class EntityStubFactory extends UnitTestCase {
}
// @todo Convert entity to TypedDataInterface and pass to the
// item list initialization as a third argument $parent.
$field
=
$fieldItemListStubFactory
->
create
(
$name
,
$value
,
$definition
??
NULL
);
// $parent = EntityAdapter::createFromEntity($this);
$parent
=
NULL
;
$field
=
$fieldItemListStubFactory
->
create
(
$name
,
$value
,
$definition
??
NULL
,
$parent
);
$this
->
fieldDefinitions
[
$name
]
=
$field
->
getFieldDefinition
();
$this
->
fields
[
$name
][
LanguageInterface
::
LANGCODE_DEFAULT
]
=
$field
;
}
...
...
@@ -126,14 +146,6 @@ class EntityStubFactory extends UnitTestCase {
);
$entity
->
stubInitValues
(
$values
);
UnitTestHelpers
::
bindClosureToClassMethod
(
function
()
use
(
$entityTypeId
)
{
return
$entityTypeId
;
},
$entity
,
'getEntityTypeId'
);
UnitTestHelpers
::
bindClosureToClassMethod
(
function
()
use
(
$storage
)
{
$idProperty
=
$this
->
getEntityType
()
->
getKey
(
'id'
)
??
NULL
;
...
...
This diff is collapsed.
Click to expand it.
src/FieldItemListStubFactory.php
+
18
−
0
View file @
ceb5ae9c
...
...
@@ -30,6 +30,7 @@ class FieldItemListStubFactory extends TestCase {
*/
public
function
__construct
(
FieldTypeManagerStub
$fieldTypeManagerStub
)
{
$this
->
fieldTypeManagerStub
=
$fieldTypeManagerStub
;
$this
->
unitTestHelpers
=
new
UnitTestHelpers
();
}
/**
...
...
@@ -80,6 +81,23 @@ class FieldItemListStubFactory extends TestCase {
$definition
=
$this
->
createFieldItemDefinitionStub
(
$name
,
$type
,
StringItem
::
class
);
}
$field
=
new
FieldItemList
(
$definition
,
$name
,
$parent
);
$field
=
$this
->
unitTestHelpers
->
createPartialMockWithCostructor
(
FieldItemList
::
class
,
[
'applyDefaultValue'
,
],
[
$definition
,
$name
,
$parent
]
);
// We have no information about default values because of missing configs,
// so just return the same object.
UnitTestHelpers
::
bindClosureToClassMethod
(
function
(
$notify
=
TRUE
)
{
return
$this
;
},
$field
,
'applyDefaultValue'
);
$field
->
setValue
(
$values
);
return
$field
;
...
...
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