testSetAddressWithInabilityByValidationToSaveQuote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 29
rs 9.584
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Hryvinskyi\QuoteAddressValidator\Test\Unit\Model;
4
5
use Hryvinskyi\QuoteAddressValidator\Model\BillingAddressManagement;
6
use Magento\Customer\Api\AddressRepositoryInterface;
7
use Magento\Framework\Exception\LocalizedException;
8
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9
use Magento\Quote\Api\CartRepositoryInterface;
10
use Magento\Quote\Model\Quote;
11
use Magento\Quote\Model\Quote\Address;
12
use Magento\Quote\Model\QuoteAddressValidator;
13
use Magento\Quote\Model\ShippingAddressAssignment;
14
use PHPUnit\Framework\MockObject\MockObject;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\MockObject\MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
 */
21
class BillingAddressManagementTest extends TestCase
22
{
23
    /**
24
     * @var ObjectManager
25
     */
26
    protected $objectManager;
27
28
    /**
29
     * @var BillingAddressManagement
30
     */
31
    protected $model;
32
33
    /**
34
     * @var MockObject
35
     */
36
    protected $quoteRepositoryMock;
37
38
    /**
39
     * @var MockObject
40
     */
41
    protected $validatorMock;
42
43
    /**
44
     * @var MockObject
45
     */
46
    protected $addressRepository;
47
48
    /**
49
     * @var MockObject
50
     */
51
    protected $shippingAssignmentMock;
52
53
    /**
54
     * @return void
55
     */
56
    protected function setUp(): void
57
    {
58
        $this->objectManager = new ObjectManager($this);
0 ignored issues
show
Deprecated Code introduced by
The class Magento\Framework\TestFr...it\Helper\ObjectManager has been deprecated: Class under test should be instantiated with `new` keyword with explicit dependencies declaration ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
        $this->objectManager = /** @scrutinizer ignore-deprecated */ new ObjectManager($this);
Loading history...
59
        $this->quoteRepositoryMock = $this->getMockForAbstractClass(CartRepositoryInterface::class);
60
        $this->validatorMock = $this->createMock(QuoteAddressValidator::class);
61
        $this->addressRepository = $this->getMockForAbstractClass(AddressRepositoryInterface::class);
62
        $logger = $this->getMockForAbstractClass(LoggerInterface::class);
63
        $this->model = $this->objectManager->getObject(
64
            BillingAddressManagement::class,
65
            [
66
                'quoteRepository' => $this->quoteRepositoryMock,
67
                'addressValidator' => $this->validatorMock,
68
                'logger' => $logger,
69
                'addressRepository' => $this->addressRepository
70
            ]
71
        );
72
73
        $this->shippingAssignmentMock = $this->createPartialMock(
74
            ShippingAddressAssignment::class,
75
            ['setAddress']
76
        );
77
        $this->objectManager->setBackwardCompatibleProperty(
78
            $this->model,
79
            'shippingAddressAssignment',
80
            $this->shippingAssignmentMock
81
        );
82
    }
83
84
    /**
85
     * @return void
86
     */
87
    public function testGetAddress()
88
    {
89
        $quoteMock = $this->createMock(Quote::class);
90
        $this->quoteRepositoryMock->expects($this->once())->method('getActive')
91
            ->with('cartId')->willReturn($quoteMock);
92
93
        $addressMock = $this->createMock(Address::class);
94
        $quoteMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock);
95
96
        $this->assertEquals($addressMock, $this->model->get('cartId'));
97
    }
98
99
    /**
100
     * @return void
101
     */
102
    public function testSetAddress()
103
    {
104
        $cartId = 100;
105
        $useForShipping = true;
106
        $addressId = 1;
107
108
        $address = $this->createPartialMock(Address::class, ['getId']);
109
        $quoteMock = $this->createPartialMock(
110
            Quote::class,
111
            ['removeAddress', 'getBillingAddress', 'setBillingAddress', 'setDataChanges']
112
        );
113
114
        $this->quoteRepositoryMock->expects($this->once())
115
            ->method('getActive')
116
            ->with($cartId)
117
            ->willReturn($quoteMock);
118
119
        $address->expects($this->exactly(2))->method('getId')->willReturn($addressId);
120
        $quoteMock->expects($this->exactly(2))->method('getBillingAddress')->willReturn($address);
121
        $quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
122
        $quoteMock->expects($this->once())->method('setBillingAddress')->with($address)->willReturnSelf();
123
        $quoteMock->expects($this->once())->method('setDataChanges')->with(1)->willReturnSelf();
124
125
        $this->shippingAssignmentMock->expects($this->once())
126
            ->method('setAddress')
127
            ->with($quoteMock, $address, $useForShipping);
128
129
        $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
130
        $this->assertEquals($addressId, $this->model->assign($cartId, $address, $useForShipping));
131
    }
132
133
    /**
134
     * @return void
135
     */
136
    public function testSetAddressWithInabilityToSaveQuote()
137
    {
138
        $this->expectException('Magento\Framework\Exception\InputException');
139
        $this->expectExceptionMessage('The address failed to save. Verify the address and try again.');
140
        $cartId = 100;
141
        $addressId = 1;
142
143
        $address = $this->createPartialMock(Address::class, ['getId']);
144
        $quoteMock = $this->createPartialMock(
145
            Quote::class,
146
            ['removeAddress', 'getBillingAddress', 'setBillingAddress', 'setDataChanges']
147
        );
148
149
        $this->quoteRepositoryMock->expects($this->once())
150
            ->method('getActive')
151
            ->with($cartId)
152
            ->willReturn($quoteMock);
153
154
        $address->expects($this->once())->method('getId')->willReturn($addressId);
155
        $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($address);
156
        $quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
157
        $quoteMock->expects($this->once())->method('setBillingAddress')->with($address)->willReturnSelf();
158
        $quoteMock->expects($this->once())->method('setDataChanges')->with(1)->willReturnSelf();
159
160
        $this->shippingAssignmentMock->expects($this->once())
161
            ->method('setAddress')
162
            ->with($quoteMock, $address, false);
163
164
        $this->quoteRepositoryMock->expects($this->once())
165
            ->method('save')
166
            ->with($quoteMock)
167
            ->willThrowException(
168
                new \Exception('Some DB Error')
169
            );
170
        $this->model->assign($cartId, $address);
171
    }
172
173
    /**
174
     * @return void
175
     */
176
    public function testSetAddressWithInabilityByValidationToSaveQuote()
177
    {
178
        $this->expectException(LocalizedException::class);
179
        $this->expectExceptionMessage('The billing information was unable to be saved. Error: "Some validation error"');
180
        $cartId = 100;
181
        $addressId = 1;
182
183
        $address = $this->createPartialMock(Address::class, ['getId']);
184
        $quoteMock = $this->createPartialMock(
185
            Quote::class,
186
            ['removeAddress', 'getBillingAddress', 'setBillingAddress', 'setDataChanges']
187
        );
188
189
        $this->quoteRepositoryMock->expects($this->once())
190
            ->method('getActive')
191
            ->with($cartId)
192
            ->willReturn($quoteMock);
193
194
        $address->expects($this->once())->method('getId')->willReturn($addressId);
195
        $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($address);
196
        $quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
197
        $quoteMock->expects($this->once())->method('setBillingAddress')->with($address)->willReturnSelf();
198
199
        $this->shippingAssignmentMock->expects($this->once())
200
            ->method('setAddress')
201
            ->with($quoteMock, $address, false)
202
            ->willThrowException(new LocalizedException(__('Some validation error')));
203
204
        $this->model->assign($cartId, $address);
205
    }
206
}
207