Skip to content

Commit ece031d

Browse files
committed
Covering the model classes by Unit Tests
1 parent 06a0dd0 commit ece031d

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Csp\Test\Unit\Model;
10+
11+
use Magento\Csp\Model\Mode\ConfigManager;
12+
use Magento\Csp\Model\Mode\Data\ModeConfigured;
13+
use Magento\Framework\App\Area;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\App\State;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
17+
use Magento\Store\Model\Store;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
use RuntimeException;
21+
22+
/**
23+
* Class ConfigManagerTest
24+
*
25+
* Test for \Magento\Csp\Model\Mode\ConfigManager
26+
*/
27+
class ConfigManagerTest extends TestCase
28+
{
29+
const STUB_CRONTAB_AREA = 'crontab';
30+
31+
/**
32+
* @var ConfigManager
33+
*/
34+
private $model;
35+
36+
/**
37+
* @var ScopeConfigInterface|MockObject
38+
*/
39+
private $scopeConfigMock;
40+
41+
/**
42+
* @var Store|MockObject
43+
*/
44+
private $storeMock;
45+
46+
/**
47+
* @var State|MockObject
48+
*/
49+
private $stateMock;
50+
51+
/**
52+
* Set Up
53+
*/
54+
protected function setUp()
55+
{
56+
$objectManager = new ObjectManager($this);
57+
58+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
59+
$this->storeMock = $this->createMock(Store::class);
60+
$this->stateMock = $this->createMock(State::class);
61+
62+
$this->model = $objectManager->getObject(
63+
ConfigManager::class,
64+
[
65+
'config' => $this->scopeConfigMock,
66+
'storeModel' => $this->storeMock,
67+
'state' => $this->stateMock
68+
]
69+
);
70+
}
71+
72+
/**
73+
* Test throwing an exception for non storefront or admin areas
74+
*
75+
* @return void
76+
*
77+
* @expectedExceptionMessage CSP can only be configured for storefront or admin area
78+
* @expectedException RuntimeException
79+
*/
80+
public function testThrownExceptionForCrontabArea()
81+
{
82+
$this->stateMock->expects($this->any())
83+
->method('getAreaCode')
84+
->willReturn(static::STUB_CRONTAB_AREA);
85+
86+
$this->model->getConfigured();
87+
}
88+
89+
/**
90+
* Test returning the configured CSP for admin area
91+
*
92+
* @return void
93+
*/
94+
public function testConfiguredCSPForAdminArea()
95+
{
96+
$this->stateMock->expects($this->any())
97+
->method('getAreaCode')
98+
->willReturn(Area::AREA_ADMINHTML);
99+
$this->scopeConfigMock->expects($this->any())
100+
->method('isSetFlag')
101+
->willReturn(true);
102+
$this->scopeConfigMock->expects($this->any())
103+
->method('getValue')
104+
->willReturn('testReportUri');
105+
$result = $this->model->getConfigured();
106+
107+
$this->assertInstanceOf(ModeConfigured::class, $result);
108+
}
109+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Csp\Test\Unit\Model;
10+
11+
use Magento\Csp\Api\Data\PolicyInterface;
12+
use Magento\Csp\Model\Policy\Renderer\SimplePolicyHeaderRenderer;
13+
use Magento\Csp\Model\PolicyRendererPool;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
use RuntimeException;
18+
19+
/**
20+
* Class PolicyRendererPoolTest
21+
*
22+
* Test for \Magento\Csp\Model\PolicyRendererPool
23+
*/
24+
class PolicyRendererPoolTest extends TestCase
25+
{
26+
const STUB_POLICY_ID = 'header';
27+
28+
/**
29+
* @var PolicyRendererPool
30+
*/
31+
private $model;
32+
33+
/**
34+
* @var SimplePolicyHeaderRenderer|MockObject
35+
*/
36+
private $simplePolicyHeaderRendererMock;
37+
38+
/**
39+
* @var PolicyInterface|MockObject
40+
*/
41+
private $policyMock;
42+
43+
/**
44+
* Set Up
45+
*/
46+
protected function setUp()
47+
{
48+
$objectManager = new ObjectManager($this);
49+
$this->simplePolicyHeaderRendererMock = $this->createPartialMock(
50+
SimplePolicyHeaderRenderer::class,
51+
['canRender']
52+
);
53+
$this->policyMock = $this->createMock(PolicyInterface::class);
54+
55+
$this->model = $objectManager->getObject(
56+
PolicyRendererPool::class,
57+
[
58+
'renderers' => [
59+
$this->simplePolicyHeaderRendererMock
60+
]
61+
]
62+
);
63+
}
64+
65+
/**
66+
* Test throwing an exception for not found policy renders
67+
*
68+
* @return void
69+
*
70+
* @expectedExceptionMessage Failed to find a renderer for policy
71+
* @expectedException RuntimeException
72+
*/
73+
public function testThrownExceptionForNotFoundPolicyRenders()
74+
{
75+
$this->policyMock->expects($this->any())
76+
->method('getId')
77+
->willReturn(static::STUB_POLICY_ID);
78+
79+
$this->model->getRenderer($this->policyMock);
80+
}
81+
82+
/**
83+
* Test returning a renderer for the given policy
84+
*
85+
* @return void
86+
*/
87+
public function testReturningThePolicyRender()
88+
{
89+
$this->simplePolicyHeaderRendererMock->expects($this->any())
90+
->method('canRender')
91+
->with($this->policyMock)
92+
->willReturn(true);
93+
94+
$this->model->getRenderer($this->policyMock);
95+
}
96+
}

0 commit comments

Comments
 (0)