1 | <?php |
||
28 | class AddressCollectorTest extends PHPUnit_Framework_TestCase { |
||
29 | |||
30 | private $mapper; |
||
31 | private $userId = 'testuser'; |
||
32 | private $logger; |
||
33 | private $collector; |
||
34 | |||
35 | protected function setUp() { |
||
36 | parent::setUp(); |
||
37 | |||
38 | $this->mapper = $this->getMockBuilder('\OCA\Mail\Db\CollectedAddressMapper') |
||
39 | ->disableOriginalConstructor() |
||
40 | ->getMock(); |
||
41 | $this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger') |
||
42 | ->disableOriginalConstructor() |
||
43 | ->getMock(); |
||
44 | |||
45 | $this->collector = new AddressCollector($this->mapper, $this->userId, |
||
46 | $this->logger); |
||
47 | } |
||
48 | |||
49 | public function testAddAddresses() { |
||
50 | $addresses = [ |
||
51 | '[email protected]', |
||
52 | '[email protected]', |
||
53 | ]; |
||
54 | $data = array_map(function($address) { |
||
55 | $ca = new CollectedAddress(); |
||
56 | $ca->setEmail($address); |
||
57 | $ca->setUserId($this->userId); |
||
58 | return $ca; |
||
59 | }, $addresses); |
||
60 | |||
61 | $this->mapper->expects($this->at(0)) |
||
62 | ->method('exists') |
||
63 | ->with($this->userId, $addresses[0]) |
||
64 | ->will($this->returnValue(false)); |
||
65 | $this->mapper->expects($this->at(1)) |
||
66 | ->method('insert') |
||
67 | ->with($data[0]); |
||
68 | $this->mapper->expects($this->at(2)) |
||
69 | ->method('exists') |
||
70 | ->with($this->userId, $addresses[1]) |
||
71 | ->will($this->returnValue(false)); |
||
72 | $this->mapper->expects($this->at(3)) |
||
73 | ->method('insert') |
||
74 | ->with($data[1]); |
||
75 | |||
76 | $this->collector->addAddresses($addresses); |
||
77 | } |
||
78 | |||
79 | public function testAddDuplicateAddresses() { |
||
80 | $addresses = [ |
||
81 | '[email protected]', |
||
82 | ]; |
||
83 | |||
84 | $this->mapper->expects($this->at(0)) |
||
85 | ->method('exists') |
||
86 | ->with($this->userId, $addresses[0]) |
||
87 | ->will($this->returnValue(true)); |
||
88 | $this->mapper->expects($this->never()) |
||
89 | ->method('insert'); |
||
90 | |||
91 | $this->collector->addAddresses($addresses); |
||
92 | } |
||
93 | |||
94 | public function testSearchAddress() { |
||
95 | $term = 'john'; |
||
96 | $mapperResult = ['some', 'data']; |
||
97 | |||
98 | $this->mapper->expects($this->once()) |
||
99 | ->method('findMatching') |
||
100 | ->with($this->userId, $term) |
||
101 | ->will($this->returnValue($mapperResult)); |
||
102 | |||
103 | $result = $this->collector->searchAddress($term); |
||
104 | |||
105 | $this->assertequals($mapperResult, $result); |
||
106 | } |
||
107 | |||
108 | } |
||
109 |