-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OXDEV-8393 Add test, refactor (squash it)
- Loading branch information
1 parent
acb9ab4
commit 0ff9fb6
Showing
3 changed files
with
203 additions
and
41 deletions.
There are no files selected for viewing
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
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
136 changes: 136 additions & 0 deletions
136
tests/Integration/Application/Controller/OrderControllerTest.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,136 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\EshopCommunity\Tests\Integration\Application\Controller; | ||
|
||
use OxidEsales\Eshop\Application\Controller\OrderController; | ||
use OxidEsales\Eshop\Application\Model\Basket; | ||
use OxidEsales\Eshop\Core\Field; | ||
use OxidEsales\Eshop\Core\Registry; | ||
use OxidEsales\Eshop\Core\Session; | ||
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade; | ||
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface; | ||
use OxidEsales\EshopCommunity\Tests\ContainerTrait; | ||
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class OrderControllerTest extends IntegrationTestCase | ||
{ | ||
use ContainerTrait; | ||
|
||
private string $basketSummaryHashParameter = 'basketSummaryHash'; | ||
private string $userId; | ||
private LoggerInterface $logger; | ||
private Basket $basket; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->prepareUserStub(); | ||
$this->prepareBasketMock(); | ||
$this->stubSession(); | ||
unset($_SESSION['Errors']); | ||
} | ||
|
||
public function testExecuteWithBasketMissingSummaryHashParameterWillLogAnError(): void | ||
{ | ||
$logger = $this->createMock(LoggerInterface::class); | ||
$this->injectLoggerMockIntoContainer($logger); | ||
|
||
$logger->expects($this->once()) | ||
->method('warning') | ||
->with($this->stringContains($this->basketSummaryHashParameter)); | ||
|
||
oxNew(OrderController::class)->execute(); | ||
} | ||
|
||
public function testExecuteWithWrongBasketSummaryHashParameterAndEmptyBasketWillRedirectAndAddError(): void | ||
{ | ||
$_GET[$this->basketSummaryHashParameter] = 'some-invalid-hash'; | ||
$this->basket->method('getProductsCount')->willReturn(0); | ||
|
||
$redirect = oxNew(OrderController::class)->execute(); | ||
|
||
$this->assertEquals('basket', $redirect); | ||
$this->assertNotEmpty($_SESSION['Errors']); | ||
} | ||
|
||
public function testExecuteWithWrongBasketSummaryHashParameterAndNonEmptyBasketWillRedirectAndAddError(): void | ||
{ | ||
$_GET[$this->basketSummaryHashParameter] = 'some-invalid-hash'; | ||
$this->basket->method('getProductsCount')->willReturn(123); | ||
|
||
$redirect = oxNew(OrderController::class)->execute(); | ||
|
||
$this->assertEquals('order', $redirect); | ||
$this->assertNotEmpty($_SESSION['Errors']); | ||
} | ||
|
||
private function prepareUserStub(): void | ||
{ | ||
Registry::getConfig()->setConfigParam('blEnableIntangibleProdAgreement', false); | ||
$user = oxNew('oxUser'); | ||
$user->oxuser__oxusername = new Field('some-user-name', Field::T_RAW); | ||
$user->oxuser__oxpassword = new Field('some-user-pass', Field::T_RAW); | ||
$user->save(); | ||
|
||
$this->userId = $user->getId(); | ||
} | ||
|
||
private function stubSession(): void | ||
{ | ||
$session = $this->createPartialMock( | ||
Session::class, | ||
[ | ||
'checkSessionChallenge', | ||
'getVariable', | ||
'getBasket', | ||
] | ||
); | ||
$session->method('checkSessionChallenge') | ||
->willReturn(true); | ||
$session->method('getBasket') | ||
->willReturn($this->basket); | ||
$session->method('getVariable') | ||
->willReturnMap( | ||
[ | ||
['login-token', null], | ||
['usr', $this->userId], | ||
] | ||
); | ||
Registry::set(Session::class, $session); | ||
} | ||
|
||
private function prepareBasketMock(): void | ||
{ | ||
$this->basket = $this->createPartialMock( | ||
Basket::class, | ||
['getProductsCount'] | ||
); | ||
} | ||
|
||
private function injectLoggerMockIntoContainer(LoggerInterface $logger): void | ||
{ | ||
$this->createContainer(); | ||
$this->useNonVfsProjectConfigurationDirectory(); | ||
$this->container->set(LoggerInterface::class, $logger); | ||
$this->container->autowire(LoggerInterface::class, LoggerInterface::class); | ||
$this->attachContainerToContainerFactory(); | ||
} | ||
|
||
private function useNonVfsProjectConfigurationDirectory(): void | ||
{ | ||
$this->container->get(ContextInterface::class) | ||
->setProjectConfigurationDirectory( | ||
ContainerFacade::get(ContextInterface::class) | ||
->getProjectConfigurationDirectory() | ||
); | ||
} | ||
} |