-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit test for Magento\Downloadable\Model\Sample\DeleteHandler #26835
Merged
magento-engcom-team
merged 1 commit into
magento:2.4-develop
from
karyna-t:unit-test-downloadable-model-sample-deletehandler
Feb 20, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
app/code/Magento/Downloadable/Test/Unit/Model/Sample/DeleteHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,100 @@ | ||||||
<?php | ||||||
/** | ||||||
* Copyright © Magento, Inc. All rights reserved. | ||||||
* See COPYING.txt for license details. | ||||||
*/ | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Magento\Downloadable\Test\Unit\Model\Sample; | ||||||
|
||||||
use Magento\Catalog\Api\Data\ProductInterface; | ||||||
use Magento\Catalog\Model\Product; | ||||||
use Magento\Downloadable\Api\Data\SampleInterface; | ||||||
use Magento\Downloadable\Api\SampleRepositoryInterface as SampleRepository; | ||||||
use Magento\Downloadable\Model\Product\Type; | ||||||
use Magento\Downloadable\Model\Sample\DeleteHandler; | ||||||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; | ||||||
use PHPUnit\Framework\MockObject\MockObject; | ||||||
use PHPUnit\Framework\TestCase; | ||||||
|
||||||
/** | ||||||
* Unit Test for \Magento\Downloadable\Model\Sample\DeleteHandler | ||||||
*/ | ||||||
class DeleteHandlerTest extends TestCase | ||||||
{ | ||||||
const STUB_PRODUCT_TYPE = 'simple'; | ||||||
const STUB_PRODUCT_SKU = 'sku'; | ||||||
const STUB_SAMPLE_ID = 1; | ||||||
|
||||||
/** | ||||||
* @var ProductInterface|MockObject | ||||||
*/ | ||||||
private $entityMock; | ||||||
|
||||||
/** | ||||||
* @var SampleRepository|MockObject | ||||||
*/ | ||||||
private $sampleRepositoryMock; | ||||||
|
||||||
/** | ||||||
* @var DeleteHandler | ||||||
*/ | ||||||
private $deleteHandler; | ||||||
|
||||||
/** | ||||||
* @inheritDoc | ||||||
*/ | ||||||
protected function setUp() | ||||||
{ | ||||||
$this->entityMock = $this->createMock(Product::class); | ||||||
$this->sampleRepositoryMock = $this->getMockBuilder(SampleRepository::class) | ||||||
->disableOriginalConstructor() | ||||||
->setMethods(['getList', 'delete']) | ||||||
->getMockForAbstractClass(); | ||||||
|
||||||
$this->deleteHandler = (new ObjectManagerHelper($this))->getObject( | ||||||
DeleteHandler::class, | ||||||
['sampleRepository' => $this->sampleRepositoryMock] | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Test case when provided Product has type Downloadable. | ||||||
*/ | ||||||
public function testExecuteWithDownloadableProduct() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another advice is to use return type hinting if we declare
Suggested change
|
||||||
{ | ||||||
$this->entityMock->expects($this->once()) | ||||||
->method('getTypeId') | ||||||
->willReturn(Type::TYPE_DOWNLOADABLE); | ||||||
$this->entityMock->expects($this->once()) | ||||||
->method('getSku') | ||||||
->willReturn(self::STUB_PRODUCT_SKU); | ||||||
|
||||||
$sampleMock = $this->createMock(SampleInterface::class); | ||||||
$sampleMock->expects($this->once()) | ||||||
->method('getId') | ||||||
->willReturn(self::STUB_SAMPLE_ID); | ||||||
|
||||||
$this->sampleRepositoryMock->expects($this->once())->method('delete'); | ||||||
$this->sampleRepositoryMock->expects($this->once()) | ||||||
->method('getList') | ||||||
->willReturn([$sampleMock]); | ||||||
|
||||||
$this->assertSame($this->entityMock, $this->deleteHandler->execute($this->entityMock)); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Test case when provided Product is not Downloadable. | ||||||
*/ | ||||||
public function testExecuteWithOtherProduct() | ||||||
{ | ||||||
$this->entityMock->expects($this->once()) | ||||||
->method('getTypeId') | ||||||
->willReturn(self::STUB_PRODUCT_TYPE); | ||||||
|
||||||
$this->sampleRepositoryMock->expects($this->never())->method('getList'); | ||||||
$this->sampleRepositoryMock->expects($this->never())->method('delete'); | ||||||
$this->assertSame($this->entityMock, $this->deleteHandler->execute($this->entityMock)); | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a recommendation for the future to declare constant scope as
private
. It will reduce the chance of "temptation" to make another dependency on the current test in another test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the note, will keep it in mind!