Passed
Pull Request — master (#52)
by Fabian
19:55
created

CustomerFixture   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 43.9%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 85
ccs 18
cts 41
cp 0.439
rs 10
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A logout() 0 8 2
A getId() 0 3 1
A rollback() 0 3 1
A getDefaultBillingAddressId() 0 3 1
A login() 0 8 2
A getDefaultShippingAddressId() 0 3 1
A getOtherAddressId() 0 3 1
A getEmail() 0 3 1
A __construct() 0 3 1
A getNonDefaultAddressIds() 0 6 1
A getAllAddressIds() 0 7 1
A getConfirmation() 0 3 1
1
<?php
2
3
namespace TddWizard\Fixtures\Customer;
4
5
use Magento\Customer\Api\Data\AddressInterface;
6
use Magento\Customer\Api\Data\CustomerInterface;
7
use Magento\Customer\Model\Session;
8
use Magento\TestFramework\Helper\Bootstrap;
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap 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...
9
10
/**
11
 * Object that can be returned from customer fixture, contains ids for test expectations
12
 */
13
class CustomerFixture
14
{
15
    /**
16
     * @var CustomerInterface
17
     */
18
    private $customer;
19
20 41
    public function __construct(CustomerInterface $customer)
21
    {
22 41
        $this->customer = $customer;
23 41
    }
24
25
    public function getDefaultShippingAddressId(): int
26
    {
27
        return $this->customer->getDefaultShipping();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getDefaultShipping() returns the type null|string which is incompatible with the type-hinted return integer.
Loading history...
28
    }
29
30
    public function getDefaultBillingAddressId(): int
31
    {
32
        return $this->customer->getDefaultBilling();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getDefaultBilling() returns the type null|string which is incompatible with the type-hinted return integer.
Loading history...
33
    }
34
35
    public function getOtherAddressId(): int
36
    {
37
        return $this->getNonDefaultAddressIds()[0];
38
    }
39
40
    public function getNonDefaultAddressIds(): array
41
    {
42
        return array_values(
43
            array_diff(
44
                $this->getAllAddressIds(),
45
                [$this->getDefaultBillingAddressId(), $this->getDefaultShippingAddressId()]
46
            )
47
        );
48
    }
49
50
    public function getAllAddressIds(): array
51
    {
52
        return array_map(
53
            function (AddressInterface $address) {
54
                return $address->getId();
55
            },
56
            $this->customer->getAddresses()
0 ignored issues
show
Bug introduced by
It seems like $this->customer->getAddresses() can also be of type null; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

56
            /** @scrutinizer ignore-type */ $this->customer->getAddresses()
Loading history...
57
        );
58
    }
59
60 40
    public function getId(): int
61
    {
62 40
        return $this->customer->getId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getId() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
63
    }
64
65
    public function getConfirmation(): string
66
    {
67
        return $this->customer->getConfirmation();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getConfirmation() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
70
    public function getEmail(): string
71
    {
72
        return $this->customer->getEmail();
73
    }
74
75 29
    public function login(Session $session = null): void
76
    {
77 29
        if ($session === null) {
78 29
            $objectManager = Bootstrap::getObjectManager();
79 29
            $objectManager->removeSharedInstance(Session::class);
80 29
            $session = $objectManager->get(Session::class);
81
        }
82 29
        $session->setCustomerId($this->getId());
83 29
    }
84
85 28
    public function logout(Session $session = null): void
86
    {
87 28
        if ($session === null) {
88 28
            $objectManager = Bootstrap::getObjectManager();
89 28
            $session = $objectManager->get(Session::class);
90
        }
91
92 28
        $session->logout();
93 28
    }
94
95
    public function rollback(): void
96
    {
97
        CustomerFixtureRollback::create()->execute($this);
98
    }
99
}
100