ProductVariationsBuilderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Test\Unit\Model;
  7. class ProductVariationsBuilderTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var ProductVariationsBuilder
  11. */
  12. protected $model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $customAttributeFactory;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $productFactory;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $variationMatrix;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $product;
  29. protected function setUp()
  30. {
  31. $this->customAttributeFactory = $this->createMock(\Magento\Framework\Api\AttributeValueFactory::class);
  32. $this->product = $this->createPartialMock(
  33. \Magento\Catalog\Model\Product::class,
  34. ['getData', 'getPrice', 'getName', 'getSku', '__wakeup', 'getCustomAttributes']
  35. );
  36. $this->productFactory = $this->createPartialMock(\Magento\Catalog\Model\ProductFactory::class, ['create']);
  37. $this->variationMatrix = $this->createMock(
  38. \Magento\ConfigurableProduct\Model\Product\Type\VariationMatrix::class
  39. );
  40. $this->model = new \Magento\ConfigurableProduct\Model\ProductVariationsBuilder(
  41. $this->productFactory,
  42. $this->customAttributeFactory,
  43. $this->variationMatrix
  44. );
  45. }
  46. public function testCreate()
  47. {
  48. $output = $this->createPartialMock(
  49. \Magento\Catalog\Model\Product::class,
  50. ['setPrice', '__wakeup', 'setData', 'getCustomAttributes', 'setName', 'setSku', 'setVisibility']
  51. );
  52. $attributes = [10 => ['attribute_code' => 'sort_order']];
  53. $variations = [
  54. [10 => ['value' => 15, 'price' => ['pricing_value' => 10]]],
  55. ];
  56. $this->variationMatrix->expects($this->once())
  57. ->method('getVariations')
  58. ->with($attributes)
  59. ->willReturn($variations);
  60. $this->productFactory->expects($this->once())->method('create')->willReturn($output);
  61. $productData = ['id' => '10', 'title' => 'simple'];
  62. $this->product->expects($this->once())->method('getData')->willReturn($productData);
  63. $this->product->expects($this->once())->method('getName')->willReturn('simple');
  64. $this->product->expects($this->once())->method('getSku')->willReturn('simple-sku');
  65. $this->product->expects($this->once())->method('getPrice')->willReturn(10);
  66. $output->expects($this->at(0))->method('setData')->with($productData);
  67. $attribute = $this->createMock(\Magento\Framework\Api\AttributeInterface::class);
  68. $attribute->expects($this->once())
  69. ->method('setAttributeCode')
  70. ->with('sort_order')
  71. ->willReturnSelf();
  72. $attribute->expects($this->once())
  73. ->method('setValue')
  74. ->with(15)
  75. ->willReturnSelf();
  76. $this->customAttributeFactory->expects($this->once())
  77. ->method('create')
  78. ->willReturn($attribute);
  79. $output->expects($this->once())->method('getCustomAttributes')->willReturn([]);
  80. $output->expects($this->at(2))->method('setData')->with('custom_attributes', ['sort_order' => $attribute]);
  81. $output->expects($this->once())->method('setPrice')->with(10);
  82. $output->expects($this->once())->method('setName')->with('simple-15');
  83. $output->expects($this->once())->method('setSku')->with('simple-sku-15');
  84. $output->expects($this->once())->method('setVisibility')
  85. ->with(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE);
  86. $this->assertEquals([$output], $this->model->create($this->product, $attributes));
  87. }
  88. }