-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathAbstractBlockTest.php
138 lines (123 loc) · 4.25 KB
/
AbstractBlockTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
namespace Magento\Framework\View\Test\Unit\Element;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Context;
use Magento\Framework\Config\View;
use Magento\Framework\View\ConfigInterface;
class AbstractBlockTest extends \PHPUnit_Framework_TestCase
{
/**
* @var AbstractBlock
*/
protected $block;
/**
* @return void
*/
protected function setUp()
{
$contextMock = $this->getMock(Context::class, [], [], '', false);
$this->block = $this->getMockForAbstractClass(
AbstractBlock::class,
['context' => $contextMock]
);
}
/**
* @param string $expectedResult
* @param string $nameInLayout
* @param array $methodArguments
* @dataProvider getUiIdDataProvider
*/
public function testGetUiId($expectedResult, $nameInLayout, $methodArguments)
{
$this->block->setNameInLayout($nameInLayout);
$this->assertEquals($expectedResult, call_user_func_array([$this->block, 'getUiId'], $methodArguments));
}
/**
* @return array
*/
public function getUiIdDataProvider()
{
return [
[' data-ui-id="" ', null, []],
[' data-ui-id="block" ', 'block', []],
[' data-ui-id="block" ', 'block---', []],
[' data-ui-id="block" ', '--block', []],
[' data-ui-id="bl-ock" ', '--bl--ock---', []],
[' data-ui-id="bl-ock" ', '--bL--Ock---', []],
[' data-ui-id="b-l-o-c-k" ', '--b!@#$%^&**()L--O;:...c<_>k---', []],
[
' data-ui-id="a0b1c2d3e4f5g6h7-i8-j9k0l1m2n-3o4p5q6r7-s8t9u0v1w2z3y4x5" ',
'a0b1c2d3e4f5g6h7',
['i8-j9k0l1m2n-3o4p5q6r7', 's8t9u0v1w2z3y4x5']
],
[
' data-ui-id="capsed-block-name-cap-ed-param1-caps2-but-ton" ',
'CaPSed BLOCK NAME',
['cAp$Ed PaRaM1', 'caPs2', 'bUT-TOn']
],
[
' data-ui-id="capsed-block-name-cap-ed-param1-caps2-but-ton-but-ton" ',
'CaPSed BLOCK NAME',
['cAp$Ed PaRaM1', 'caPs2', 'bUT-TOn', 'bUT-TOn']
],
[' data-ui-id="block-0-1-2-3-4" ', '!block!', range(0, 5)]
];
}
/**
* @return void
*/
public function testGetVar()
{
$config = $this->getMock(View::class, ['getVarValue'], [], '', false);
$module = uniqid();
$config->expects($this->any())
->method('getVarValue')
->willReturnMap([
['Magento_Theme', 'v1', 'one'],
[$module, 'v2', 'two']
]);
$configManager = $this->getMock(ConfigInterface::class, [], [], '', false);
$configManager->expects($this->exactly(2))->method('getViewConfig')->willReturn($config);
/** @var $block AbstractBlock|\PHPUnit_Framework_MockObject_MockObject */
$params = ['viewConfig' => $configManager];
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$block = $this->getMockForAbstractClass(
AbstractBlock::class,
$helper->getConstructArguments(AbstractBlock::class, $params)
);
$block->setData('module_name', 'Magento_Theme');
$this->assertEquals('one', $block->getVar('v1'));
$this->assertEquals('two', $block->getVar('v2', $module));
}
/**
* @return void
*/
public function testIsScopePrivate()
{
$this->assertFalse($this->block->isScopePrivate());
}
/**
* @return void
*/
public function testGetCacheKey()
{
$cacheKey = 'testKey';
$this->block->setData('cache_key', $cacheKey);
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
}
/**
* @return void
*/
public function testGetCacheKeyByName()
{
$nameInLayout = 'testBlock';
$this->block->setNameInLayout($nameInLayout);
$cacheKey = sha1($nameInLayout);
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
}
}