MetadataTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\Entity\VersionControl;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Test for version control metadata model.
  10. */
  11. class MetadataTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Eav\Model\Entity\VersionControl\Metadata
  15. */
  16. protected $metadata;
  17. /**
  18. * @var \Magento\Framework\Model\AbstractModel|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $model;
  21. /**
  22. * @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $resource;
  25. /**
  26. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $connection;
  29. protected function setUp()
  30. {
  31. $objectManager = new ObjectManager($this);
  32. $this->model = $this->createPartialMock(
  33. \Magento\Framework\Model\AbstractModel::class,
  34. ['getResource', 'getAttributes']
  35. );
  36. $this->resource = $this->getMockForAbstractClass(
  37. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  38. [],
  39. '',
  40. false,
  41. false,
  42. true,
  43. ['getConnection', 'getEntityTable']
  44. );
  45. $this->connection = $this->getMockForAbstractClass(
  46. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  47. [],
  48. '',
  49. false,
  50. false
  51. );
  52. $this->model->expects($this->any())->method('getResource')->willReturn($this->resource);
  53. $this->resource->expects($this->any())->method('getConnection')->willReturn($this->connection);
  54. $this->metadata = $objectManager->getObject(
  55. \Magento\Eav\Model\Entity\VersionControl\Metadata::class
  56. );
  57. }
  58. public function testGetFields()
  59. {
  60. $entityTable = 'entity_table';
  61. $expectedDescribedTable = ['field1' => null, 'field2' => null];
  62. $expectedAttributes = ['attribute1' => 'value1', 'attribute2' => 'value2'];
  63. $expectedResults = array_merge($expectedDescribedTable, $expectedAttributes);
  64. $this->resource->expects($this->any())->method('getEntityTable')->willReturn($entityTable);
  65. $this->connection->expects($this->once())->method('describeTable')->with($entityTable)->willReturn(
  66. $expectedDescribedTable
  67. );
  68. $this->model->expects($this->any())->method('getAttributes')->willReturn($expectedAttributes);
  69. //check that fields load with null initial value
  70. $this->assertEquals(
  71. array_fill_keys(array_keys($expectedResults), null),
  72. $this->metadata->getFields($this->model)
  73. );
  74. // Testing loading data from cache.
  75. $this->connection->expects($this->never())->method('describeTable');
  76. $this->assertEquals(
  77. array_fill_keys(array_keys($expectedResults), null),
  78. $this->metadata->getFields($this->model)
  79. );
  80. }
  81. }