Skip to content

Commit 54b68fc

Browse files
committed
Fix unit tests
1 parent 4a359ac commit 54b68fc

21 files changed

Lines changed: 168 additions & 45 deletions

Api/FilterSettingsResolverInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
44
*
55
* @see PROJECT_LICENSE.txt
66
*/

Api/RequestFilterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
44
*
55
* @see PROJECT_LICENSE.txt
66
*/

Exception/NotAllowedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
44
*
55
* @see PROJECT_LICENSE.txt
66
*/

Model/Config.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
4+
*
5+
* @see PROJECT_LICENSE.txt
6+
*/
7+
8+
namespace N98\Guillotine\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
12+
/**
13+
* General wrapper for module configuration
14+
*/
15+
class Config implements ConfigInterface
16+
{
17+
const XML_PATH_THROW_EXCEPTION = 'n98_headless/guillotine/throw_exception';
18+
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
private ScopeConfigInterface $scopeConfig;
23+
24+
/**
25+
* Config constructor.
26+
*
27+
* @param ScopeConfigInterface $scopeConfig
28+
*/
29+
public function __construct(ScopeConfigInterface $scopeConfig)
30+
{
31+
$this->scopeConfig = $scopeConfig;
32+
}
33+
34+
/**
35+
* Return true if exception throwing is enabled
36+
*
37+
* @return bool
38+
*/
39+
public function shouldThrowException()
40+
{
41+
return $this->scopeConfig->isSetFlag(self::XML_PATH_THROW_EXCEPTION);
42+
}
43+
}

Model/ConfigInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
4+
*
5+
* @see PROJECT_LICENSE.txt
6+
*/
7+
8+
namespace N98\Guillotine\Model;
9+
10+
/**
11+
* General wrapper for module configuration
12+
*/
13+
interface ConfigInterface
14+
{
15+
/**
16+
* Return true if exception throwing is enabled
17+
*
18+
* @return bool
19+
*/
20+
public function shouldThrowException();
21+
}

Observer/FrontendBlacklistFilterObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
44
*
55
* @see PROJECT_LICENSE.txt
66
*/

PROJECT_LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2017 netz98 GmbH
3+
Copyright (c) netz98 GmbH
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

Service/FilterSettingsResolverService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
44
*
55
* @see PROJECT_LICENSE.txt
66
*/

Service/RequestFilterService.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,70 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
44
*
55
* @see PROJECT_LICENSE.txt
66
*/
77

88
namespace N98\Guillotine\Service;
99

10+
use Magento\Framework\App\ResponseInterface;
11+
use Magento\Framework\Controller\Result\Raw;
12+
use Magento\Framework\Controller\Result\RawFactory;
1013
use N98\Guillotine\Api\FilterSettingsResolverInterface;
1114
use N98\Guillotine\Api\RequestFilterInterface;
1215
use N98\Guillotine\Exception\NotAllowedException;
16+
use N98\Guillotine\Model\ConfigInterface;
1317

1418
/**
15-
* Filters a request path agains a whitelist
19+
* Filters a request path against a whitelist
1620
*/
1721
class RequestFilterService implements RequestFilterInterface
1822
{
1923
/**
20-
* @var \N98\Guillotine\Api\FilterSettingsResolverInterface
24+
* @var FilterSettingsResolverInterface
2125
*/
2226
private $filterSettingsResolver;
2327

28+
/**
29+
* @var ConfigInterface
30+
*/
31+
private $config;
32+
33+
/**
34+
* @var RawFactory
35+
*/
36+
private $rawResultFactory;
37+
38+
/**
39+
* @var ResponseInterface
40+
*/
41+
private ResponseInterface $response;
42+
2443
/**
2544
* RequestFilterService constructor.
2645
*
2746
* @param \N98\Guillotine\Api\FilterSettingsResolverInterface $filterSettingsResolver
47+
* @param \N98\Guillotine\Model\ConfigInterface $config
48+
* @param \Magento\Framework\App\ResponseInterface $response
49+
* @param \Magento\Framework\Controller\Result\RawFactory $rawResultFactory
2850
*/
29-
public function __construct(FilterSettingsResolverInterface $filterSettingsResolver)
30-
{
51+
public function __construct(
52+
FilterSettingsResolverInterface $filterSettingsResolver,
53+
ConfigInterface $config,
54+
ResponseInterface $response,
55+
RawFactory $rawResultFactory
56+
) {
3157
$this->filterSettingsResolver = $filterSettingsResolver;
58+
$this->config = $config;
59+
$this->rawResultFactory = $rawResultFactory;
60+
$this->response = $response;
3261
}
3362

3463
/**
3564
* @param string $requestPath
3665
*
3766
* @return void
38-
* @throws \N98\Guillotine\Exception\NotAllowedException
67+
* @throws NotAllowedException
3968
*/
4069
public function execute($requestPath)
4170
{
@@ -47,6 +76,14 @@ public function execute($requestPath)
4776
}
4877
}
4978

50-
throw NotAllowedException::notAllowed();
79+
if ($this->config->shouldThrowException()) {
80+
throw NotAllowedException::notAllowed();
81+
}
82+
83+
/** @var Raw $rawResult */
84+
$rawResult = $this->rawResultFactory->create();
85+
$rawResult->setContents(NotAllowedException::MSG_BLOCKED_DEFAULT);
86+
$rawResult->renderResult($this->response);
87+
$this->response->sendResponse();
5188
}
5289
}

Tests/Unit/Exception/NotAllowedExceptionTest.php renamed to Test/Unit/Exception/NotAllowedExceptionTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
3+
* @copyright Copyright (c) netz98 GmbH (https://www.netz98.de)
44
*
55
* @see PROJECT_LICENSE.txt
66
*/
77

8-
namespace N98\Guillotine\Tests\Unit\Exception;
8+
namespace N98\Guillotine\Test\Unit\Exception;
99

1010
use Magento\Framework\Phrase;
1111
use N98\Guillotine\Exception\NotAllowedException;
@@ -15,7 +15,7 @@
1515
*
1616
* @covers \N98\Guillotine\Exception\NotAllowedException
1717
*/
18-
class NotAllowedExceptionTest extends \PHPUnit_Framework_TestCase
18+
class NotAllowedExceptionTest extends \PHPUnit\Framework\TestCase
1919
{
2020
/**
2121
* @param string|null $msg
@@ -27,7 +27,6 @@ public function test($msg)
2727
try {
2828
throw NotAllowedException::notAllowed($msg);
2929
} catch (NotAllowedException $exception) {
30-
$this->assertAttributeInstanceOf(Phrase::class, 'phrase', $exception);
3130
$this->assertEquals($msg ?: NotAllowedException::MSG_BLOCKED_DEFAULT, $exception->getRawMessage());
3231
}
3332
}

0 commit comments

Comments
 (0)