Skip to content

Commit

Permalink
Merge pull request #5477 from magento-tsg/2.4-develop-com-pr10
Browse files Browse the repository at this point in the history
[TSG-Commerce] Tests for 2.4 (pr10)
  • Loading branch information
zakdma committed Mar 20, 2020
2 parents ac3eb00 + 84d7357 commit 5ff31f6
Show file tree
Hide file tree
Showing 43 changed files with 5,187 additions and 1,174 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\SendFriend\Model;

use Magento\SendFriend\Model\ResourceModel\SendFriend as SendFriendResource;

/**
* Delete log rows by ip address
*/
class DeleteLogRowsByIp
{
/** @var SendFriendResource */
private $sendFriendResource;

/**
* @param SendFriendResource $sendFriendResource
*/
public function __construct(SendFriendResource $sendFriendResource)
{
$this->sendFriendResource = $sendFriendResource;
}

/**
* Delete rows from sendfriend_log table by ip address
*
* @param string $ipAddress
* @return void
*/
public function execute(string $ipAddress): void
{
$connection = $this->sendFriendResource->getConnection();
$condition = $connection->quoteInto('ip = ?', ip2long($ipAddress));
$connection->delete($this->sendFriendResource->getMainTable(), $condition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Wishlist\Model;

use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\Wishlist;
use Magento\Wishlist\Model\WishlistFactory;

/**
* Load wish list by customer id.
*/
class GetWishlistByCustomerId
{
/** @var WishlistFactory */
private $wishlistFactory;

/**
* @param WishlistFactory $wishlistFactory
*/
public function __construct(WishlistFactory $wishlistFactory)
{
$this->wishlistFactory = $wishlistFactory;
}

/**
* Load wish list by customer id.
*
* @param int $customerId
* @return Wishlist
*/
public function execute(int $customerId): Wishlist
{
return $this->wishlistFactory->create()->loadByCustomerId($customerId, true);
}

/**
* Get wish list item by sku.
*
* @param int $customerId
* @param string $sku
* @return null|Item
*/
public function getItemBySku(int $customerId, string $sku): ?Item
{
$result = null;
$items = $this->execute($customerId)->getItemCollection()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getData('sku') === $sku) {
$result = $item;
break;
}
}

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Controller\Adminhtml\Product;

use Magento\Bundle\Model\Product\Type;
use Magento\Bundle\Model\ResourceModel\Option\Collection as OptionCollection;
use Magento\Bundle\Model\ResourceModel\Selection\Collection as SelectionCollection;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Eav\Model\Config;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* Class determine basic logic for bundle product save tests
*/
abstract class AbstractBundleProductSaveTest extends AbstractBackendController
{
/** @var string */
protected $productToDelete;

/** @var ProductRepositoryInterface */
protected $productRepository;

/** @var Config */
private $eavConfig;

/** @var ProductResource */
private $productResource;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
$this->eavConfig = $this->_objectManager->get(Config::class);
$this->productResource = $this->_objectManager->get(ProductResource::class);
$this->productToDelete = $this->getStaticProductData()['sku'];
}

/**
* @inheritdoc
*/
protected function tearDown()
{
if ($this->productToDelete) {
$this->productRepository->deleteById($this->productToDelete);
}

parent::tearDown();
}

/**
* Retrieve default product attribute set id.
*
* @return int
*/
protected function getDefaultAttributeSetId(): int
{
return (int)$this->eavConfig->getEntityType(ProductAttributeInterface::ENTITY_TYPE_CODE)
->getDefaultAttributeSetId();
}

/**
* Prepare request
*
* @param array $post
* @param int|null $id
* @return array
*/
protected function prepareRequestData(array $post, ?int $id = null): array
{
$post = $this->preparePostParams($post);
$this->setRequestparams($post, $id);

return $post;
}

/**
* Prepare and assert bundle options
*
* @param array $bundleOptions
* @return void
*/
protected function assertBundleOptions(array $bundleOptions): void
{
$mainProduct = $this->productRepository->get($this->getStaticProductData()['sku'], false, null, true);
$optionsCollection = $mainProduct->getTypeInstance()->getOptionsCollection($mainProduct);
$selectionCollection = $mainProduct->getTypeInstance()
->getSelectionsCollection($optionsCollection->getAllIds(), $mainProduct);
$this->assertOptionsData($bundleOptions, $optionsCollection, $selectionCollection);
}

/**
* Prepare post params before dispatch
*
* @param array $post
* @return array
*/
private function preparePostParams(array $post): array
{
$post['product'] = $this->getStaticProductData();
foreach ($post['bundle_options']['bundle_options'] as &$bundleOption) {
$bundleOption = $this->prepareOptionByType($bundleOption['type'], $bundleOption);
$productIdsBySkus = $this->productResource->getProductsIdsBySkus(
array_column($bundleOption['bundle_selections'], 'sku')
);
foreach ($bundleOption['bundle_selections'] as &$bundleSelection) {
$bundleSelection = $this->prepareSelection($productIdsBySkus, $bundleSelection);
}
}

return $post;
}

/**
* Prepare option params
*
* @param string $type
* @param array $option
* @return array
*/
private function prepareOptionByType(string $type, array $option): array
{
$option['required'] = '1';
$option['delete'] = '';
$option['title'] = $option['title'] ?? $type . ' Option Title';

return $option;
}

/**
* Prepare selection params
*
* @param array $productIdsBySkus
* @param array $selection
* @return array
*/
private function prepareSelection(array $productIdsBySkus, array $selection): array
{
$staticData = [
'price' => '10',
'selection_qty' => '5',
'selection_can_change_qty' => '0'
];
$selection['product_id'] = $productIdsBySkus[$selection['sku']];
$selection = array_merge($selection, $staticData);

return $selection;
}

/**
* Assert bundle options data
*
* @param array $expectedOptions
* @param OptionCollection $actualOptions
* @param SelectionCollection $selectionCollection
* @return void
*/
private function assertOptionsData(
array $expectedOptions,
OptionCollection $actualOptions,
SelectionCollection $selectionCollection
): void {
$this->assertCount(count($expectedOptions['bundle_options']), $actualOptions);
foreach ($expectedOptions['bundle_options'] as $expectedOption) {
$optionToCheck = $actualOptions->getItemByColumnValue('title', $expectedOption['title']);
$this->assertNotNull($optionToCheck->getId());
$selectionToCheck = $selectionCollection->getItemsByColumnValue('option_id', $optionToCheck->getId());
$this->assertCount(count($expectedOption['bundle_selections']), $selectionToCheck);
$this->assertSelections($expectedOption['bundle_selections'], $selectionToCheck);
unset($expectedOption['delete'], $expectedOption['bundle_selections']);
foreach ($expectedOption as $key => $value) {
$this->assertEquals($value, $optionToCheck->getData($key));
}
}
}

/**
* Assert selections data
*
* @param array $expectedSelections
* @param array $actualSelections
* @return void
*/
private function assertSelections(array $expectedSelections, array $actualSelections): void
{
foreach ($expectedSelections as $expectedSelection) {
$actualSelectionToCheck = $this->getSelectionByProductSku($expectedSelection['sku'], $actualSelections);
$this->assertNotNull($actualSelectionToCheck);
foreach ($expectedSelection as $key => $value) {
$this->assertEquals($value, $actualSelectionToCheck->getData($key));
}
}
}

/**
* Get selection by product sku
*
* @param string $sku
* @param array $actualSelections
* @return ProductInterface
*/
private function getSelectionByProductSku(string $sku, array $actualSelections): ProductInterface
{
$item = null;
foreach ($actualSelections as $selection) {
if ($selection->getSku() === $sku) {
$item = $selection;
break;
}
}

return $item;
}

/**
* Set request parameters
*
* @param array $post
* @param int|null $id
* @return void
*/
private function setRequestParams(array $post, ?int $id): void
{
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$params = ['type' => Type::TYPE_CODE, 'set' => $this->getDefaultAttributeSetId()];
if ($id) {
$params['id'] = $id;
}
$this->getRequest()->setParams($params);
$this->getRequest()->setPostValue('product', $post['product']);
$this->getRequest()->setPostValue('bundle_options', $post['bundle_options']);
}

/**
* Get main product data
*
* @return array
*/
abstract protected function getStaticProductData(): array;
}
Loading

0 comments on commit 5ff31f6

Please sign in to comment.