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

Public Member Functions

 saveModelSuccessfully ()
 
 saveModelAndFailOnUpdate ()
 
 deleteModelSuccessfully ()
 
 testConstructorIrrelevantModelClass ()
 
 crudDataProvider ()
 
 testTestCrud ($saveCallback, $expectedException=null)
 

Protected Member Functions

 setUp ()
 

Protected Attributes

 $_model
 

Detailed Description

Definition at line 8 of file EntityTest.php.

Member Function Documentation

◆ crudDataProvider()

crudDataProvider ( )

Definition at line 62 of file EntityTest.php.

63  {
64  return [
65  'successful CRUD' => ['saveModelSuccessfully'],
66  'cleanup on update error' => [
67  'saveModelAndFailOnUpdate',
68  \Magento\Framework\Exception\LocalizedException::class
69  ]
70  ];
71  }

◆ deleteModelSuccessfully()

deleteModelSuccessfully ( )

Callback for delete method in mocked model

Definition at line 48 of file EntityTest.php.

49  {
50  $this->_model->setId(null);
51  }

◆ saveModelAndFailOnUpdate()

saveModelAndFailOnUpdate ( )

Callback for save method in mocked model

Exceptions

Definition at line 36 of file EntityTest.php.

37  {
38  if (!$this->_model->getId()) {
39  $this->saveModelSuccessfully();
40  } else {
41  throw new \Magento\Framework\Exception\LocalizedException(__('Synthetic model update failure.'));
42  }
43  }
__()
Definition: __.php:13

◆ saveModelSuccessfully()

saveModelSuccessfully ( )

Callback for save method in mocked model

Definition at line 26 of file EntityTest.php.

27  {
28  $this->_model->setId('1');
29  }

◆ setUp()

setUp ( )
protected

Definition at line 15 of file EntityTest.php.

16  {
17  $this->_model = $this->createPartialMock(
18  \Magento\Framework\Model\AbstractModel::class,
19  ['load', 'save', 'delete', 'getIdFieldName', '__wakeup']
20  );
21  }

◆ testConstructorIrrelevantModelClass()

testConstructorIrrelevantModelClass ( )

@expectedException \InvalidArgumentException @expectedExceptionMessage Class 'stdClass' is irrelevant to the tested model

Definition at line 57 of file EntityTest.php.

58  {
59  new \Magento\TestFramework\Entity($this->_model, [], 'stdClass');
60  }

◆ testTestCrud()

testTestCrud (   $saveCallback,
  $expectedException = null 
)

@dataProvider crudDataProvider

Definition at line 76 of file EntityTest.php.

77  {
78  if ($expectedException != null) {
79  $this->expectException($expectedException);
80  }
81 
82  $this->_model->expects($this->atLeastOnce())
83  ->method('load');
84  $this->_model->expects($this->atLeastOnce())
85  ->method('save')
86  ->will($this->returnCallback([$this, $saveCallback]));
87  /* It's important that 'delete' should be always called to guarantee the cleanup */
88  $this->_model->expects(
89  $this->atLeastOnce()
90  )->method(
91  'delete'
92  )->will(
93  $this->returnCallback([$this, 'deleteModelSuccessfully'])
94  );
95 
96  $this->_model->expects($this->any())->method('getIdFieldName')->will($this->returnValue('id'));
97 
98  $test = $this->getMockBuilder(\Magento\TestFramework\Entity::class)
99  ->setMethods(['_getEmptyModel'])
100  ->setConstructorArgs([$this->_model, ['test' => 'test']])
101  ->getMock();
102 
103  $test->expects($this->any())->method('_getEmptyModel')->will($this->returnValue($this->_model));
104  $test->testCrud();
105  }

Field Documentation

◆ $_model

$_model
protected

Definition at line 13 of file EntityTest.php.


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