Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerRegistryTest.php
Go to the documentation of this file.
1 <?php
8 
10 
15 class CustomerRegistryTest extends \PHPUnit\Framework\TestCase
16 {
20  private $customerRegistry;
21 
25  private $customerFactory;
26 
30  private $customer;
31 
35  const CUSTOMER_ID = 1;
37  const WEBSITE_ID = 1;
38 
39  protected function setUp()
40  {
41  $this->customerFactory = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
42  ->setMethods(['create'])
43  ->disableOriginalConstructor()
44  ->getMock();
45  $objectManager = new ObjectManager($this);
46  $this->customerRegistry = $objectManager->getObject(
47  \Magento\Customer\Model\CustomerRegistry::class,
48  ['customerFactory' => $this->customerFactory]
49  );
50  $this->customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
51  ->disableOriginalConstructor()
52  ->setMethods(
53  [
54  'load',
55  'getId',
56  'getEmail',
57  'getWebsiteId',
58  '__wakeup',
59  'setEmail',
60  'setWebsiteId',
61  'loadByEmail',
62  ]
63  )
64  ->getMock();
65  }
66 
67  public function testRetrieve()
68  {
69  $this->customer->expects($this->once())
70  ->method('load')
71  ->with(self::CUSTOMER_ID)
72  ->will($this->returnValue($this->customer));
73  $this->customer->expects($this->any())
74  ->method('getId')
75  ->will($this->returnValue(self::CUSTOMER_ID));
76  $this->customerFactory->expects($this->once())
77  ->method('create')
78  ->will($this->returnValue($this->customer));
79  $actual = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
80  $this->assertEquals($this->customer, $actual);
81  $actualCached = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
82  $this->assertEquals($this->customer, $actualCached);
83  }
84 
85  public function testRetrieveByEmail()
86  {
87  $this->customer->expects($this->once())
88  ->method('loadByEmail')
89  ->with(self::CUSTOMER_EMAIL)
90  ->will($this->returnValue($this->customer));
91  $this->customer->expects($this->any())
92  ->method('getId')
93  ->will($this->returnValue(self::CUSTOMER_ID));
94  $this->customer->expects($this->any())
95  ->method('getEmail')
96  ->will($this->returnValue(self::CUSTOMER_EMAIL));
97  $this->customer->expects($this->any())
98  ->method('getWebsiteId')
99  ->will($this->returnValue(self::WEBSITE_ID));
100  $this->customer->expects($this->any())
101  ->method('setEmail')
102  ->will($this->returnValue($this->customer));
103  $this->customer->expects($this->any())
104  ->method('setWebsiteId')
105  ->will($this->returnValue($this->customer));
106  $this->customerFactory->expects($this->once())
107  ->method('create')
108  ->will($this->returnValue($this->customer));
109  $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
110  $this->assertEquals($this->customer, $actual);
111  $actualCached = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
112  $this->assertEquals($this->customer, $actualCached);
113  }
114 
118  public function testRetrieveException()
119  {
120  $this->customer->expects($this->once())
121  ->method('load')
122  ->with(self::CUSTOMER_ID)
123  ->will($this->returnValue($this->customer));
124  $this->customer->expects($this->any())
125  ->method('getId')
126  ->will($this->returnValue(null));
127  $this->customerFactory->expects($this->once())
128  ->method('create')
129  ->will($this->returnValue($this->customer));
130  $this->customerRegistry->retrieve(self::CUSTOMER_ID);
131  }
132 
137  {
138  $this->customer->expects($this->once())
139  ->method('loadByEmail')
140  ->with(self::CUSTOMER_EMAIL)
141  ->will($this->returnValue($this->customer));
142  $this->customer->expects($this->any())
143  ->method('getEmail')
144  ->will($this->returnValue(null));
145  $this->customer->expects($this->any())
146  ->method('getWebsiteId')
147  ->will($this->returnValue(null));
148  $this->customer->expects($this->any())
149  ->method('setEmail')
150  ->will($this->returnValue($this->customer));
151  $this->customer->expects($this->any())
152  ->method('setWebsiteId')
153  ->will($this->returnValue($this->customer));
154  $this->customerFactory->expects($this->once())
155  ->method('create')
156  ->will($this->returnValue($this->customer));
157  $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
158  }
159 
160  public function testRemove()
161  {
162  $this->customer->expects($this->exactly(2))
163  ->method('load')
164  ->with(self::CUSTOMER_ID)
165  ->will($this->returnValue($this->customer));
166  $this->customer->expects($this->any())
167  ->method('getId')
168  ->will($this->returnValue(self::CUSTOMER_ID));
169  $this->customerFactory->expects($this->exactly(2))
170  ->method('create')
171  ->will($this->returnValue($this->customer));
172  $actual = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
173  $this->assertEquals($this->customer, $actual);
174  $this->customerRegistry->remove(self::CUSTOMER_ID);
175  $actual = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
176  $this->assertEquals($this->customer, $actual);
177  }
178 
179  public function testRemoveByEmail()
180  {
181  $this->customer->expects($this->exactly(2))
182  ->method('loadByEmail')
183  ->with(self::CUSTOMER_EMAIL)
184  ->will($this->returnValue($this->customer));
185  $this->customer->expects($this->any())
186  ->method('getId')
187  ->will($this->returnValue(self::CUSTOMER_ID));
188  $this->customer->expects($this->any())
189  ->method('getEmail')
190  ->will($this->returnValue(self::CUSTOMER_EMAIL));
191  $this->customer->expects($this->any())
192  ->method('getWebsiteId')
193  ->will($this->returnValue(self::WEBSITE_ID));
194  $this->customer->expects($this->any())
195  ->method('setEmail')
196  ->will($this->returnValue($this->customer));
197  $this->customer->expects($this->any())
198  ->method('setWebsiteId')
199  ->will($this->returnValue($this->customer));
200  $this->customerFactory->expects($this->exactly(2))
201  ->method('create')
202  ->will($this->returnValue($this->customer));
203  $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
204  $this->assertEquals($this->customer, $actual);
205  $this->customerRegistry->removeByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
206  $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
207  $this->assertEquals($this->customer, $actual);
208  }
209 }
$objectManager
Definition: bootstrap.php:17