Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Entity Class Reference

Public Member Functions

 __construct (\Magento\Framework\Model\AbstractModel $model, array $updateData, $modelClass=null)
 
 testCrud ()
 

Protected Member Functions

 _getEmptyModel ()
 
 _testCreate ()
 
 _testRead ()
 
 _testUpdate ()
 
 _testDelete ()
 

Protected Attributes

 $_model
 
 $_updateData
 
 $_modelClass
 

Detailed Description

Class that implements CRUD tests for \Magento\Framework\Model\AbstractModel based objects

Definition at line 12 of file Entity.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\AbstractModel  $model,
array  $updateData,
  $modelClass = null 
)
Parameters
\Magento\Framework\Model\AbstractModel$model
array$updateData
string | null$modelClassClass of a model to use when creating new instances, or NULL for auto-detection
Exceptions

Definition at line 35 of file Entity.php.

36  {
37  $this->_model = $model;
38  $this->_updateData = $updateData;
39  if ($modelClass) {
40  if (!$model instanceof $modelClass) {
41  throw new \InvalidArgumentException("Class '$modelClass' is irrelevant to the tested model.");
42  }
43  $this->_modelClass = $modelClass;
44  } else {
45  $this->_modelClass = get_class($this->_model);
46  }
47  }

Member Function Documentation

◆ _getEmptyModel()

_getEmptyModel ( )
protected

Retrieve new instance of not yet loaded model

Returns
\Magento\Framework\Model\AbstractModel

Definition at line 70 of file Entity.php.

71  {
72  return \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($this->_modelClass);
73  }

◆ _testCreate()

_testCreate ( )
protected

Definition at line 75 of file Entity.php.

76  {
77  if ($this->_model->getId()) {
78  \PHPUnit\Framework\Assert::fail("Can't run creation test for models with defined id");
79  }
80  $this->_model->save();
81  \PHPUnit\Framework\Assert::assertNotEmpty($this->_model->getId(), 'CRUD Create error');
82  }

◆ _testDelete()

_testDelete ( )
protected

Definition at line 109 of file Entity.php.

110  {
111  $modelId = $this->_model->getId();
112  $this->_model->delete();
113 
114  $model = $this->_getEmptyModel();
115  $model->load($modelId);
116  \PHPUnit\Framework\Assert::assertEmpty($model->getId(), 'CRUD Delete error');
117  }

◆ _testRead()

_testRead ( )
protected

Definition at line 84 of file Entity.php.

85  {
86  $model = $this->_getEmptyModel();
87  $model->load($this->_model->getId());
88  \PHPUnit\Framework\Assert::assertEquals($this->_model->getId(), $model->getId(), 'CRUD Read error');
89  }

◆ _testUpdate()

_testUpdate ( )
protected

Definition at line 91 of file Entity.php.

92  {
93  foreach ($this->_updateData as $key => $value) {
94  $this->_model->setDataUsingMethod($key, $value);
95  }
96  $this->_model->save();
97 
98  $model = $this->_getEmptyModel();
99  $model->load($this->_model->getId());
100  foreach ($this->_updateData as $key => $value) {
101  \PHPUnit\Framework\Assert::assertEquals(
102  $value,
103  $model->getDataUsingMethod($key),
104  'CRUD Update "' . $key . '" error'
105  );
106  }
107  }
$value
Definition: gender.phtml:16

◆ testCrud()

testCrud ( )

Test Create -> Read -> Update -> Delete operations

Definition at line 52 of file Entity.php.

53  {
54  $this->_testCreate();
55  try {
56  $this->_testRead();
57  $this->_testUpdate();
58  $this->_testDelete();
59  } catch (\Exception $e) {
60  $this->_model->delete();
61  throw $e;
62  }
63  }

Field Documentation

◆ $_model

$_model
protected

Definition at line 17 of file Entity.php.

◆ $_modelClass

$_modelClass
protected

Definition at line 27 of file Entity.php.

◆ $_updateData

$_updateData
protected

Definition at line 22 of file Entity.php.


The documentation for this class was generated from the following file: