1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\ContactBundle\Tests\Unit\Provider; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\EntityBundle\Provider\EntityNameProviderInterface; |
6
|
|
|
use Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink; |
7
|
|
|
use Oro\Bundle\LocaleBundle\DQL\DQLNameFormatter; |
8
|
|
|
|
9
|
|
|
use OroCRM\Bundle\ContactBundle\Entity\Contact; |
10
|
|
|
use OroCRM\Bundle\ContactBundle\Formatter\ContactNameFormatter; |
11
|
|
|
use OroCRM\Bundle\ContactBundle\Provider\ContactEntityNameProvider; |
12
|
|
|
|
13
|
|
|
class ContactEntityNameProviderTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var ContactEntityNameProvider */ |
16
|
|
|
protected $provider; |
17
|
|
|
|
18
|
|
|
/** @var ServiceLink */ |
19
|
|
|
protected $nameFormatterLink; |
20
|
|
|
|
21
|
|
|
/** @var ServiceLink */ |
22
|
|
|
protected $dqlNameFormatterLink; |
23
|
|
|
|
24
|
|
|
/** @var ContactNameFormatter */ |
25
|
|
|
protected $contactNameFormatter; |
26
|
|
|
|
27
|
|
|
/** @var DQLNameFormatter */ |
28
|
|
|
protected $dqlNameFormatter; |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->contactNameFormatter = $this |
33
|
|
|
->getMockBuilder('OroCRM\Bundle\ContactBundle\Formatter\ContactNameFormatter') |
34
|
|
|
->disableOriginalConstructor()->getMock(); |
35
|
|
|
|
36
|
|
|
$this->dqlNameFormatter = $this |
37
|
|
|
->getMockBuilder('Oro\Bundle\LocaleBundle\DQL\DQLNameFormatter') |
38
|
|
|
->disableOriginalConstructor()->getMock(); |
39
|
|
|
|
40
|
|
|
$this->nameFormatterLink = $this |
41
|
|
|
->getMockBuilder('Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink') |
42
|
|
|
->disableOriginalConstructor()->getMock(); |
43
|
|
|
|
44
|
|
|
$this->dqlNameFormatterLink = $this |
45
|
|
|
->getMockBuilder('Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink') |
46
|
|
|
->disableOriginalConstructor()->getMock(); |
47
|
|
|
|
48
|
|
|
$this->provider = new ContactEntityNameProvider($this->nameFormatterLink, $this->dqlNameFormatterLink); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider getNameDataProvider |
53
|
|
|
* |
54
|
|
|
* @param $format |
55
|
|
|
* @param $locale |
56
|
|
|
* @param $entity |
57
|
|
|
* @param $expected |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testGetName($format, $locale, $entity, $expected) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if ($expected) { |
62
|
|
|
$this->contactNameFormatter->expects($this->once())->method('format') |
|
|
|
|
63
|
|
|
->willReturn('Contact'); |
64
|
|
|
|
65
|
|
|
$this->nameFormatterLink->expects($this->once()) |
|
|
|
|
66
|
|
|
->method('getService') |
67
|
|
|
->willReturn($this->contactNameFormatter); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$result = $this->provider->getName($format, $locale, $entity); |
71
|
|
|
$this->assertEquals($expected, $result); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @dataProvider getNameDQLDataProvider |
76
|
|
|
* |
77
|
|
|
* @param $format |
78
|
|
|
* @param $locale |
79
|
|
|
* @param $className |
80
|
|
|
* @param $alias |
81
|
|
|
* @param $expected |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function testGetNameDQL($format, $locale, $className, $alias, $expected) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
if ($expected) { |
86
|
|
|
$this->dqlNameFormatter->expects($this->once())->method('getFormattedNameDQL') |
|
|
|
|
87
|
|
|
->willReturn('Contact'); |
88
|
|
|
|
89
|
|
|
$this->dqlNameFormatterLink->expects($this->once()) |
|
|
|
|
90
|
|
|
->method('getService') |
91
|
|
|
->willReturn($this->dqlNameFormatter); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$result = $this->provider->getNameDQL($format, $locale, $className, $alias); |
95
|
|
|
$this->assertEquals($expected, $result); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function getNameDataProvider() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
'test unsupported class' => [ |
105
|
|
|
'format' => '', |
106
|
|
|
'locale' => null, |
107
|
|
|
'entity' => new \stdClass(), |
108
|
|
|
'expected' => false |
109
|
|
|
], |
110
|
|
|
'test unsupported format' => [ |
111
|
|
|
'format' => '', |
112
|
|
|
'locale' => null, |
113
|
|
|
'entity' => $this->getEntity(), |
114
|
|
|
'expected' => false |
115
|
|
|
], |
116
|
|
|
'correct data' => [ |
117
|
|
|
'format' => EntityNameProviderInterface::FULL, |
118
|
|
|
'locale' => '', |
119
|
|
|
'entity' => $this->getEntity(), |
120
|
|
|
'expected' => 'Contact' |
121
|
|
|
] |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function getNameDQLDataProvider() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
'test unsupported class Name' => [ |
132
|
|
|
'format' => '', |
133
|
|
|
'locale' => null, |
134
|
|
|
'className' => '', |
135
|
|
|
'alias' => '', |
136
|
|
|
'expected' => false |
137
|
|
|
], |
138
|
|
|
'test unsupported format' => [ |
139
|
|
|
'format' => '', |
140
|
|
|
'locale' => null, |
141
|
|
|
'className' => ContactEntityNameProvider::CLASS_NAME, |
142
|
|
|
'alias' => '', |
143
|
|
|
'expected' => false |
144
|
|
|
], |
145
|
|
|
'correct data' => [ |
146
|
|
|
'format' => EntityNameProviderInterface::FULL, |
147
|
|
|
'locale' => null, |
148
|
|
|
'className' => ContactEntityNameProvider::CLASS_NAME, |
149
|
|
|
'alias' => '', |
150
|
|
|
'expected' => 'Contact' |
151
|
|
|
] |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return Contact |
157
|
|
|
*/ |
158
|
|
|
protected function getEntity() |
159
|
|
|
{ |
160
|
|
|
return new Contact(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.