OptionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class OptionTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $selectionFirst;
  14. /**
  15. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $selectionSecond;
  18. /**
  19. * @var \Magento\Framework\Model\ResourceModel\AbstractResource|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $resource;
  22. /**
  23. * @var \Magento\Bundle\Model\Option
  24. */
  25. protected $model;
  26. protected function setUp()
  27. {
  28. $this->selectionFirst = $this->createPartialMock(
  29. \Magento\Catalog\Model\Product::class,
  30. ['__wakeup', 'isSaleable', 'getIsDefault', 'getSelectionId']
  31. );
  32. $this->selectionSecond = $this->createPartialMock(
  33. \Magento\Catalog\Model\Product::class,
  34. ['__wakeup', 'isSaleable', 'getIsDefault', 'getSelectionId']
  35. );
  36. $this->resource = $this->createPartialMock(\Magento\Framework\Model\ResourceModel\AbstractResource::class, [
  37. '_construct',
  38. 'getConnection',
  39. 'getIdFieldName',
  40. 'getSearchableData',
  41. ]);
  42. $this->model = (new ObjectManager($this))->getObject(\Magento\Bundle\Model\Option::class, [
  43. 'resource' => $this->resource,
  44. ]);
  45. }
  46. /**
  47. * @covers \Magento\Bundle\Model\Option::addSelection
  48. */
  49. public function testAddSelection()
  50. {
  51. $this->model->addSelection($this->selectionFirst);
  52. $this->assertContains($this->selectionFirst, $this->model->getSelections());
  53. }
  54. public function testIsSaleablePositive()
  55. {
  56. $this->selectionFirst->expects($this->any())->method('isSaleable')->will($this->returnValue(true));
  57. $this->selectionSecond->expects($this->any())->method('isSaleable')->will($this->returnValue(false));
  58. $this->model->setSelections([$this->selectionFirst, $this->selectionSecond]);
  59. $this->assertTrue($this->model->isSaleable());
  60. }
  61. public function testIsSaleableNegative()
  62. {
  63. $this->selectionFirst->expects($this->any())->method('isSaleable')->will($this->returnValue(false));
  64. $this->selectionSecond->expects($this->any())->method('isSaleable')->will($this->returnValue(false));
  65. $this->model->setSelections([$this->selectionFirst, $this->selectionSecond]);
  66. $this->assertFalse($this->model->isSaleable());
  67. }
  68. public function testGetDefaultSelection()
  69. {
  70. $this->selectionFirst->expects($this->any())->method('getIsDefault')->will($this->returnValue(true));
  71. $this->selectionSecond->expects($this->any())->method('getIsDefault')->will($this->returnValue(false));
  72. $this->model->setSelections([$this->selectionFirst, $this->selectionSecond]);
  73. $this->assertEquals($this->selectionFirst, $this->model->getDefaultSelection());
  74. }
  75. public function testGetDefaultSelectionNegative()
  76. {
  77. $this->selectionFirst->expects($this->any())->method('getIsDefault')->will($this->returnValue(false));
  78. $this->selectionSecond->expects($this->any())->method('getIsDefault')->will($this->returnValue(false));
  79. $this->model->setSelections([$this->selectionFirst, $this->selectionSecond]);
  80. $this->assertNull($this->model->getDefaultSelection());
  81. }
  82. /**
  83. * @param string $type
  84. * @param bool $expectedValue
  85. * @dataProvider dataProviderForIsMultiSelection
  86. */
  87. public function testIsMultiSelection($type, $expectedValue)
  88. {
  89. $this->model->setType($type);
  90. $this->assertEquals($expectedValue, $this->model->isMultiSelection());
  91. }
  92. /**
  93. * @return array
  94. */
  95. public function dataProviderForIsMultiSelection()
  96. {
  97. return [
  98. ['checkbox', true],
  99. ['multi', true],
  100. ['some_type', false],
  101. ];
  102. }
  103. public function testGetSearchableData()
  104. {
  105. $productId = 15;
  106. $storeId = 1;
  107. $data = 'data';
  108. $this->resource->expects($this->any())->method('getSearchableData')->with($productId, $storeId)
  109. ->will($this->returnValue($data));
  110. $this->assertEquals($data, $this->model->getSearchableData($productId, $storeId));
  111. }
  112. public function testGetSelectionById()
  113. {
  114. $selectionId = 15;
  115. $this->selectionFirst->expects($this->any())->method('getSelectionId')->will($this->returnValue($selectionId));
  116. $this->selectionSecond->expects($this->any())->method('getSelectionId')->will($this->returnValue(16));
  117. $this->model->setSelections([$this->selectionFirst, $this->selectionSecond]);
  118. $this->assertEquals($this->selectionFirst, $this->model->getSelectionById($selectionId));
  119. }
  120. public function testGetSelectionByIdNegative()
  121. {
  122. $selectionId = 15;
  123. $this->selectionFirst->expects($this->any())->method('getSelectionId')->will($this->returnValue(16));
  124. $this->selectionSecond->expects($this->any())->method('getSelectionId')->will($this->returnValue(17));
  125. $this->model->setSelections([$this->selectionFirst, $this->selectionSecond]);
  126. $this->assertNull($this->model->getSelectionById($selectionId));
  127. }
  128. }