1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\ContactBundle\Tests\Unit\Formatter; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\LocaleBundle\Formatter\NameFormatter; |
6
|
|
|
|
7
|
|
|
use OroCRM\Bundle\ContactBundle\Entity\Contact; |
8
|
|
|
use OroCRM\Bundle\ContactBundle\Entity\ContactEmail; |
9
|
|
|
use OroCRM\Bundle\ContactBundle\Entity\ContactPhone; |
10
|
|
|
use OroCRM\Bundle\ContactBundle\Formatter\ContactNameFormatter; |
11
|
|
|
|
12
|
|
|
class ContactNameFormatterTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** @var NameFormatter */ |
15
|
|
|
protected $nameFormatter; |
16
|
|
|
|
17
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->nameFormatter = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Formatter\NameFormatter') |
20
|
|
|
->disableOriginalConstructor() |
21
|
|
|
->getMock(); |
22
|
|
|
|
23
|
|
|
$this->nameFormatter->expects($this->any()) |
24
|
|
|
->method('format') |
25
|
|
|
->will($this->returnCallback(function (Contact $contact) { |
26
|
|
|
return trim(implode(' ', [$contact->getFirstName(), $contact->getLastName()])); |
27
|
|
|
})); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @dataProvider formatDataProvider |
32
|
|
|
*/ |
33
|
|
|
public function testFormat(Contact $contact, $expectedResult) |
34
|
|
|
{ |
35
|
|
|
$contactNameFormatter = new ContactNameFormatter($this->nameFormatter); |
36
|
|
|
$this->assertEquals($expectedResult, $contactNameFormatter->format($contact)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function formatDataProvider() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
'contact with all contact info' => [ |
43
|
|
|
(new Contact()) |
44
|
|
|
->setFirstName('first') |
45
|
|
|
->setLastName('last') |
46
|
|
|
->addEmail((new ContactEmail('[email protected]'))->setPrimary(true)) |
|
|
|
|
47
|
|
|
->addPhone((new ContactPhone('542435'))->setPrimary(true)), |
|
|
|
|
48
|
|
|
'first last', |
49
|
|
|
], |
50
|
|
|
'contact with empty name' => [ |
51
|
|
|
(new Contact()) |
52
|
|
|
->addEmail((new ContactEmail('[email protected]'))->setPrimary(true)) |
|
|
|
|
53
|
|
|
->addPhone((new ContactPhone('542435'))->setPrimary(true)), |
|
|
|
|
54
|
|
|
'542435', |
55
|
|
|
], |
56
|
|
|
'contact with only phone' => [ |
57
|
|
|
(new Contact()) |
58
|
|
|
->addPhone((new ContactPhone('542435'))->setPrimary(true)), |
|
|
|
|
59
|
|
|
'542435', |
60
|
|
|
], |
61
|
|
|
'contact with only email' => [ |
62
|
|
|
(new Contact()) |
63
|
|
|
->addEmail((new ContactEmail('[email protected]'))->setPrimary(true)), |
|
|
|
|
64
|
|
|
'[email protected]', |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.