Skip to content

Commit 4f24882

Browse files
committed
MAGETWO-39201: Refactoring and UnitTest Coverage \Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date
1 parent 163044a commit 4f24882

File tree

2 files changed

+182
-59
lines changed
  • app/code/Magento/Reports
    • Block/Adminhtml/Sales/Grid/Column/Renderer
    • Test/Unit/Block/Adminhtml/Sales/Grid/Column/Renderer

2 files changed

+182
-59
lines changed

app/code/Magento/Reports/Block/Adminhtml/Sales/Grid/Column/Renderer/Date.php

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,23 @@ protected function _getFormat()
3535
{
3636
$format = $this->getColumn()->getFormat();
3737
if (!$format) {
38-
if (self::$_format === null) {
39-
try {
40-
$formats = (new DataBundle())->get(
41-
$this->_localeResolver->getLocale()
42-
)['calendar']['gregorian']['availableFormats'];
43-
44-
switch ($this->getColumn()->getPeriodType()) {
45-
case 'month':
46-
self::$_format = $formats['yM'];
47-
break;
48-
49-
case 'year':
50-
self::$_format = $formats['y'];
51-
break;
52-
53-
default:
54-
self::$_format = $this->_localeDate->getDateFormat(
55-
\IntlDateFormatter::MEDIUM
56-
);
57-
break;
58-
}
59-
} catch (\Exception $e) {
38+
try {
39+
$dataBundle = new DataBundle();
40+
$resourceBundle = $dataBundle->get($this->_localeResolver->getLocale());
41+
$formats = $resourceBundle['calendar']['gregorian']['availableFormats'];
42+
switch ($this->getColumn()->getPeriodType()) {
43+
case 'month':
44+
$format = $formats['yM'];
45+
break;
46+
case 'year':
47+
$format = $formats['y'];
48+
break;
49+
default:
50+
$format = $this->_localeDate->getDateFormat(\IntlDateFormatter::MEDIUM);
51+
break;
6052
}
53+
} catch (\Exception $e) {
6154
}
62-
$format = self::$_format;
6355
}
6456
return $format;
6557
}
@@ -72,51 +64,23 @@ protected function _getFormat()
7264
*/
7365
public function render(\Magento\Framework\Object $row)
7466
{
75-
//@todo: check this logic manually
7667
if ($data = $row->getData($this->getColumn()->getIndex())) {
7768
switch ($this->getColumn()->getPeriodType()) {
7869
case 'month':
79-
$dateFormat = 'yyyy-MM';
70+
$data = $data . '-01';
8071
break;
8172
case 'year':
82-
$dateFormat = 'yyyy';
83-
break;
84-
default:
85-
$dateFormat = \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT;
73+
$data = $data . '-01-01';
8674
break;
8775
}
88-
8976
$format = $this->_getFormat();
90-
try {
91-
$data = $this->getColumn()->getGmtoffset()
92-
? \IntlDateFormatter::formatObject(
93-
$this->_localeDate->date(new \DateTime($data)),
94-
$format
95-
)
96-
: \IntlDateFormatter::formatObject(
97-
$this->_localeDate->date(
98-
new \DateTime($data),
99-
null,
100-
false
101-
),
102-
$format
103-
);
104-
} catch (\Exception $e) {
105-
$data = $this->getColumn()->getTimezone()
106-
? \IntlDateFormatter::formatObject(
107-
$this->_localeDate->date(new \DateTime($data)),
108-
$format
109-
)
110-
: \IntlDateFormatter::formatObject(
111-
$this->_localeDate->date(
112-
new \DateTime($data),
113-
null,
114-
false
115-
),
116-
$format
117-
);
77+
if ($this->getColumn()->getGmtoffset() || $this->getColumn()->getTimezone()) {
78+
$date = $this->_localeDate->date(new \DateTime($data));
79+
} else {
80+
$date = $this->_localeDate->date(new \DateTime($data), null, false);
11881
}
119-
return $data;
82+
$result = \IntlDateFormatter::formatObject($date, $format);
83+
return $result;
12084
}
12185
return $this->getColumn()->getDefault();
12286
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Reports\Test\Unit\Block\Adminhtml\Sales\Grid\Column\Renderer;
8+
9+
use Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date;
10+
11+
class DateTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date
15+
*/
16+
protected $date;
17+
18+
/**
19+
* @var \Magento\Backend\Block\Context|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $contextMock;
22+
23+
/**
24+
* @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $resolverMock;
27+
28+
/**
29+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $localeDate;
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
protected function setUp()
37+
{
38+
$this->localeDate = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime\TimezoneInterface')
39+
->disableOriginalConstructor()
40+
->getMock();
41+
$this->localeDate
42+
->expects($this->once())
43+
->method('date')
44+
->will($this->returnArgument(0));
45+
46+
$this->contextMock = $this->getMockBuilder('Magento\Backend\Block\Context')
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$this->contextMock
50+
->expects($this->once())
51+
->method('getLocaleDate')
52+
->will($this->returnValue($this->localeDate));
53+
54+
$this->resolverMock = $this->getMockBuilder('Magento\Framework\Locale\ResolverInterface')
55+
->getMock();
56+
57+
$this->date = new Date(
58+
$this->contextMock,
59+
$this->resolverMock
60+
);
61+
}
62+
63+
/**
64+
* @param string $data
65+
* @param string $index
66+
* @param string $locale
67+
* @param string $period
68+
* @param string $result
69+
* @dataProvider datesDataProvider
70+
* @return void
71+
*/
72+
public function testRender($data, $index, $locale, $period, $result)
73+
{
74+
$this->resolverMock->expects($this->any())->method('getLocale')->will($this->returnValue($locale));
75+
$this->localeDate->expects($this->any())->method('getDateFormat')->willReturnCallback(
76+
function ($value) use ($locale) {
77+
return (new \IntlDateFormatter(
78+
$locale,
79+
$value,
80+
\IntlDateFormatter::NONE
81+
))->getPattern();
82+
}
83+
);
84+
85+
$objectMock = $this->getMockBuilder('Magento\Framework\Object')
86+
->setMethods(['getData'])
87+
->getMock();
88+
$objectMock->expects($this->once())->method('getData')->will($this->returnValue($data));
89+
90+
$columnMock = $this->getMockBuilder('Magento\Backend\Block\Widget\Grid\Column')
91+
->disableOriginalConstructor()
92+
->setMethods(['getIndex', 'getPeriodType'])
93+
->getMock();
94+
$columnMock->expects($this->once())->method('getIndex')->will($this->returnValue($index));
95+
$columnMock->expects($this->atLeastOnce())->method('getPeriodType')->will($this->returnValue($period));
96+
97+
$this->date->setColumn($columnMock);
98+
99+
$this->assertEquals($result, $this->date->render($objectMock));
100+
}
101+
102+
/**
103+
* @return array
104+
*/
105+
public function datesDataProvider()
106+
{
107+
return [
108+
[
109+
'data' => '2000',
110+
'index' => 'period',
111+
'locale' => 'en_US',
112+
'period' => 'year',
113+
'result' => '2000'
114+
],
115+
[
116+
'data' => '2030',
117+
'index' => 'period',
118+
'locale' => 'en_US',
119+
'period' => 'year',
120+
'result' => '2030'
121+
],
122+
[
123+
'data' => '2000-01',
124+
'index' => 'period',
125+
'locale' => 'en_US',
126+
'period' => 'month',
127+
'result' => '1/2000'
128+
],
129+
[
130+
'data' => '2030-12',
131+
'index' => 'period',
132+
'locale' => 'en_US',
133+
'period' => 'month',
134+
'result' => '12/2030'
135+
],
136+
[
137+
'data' => '2016-12',
138+
'index' => 'period',
139+
'locale' => 'uk_UA',
140+
'period' => 'month',
141+
'result' => '12.2016'
142+
],
143+
[
144+
'data' => '2014-06-25',
145+
'index' => 'period',
146+
'locale' => 'en_US',
147+
'period' => 'day',
148+
'result' => 'Jun 25, 2014'
149+
],
150+
[
151+
'data' => '2014-06-25',
152+
'index' => 'period',
153+
'locale' => 'uk_UA',
154+
'period' => 'day',
155+
'result' => '25 Jun 2014'
156+
]
157+
];
158+
}
159+
}

0 commit comments

Comments
 (0)