|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Bundle\Test\Unit\Model\ResourceModel; |
| 9 | + |
| 10 | +use Codeception\PHPUnit\TestCase; |
| 11 | +use Magento\Bundle\Model\ResourceModel\Selection as ResourceSelection; |
| 12 | +use Magento\Bundle\Model\Selection; |
| 13 | +use Magento\Framework\App\ResourceConnection; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 15 | +use Magento\Framework\EntityManager\MetadataPool; |
| 16 | +use Magento\Framework\Model\ResourceModel\Db\Context; |
| 17 | + |
| 18 | +class SelectionTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Context|Context&\PHPUnit\Framework\MockObject\MockObject|\PHPUnit\Framework\MockObject\MockObject |
| 22 | + */ |
| 23 | + private Context $context; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var MetadataPool|MetadataPool&\PHPUnit\Framework\MockObject\MockObject|\PHPUnit\Framework\MockObject\MockObject |
| 27 | + */ |
| 28 | + private MetadataPool $metadataPool; |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritdoc |
| 32 | + */ |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + |
| 37 | + $this->context = $this->createMock(Context::class); |
| 38 | + $this->metadataPool = $this->createMock(MetadataPool::class); |
| 39 | + } |
| 40 | + |
| 41 | + public function testSaveSelectionPrice() |
| 42 | + { |
| 43 | + $item = $this->getMockBuilder(Selection::class) |
| 44 | + ->disableOriginalConstructor() |
| 45 | + ->addMethods([ |
| 46 | + 'getSelectionId', |
| 47 | + 'getWebsiteId', |
| 48 | + 'getSelectionPriceType', |
| 49 | + 'getSelectionPriceValue', |
| 50 | + 'getParentProductId', |
| 51 | + 'getDefaultPriceScope']) |
| 52 | + ->getMock(); |
| 53 | + $values = [ |
| 54 | + 'selection_id' => 1, |
| 55 | + 'website_id' => 1, |
| 56 | + 'selection_price_type' => null, |
| 57 | + 'selection_price_value' => null, |
| 58 | + 'parent_product_id' => 1, |
| 59 | + ]; |
| 60 | + $item->expects($this->once())->method('getDefaultPriceScope')->willReturn(false); |
| 61 | + $item->expects($this->once())->method('getSelectionId')->willReturn($values['selection_id']); |
| 62 | + $item->expects($this->once())->method('getWebsiteId')->willReturn($values['website_id']); |
| 63 | + $item->expects($this->once())->method('getSelectionPriceType')->willReturn($values['selection_price_type']); |
| 64 | + $item->expects($this->once())->method('getSelectionPriceValue')->willReturn($values['selection_price_value']); |
| 65 | + $item->expects($this->once())->method('getParentProductId')->willReturn($values['parent_product_id']); |
| 66 | + |
| 67 | + $connection = $this->createMock(AdapterInterface::class); |
| 68 | + $connection->expects($this->once()) |
| 69 | + ->method('insertOnDuplicate') |
| 70 | + ->with( |
| 71 | + 'catalog_product_bundle_selection_price', |
| 72 | + $this->callback(function ($insertValues) { |
| 73 | + return $insertValues['selection_price_type'] === 0 && $insertValues['selection_price_value'] === 0; |
| 74 | + }), |
| 75 | + ['selection_price_type', 'selection_price_value'] |
| 76 | + ); |
| 77 | + |
| 78 | + $parentResources = $this->createMock(ResourceConnection::class); |
| 79 | + $parentResources->expects($this->once())->method('getConnection')->willReturn($connection); |
| 80 | + $parentResources->expects($this->once())->method('getTableName') |
| 81 | + ->with('catalog_product_bundle_selection_price', 'test_connection_name') |
| 82 | + ->willReturn('catalog_product_bundle_selection_price'); |
| 83 | + $this->context->expects($this->once())->method('getResources')->willReturn($parentResources); |
| 84 | + |
| 85 | + $selection = new ResourceSelection($this->context, $this->metadataPool, 'test_connection_name'); |
| 86 | + $selection->saveSelectionPrice($item); |
| 87 | + } |
| 88 | +} |
0 commit comments