Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressTest.php
Go to the documentation of this file.
1 <?php
8 
9 class AddressTest extends \PHPUnit\Framework\TestCase
10 {
11  const ORIG_CUSTOMER_ID = 1;
12  const ORIG_PARENT_ID = 2;
13 
17  protected $objectManager;
18 
22  protected $address;
23 
27  protected $customer;
28 
32  protected $customerFactory;
33 
37  protected $resource;
38 
39  protected function setUp()
40  {
41  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
42 
43  $this->customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46  $this->customer->expects($this->any())
47  ->method('getId')
48  ->will($this->returnValue(self::ORIG_CUSTOMER_ID));
49  $this->customer->expects($this->any())
50  ->method('load')
51  ->will($this->returnSelf());
52 
53  $this->customerFactory = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
54  ->disableOriginalConstructor()
55  ->setMethods(['create'])
56  ->getMock();
57  $this->customerFactory->expects($this->any())
58  ->method('create')
59  ->will($this->returnValue($this->customer));
60 
61  $this->resource = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Address::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64 
65  $this->address = $this->objectManager->getObject(
66  \Magento\Customer\Model\Address::class,
67  [
68  'customerFactory' => $this->customerFactory,
69  'resource' => $this->resource,
70  ]
71  );
72  }
73 
74  public function testCustomerId()
75  {
76  $this->address->setParentId(self::ORIG_PARENT_ID);
77  $this->assertEquals(self::ORIG_PARENT_ID, $this->address->getCustomerId());
78 
79  $this->address->setCustomerId(self::ORIG_CUSTOMER_ID);
80  $this->assertEquals(self::ORIG_CUSTOMER_ID, $this->address->getCustomerId());
81  }
82 
83  public function testCustomer()
84  {
85  $this->address->unsetData('customer_id');
86  $this->assertFalse($this->address->getCustomer());
87 
88  $this->address->setCustomerId(self::ORIG_CUSTOMER_ID);
89 
90  $customer = $this->address->getCustomer();
91  $this->assertEquals(self::ORIG_CUSTOMER_ID, $customer->getId());
92 
94  $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
95  ->disableOriginalConstructor()
96  ->getMock();
97  $customer->expects($this->any())
98  ->method('getId')
99  ->will($this->returnValue(self::ORIG_CUSTOMER_ID + 1));
100 
101  $this->address->setCustomer($customer);
102  $this->assertEquals(self::ORIG_CUSTOMER_ID + 1, $this->address->getCustomerId());
103  }
104 
105  public function testGetAttributes()
106  {
107  $resultValue = 'test';
108 
109  $this->resource->expects($this->any())
110  ->method('loadAllAttributes')
111  ->will($this->returnSelf());
112  $this->resource->expects($this->any())
113  ->method('getSortedAttributes')
114  ->will($this->returnValue($resultValue));
115 
116  $this->assertEquals($resultValue, $this->address->getAttributes());
117  }
118 
119  public function testRegionId()
120  {
121  $this->address->setRegionId(1);
122  $this->assertEquals(1, $this->address->getRegionId());
123  }
124 
125  public function testGetEntityTypeId()
126  {
127  $mockEntityType = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130  $mockEntityType->expects($this->any())
131  ->method('getId')
132  ->will($this->returnValue(self::ORIG_CUSTOMER_ID));
133 
134  $this->resource->expects($this->any())
135  ->method('getEntityType')
136  ->will($this->returnValue($mockEntityType));
137 
138  $this->assertEquals(self::ORIG_CUSTOMER_ID, $this->address->getEntityTypeId());
139  }
140 }