Completed
Pull Request — master (#105)
by
unknown
02:01
created

Iguana2GatewayFake   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 52
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDb() 0 3 1
A getDb() 0 3 1
A get() 0 5 2
A getNZX() 0 5 2
A getASX() 0 5 2
1
<?php
2
/**
3
 * The "Fake-Manager" instantiates fake objects, mainly to replace webservices with
4
 * faked implementations. It also provides shortcuts for creating complex fake datasets
5
 * through {@link FakeDatabase}.
6
 *
7
 * This class needs to be instantiated early in the application bootstrap process.
8
 * By default that's implemented through the {@link SampleTestSessionExtension}
9
 * and config.yml.
10
 *
11
 * The instantiation is conditional on there being a Behat test actually running
12
 * (by virtue of there being a TestSession instantiated) by a 'useFakeManager' flag
13
 * set through the "TestSession" module (@see {@link App\Test\Behaviour\FeatureContext->getTestSessionState()}).
14
 *
15
 * Since both the FeatureContext and actual application bootstrap share the same
16
 * {@link FakeDatabase} persisted on disk, they can share state.
17
 *
18
 * In terms of mock-data, at time of writing we're using the same sources as unit-test
19
 * mocks do.
20
 *
21
 * @author SilverStripe Iterators <[email protected]>
22
 * @author SilverStripe Science Ninjas <[email protected]>
23
 */
24
class SampleFakeManager {
25
26
    /**
27
     *
28
     * @var type
29
     */
30
    protected $db;
31
32
    /**
33
     *
34
     * @var type
35
     */
36
    protected $addressRightGateway;
37
38
    /**
39
     *
40
     * @param FakeDatabase $db
41
     */
42
    function __construct($db = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
        $testState = Injector::inst()->get('TestSessionEnvironment')->getState();
44
45
        if(!$db) {
46
            $db = new FakeDatabase($testState->fakeDatabasePath);
47
        }
48
49
        $this->addressRightGateway = new AddressRightGatewayFake();
50
        $this->addressRightGateway->setDb($db);
51
52
        /*
53
         * Registering a service is where the actual injection happens, depending on
54
         * whether a TestSession exists or not.
55
         */
56
        Injector::inst()->registerService(
57
            $this->addressRightGateway,
58
            'AddressRightGateway'
59
        );
60
61
        $this->iguna2Gateway = new Iguana2GatewayFake();
0 ignored issues
show
Bug introduced by
The property iguna2Gateway does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
        $this->iguna2Gateway->setDb($db);
63
64
        /*
65
         * Registering a service is where the actual injection happens, depending on
66
         * whether a TestSession exists or not.
67
         */
68
        Injector::inst()->registerService(
69
            $this->iguna2Gateway,
70
            'Iguana2Gateway'
71
        );
72
73
        $this->db = $db;
0 ignored issues
show
Documentation Bug introduced by
It seems like $db of type object<FakeDatabase> is incompatible with the declared type object<type> of property $db.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
    }
75
76
    public function getDb() {
77
        return $this->db;
78
    }
79
80
    // For use within specific Behat contexts
81
    public function getAddressRightGateway() {
82
        return $this->addressRightGateway;
83
    }
84
85
    // For use within specific Behat contexts
86
    public function getIguana2Gateway() {
87
        return $this->iguana2Gateway;
0 ignored issues
show
Bug introduced by
The property iguana2Gateway does not seem to exist. Did you mean iguna2Gateway?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
88
    }
89
90
}
91
92
class AddressRightGatewayFake extends AddressRightGateway {
93
94
    protected $db;
95
96
    public function __construct() {
97
        // Don't initialize client
98
    }
99
100
    public function setDb($db) {
101
        $this->db = $db;
102
    }
103
104
    public function getDb() {
105
        return $this->db;
106
    }
107
108
    /**
109
     * In more complex API's this function would be named for the remote method call
110
     * that is under test e.g. getAccountDataForMember() or something.
111
     *
112
     * @return string
113
     */
114
    public function get($endpoint = 'autocomplete')  {
115
        if($response = GatewayCommon::get_mock_behat_data('Addressright')) {
116
            return $response;
117
        }
118
    }
119
}
120
121
class Iguana2GatewayFake extends Iguana2Gateway {
122
123
    protected $db;
124
125
    public function __construct() {
126
        // Don't initialize client
127
    }
128
129
    public function setDb($db) {
130
        $this->db = $db;
131
    }
132
133
    public function getDb() {
134
        return $this->db;
135
    }
136
137
    /**
138
     * In more complex API's this function would be named for the remote method call
139
     * that is under test e.g. getAccountDataForMember() or something.
140
     *
141
     * AddressRight is a single URL and a single 'endpoint' so we've simply called it 'get'.
142
     *
143
     * @return string
144
     */
145
    public function get() {
146
        if($response = GatewayCommon::get_mock_behat_data('Iguana2')) {
147
            return $response;
148
        }
149
    }
150
151
    /**
152
     *
153
     * @see {@link $this->get()}
154
     * @return string
155
     */
156
    public function getNZX() {
157
        if($response = GatewayCommon::get_mock_behat_data('Iguana2', 'NZX')) {
158
            return $response;
159
        }
160
    }
161
162
    /**
163
     *
164
     * @see {@link $this->get()}
165
     * @return string
166
     */
167
    public function getASX() {
168
        if($response = GatewayCommon::get_mock_behat_data('Iguana2', 'ASX')) {
169
            return $response;
170
        }
171
    }
172
}
173