Entity.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework;
  7. /**
  8. * Class that implements CRUD tests for \Magento\Framework\Model\AbstractModel based objects
  9. */
  10. class Entity
  11. {
  12. /**
  13. * @var \Magento\Framework\Model\AbstractModel
  14. */
  15. protected $_model;
  16. /**
  17. * @var array
  18. */
  19. protected $_updateData;
  20. /**
  21. * @var string
  22. */
  23. protected $_modelClass;
  24. /**
  25. * @param \Magento\Framework\Model\AbstractModel $model
  26. * @param array $updateData
  27. * @param string|null $modelClass Class of a model to use when creating new instances, or NULL for auto-detection
  28. * @throws \InvalidArgumentException
  29. */
  30. public function __construct(\Magento\Framework\Model\AbstractModel $model, array $updateData, $modelClass = null)
  31. {
  32. $this->_model = $model;
  33. $this->_updateData = $updateData;
  34. if ($modelClass) {
  35. if (!$model instanceof $modelClass) {
  36. throw new \InvalidArgumentException("Class '$modelClass' is irrelevant to the tested model.");
  37. }
  38. $this->_modelClass = $modelClass;
  39. } else {
  40. $this->_modelClass = get_class($this->_model);
  41. }
  42. }
  43. /**
  44. * Test Create -> Read -> Update -> Delete operations
  45. */
  46. public function testCrud()
  47. {
  48. $this->_testCreate();
  49. try {
  50. $this->_testRead();
  51. $this->_testUpdate();
  52. $this->_testDelete();
  53. } catch (\Exception $e) {
  54. $this->_model->delete();
  55. throw $e;
  56. }
  57. }
  58. /**
  59. * Retrieve new instance of not yet loaded model
  60. *
  61. * @return \Magento\Framework\Model\AbstractModel
  62. */
  63. protected function _getEmptyModel()
  64. {
  65. return \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($this->_modelClass);
  66. }
  67. protected function _testCreate()
  68. {
  69. if ($this->_model->getId()) {
  70. \PHPUnit\Framework\Assert::fail("Can't run creation test for models with defined id");
  71. }
  72. $this->_model->save();
  73. \PHPUnit\Framework\Assert::assertNotEmpty($this->_model->getId(), 'CRUD Create error');
  74. }
  75. protected function _testRead()
  76. {
  77. $model = $this->_getEmptyModel();
  78. $model->load($this->_model->getId());
  79. \PHPUnit\Framework\Assert::assertEquals($this->_model->getId(), $model->getId(), 'CRUD Read error');
  80. }
  81. protected function _testUpdate()
  82. {
  83. foreach ($this->_updateData as $key => $value) {
  84. $this->_model->setDataUsingMethod($key, $value);
  85. }
  86. $this->_model->save();
  87. $model = $this->_getEmptyModel();
  88. $model->load($this->_model->getId());
  89. foreach ($this->_updateData as $key => $value) {
  90. \PHPUnit\Framework\Assert::assertEquals(
  91. $value,
  92. $model->getDataUsingMethod($key),
  93. 'CRUD Update "' . $key . '" error'
  94. );
  95. }
  96. }
  97. protected function _testDelete()
  98. {
  99. $modelId = $this->_model->getId();
  100. $this->_model->delete();
  101. $model = $this->_getEmptyModel();
  102. $model->load($modelId);
  103. \PHPUnit\Framework\Assert::assertEmpty($model->getId(), 'CRUD Delete error');
  104. }
  105. }