Completed
Pull Request — 2.0 (#511)
by Roman
49:11 queued 46:11
created

OrderActionsFormTest::testValidation()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.8571
cc 3
eloc 19
nc 3
nop 0
1
<?php
2
3
class OrderActionsFormTest extends FunctionalTest
4
{
5
    protected static $fixture_file = array(
6
        'silvershop/tests/fixtures/Pages.yml',
7
        'silvershop/tests/fixtures/shop.yml',
8
    );
9
10
    protected $order;
11
    protected $checkoutPage;
12
13
    public function setUp()
14
    {
15
        parent::setUp();
16
        ShopTest::setConfiguration();
17
18
        // create order from fixture and persist to DB
19
        $this->order = $this->objFromFixture("Order", "unpaid");
20
        $this->order->write();
21
22
        OrderManipulation::add_session_order($this->order);
0 ignored issues
show
Documentation introduced by
$this->order is of type object<DataObject>|null, but the function expects a object<Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
23
24
        // create checkoug page from fixture and publish it
25
        $this->checkoutPage = $this->objFromFixture("CheckoutPage", "checkout");
26
        $this->checkoutPage->publish('Stage', 'Live');
27
28
        Config::inst()->update('Payment', 'allowed_gateways', array('Dummy'));
29
    }
30
31
    public function testOffsitePayment()
32
    {
33
        Config::inst()->update('GatewayInfo', 'Dummy', array('is_offsite' => true));
34
        $stubGateway = $this->buildPaymentGatewayStub(true, 'test-' . $this->order->ID, true);
35
        Injector::inst()->registerService($this->stubGatewayFactory($stubGateway), 'Omnipay\Common\GatewayFactory');
36
37
        $ctrl = ModelAsController::controller_for($this->checkoutPage);
0 ignored issues
show
Documentation introduced by
$this->checkoutPage is of type object<DataObject>|null, but the function expects a object<SiteTree>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
39
        $response = Director::test($ctrl->Link('ActionsForm'), array(
40
            'action_dopayment' => true,
41
            'OrderID'       => $this->order->ID,
42
            'PaymentMethod' => 'Dummy'
43
        ), $this->session());
44
45
        // There should be a new payment
46
        $this->assertEquals(1, $this->order->Payments()->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        // The status of the payment should be pending purchase, as there's a redirect to the offsite gateway
48
        $this->assertEquals('PendingPurchase', $this->order->Payments()->first()->Status);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        // The response we get from submitting the form should be a redirect to the offsite payment form
50
        $this->assertEquals('http://paymentprovider/test/offsiteform', $response->getHeader('Location'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
52
    }
53
54
    public function testOnsitePayment()
55
    {
56
        $stubGateway = $this->buildPaymentGatewayStub(true, 'test-' . $this->order->ID, false);
57
        Injector::inst()->registerService($this->stubGatewayFactory($stubGateway), 'Omnipay\Common\GatewayFactory');
58
59
        $ctrl = ModelAsController::controller_for($this->checkoutPage);
0 ignored issues
show
Documentation introduced by
$this->checkoutPage is of type object<DataObject>|null, but the function expects a object<SiteTree>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
61
        $response = Director::test($ctrl->Link('ActionsForm'), array(
62
            'action_dopayment' => true,
63
            'OrderID'       => $this->order->ID,
64
            'PaymentMethod' => 'Dummy',
65
            'type' => 'visa',
66
            'name' => 'Tester Mc. Testerson',
67
            'number' => '4242424242424242',
68
            'expiryMonth' => 10,
69
            'expiryYear' => date('Y') + 1,
70
            'cvv' => 123
71
        ), $this->session());
72
73
        // There should be a new payment
74
        $this->assertEquals(1, $this->order->Payments()->count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
        // The status of the payment should be Captured
76
        $this->assertEquals('Captured', $this->order->Payments()->first()->Status);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        // The response we get from submitting the form should be a redirect to the paid order
78
        $this->assertEquals($ctrl->Link('order/' . $this->order->ID), $response->getHeader('Location'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
    }
80
81
    public function testValidation()
82
    {
83
        $validator = new OrderActionsForm_Validator('PaymentMethod');
84
        $form = new OrderActionsForm(
85
            ModelAsController::controller_for($this->checkoutPage),
0 ignored issues
show
Documentation introduced by
$this->checkoutPage is of type object<DataObject>|null, but the function expects a object<SiteTree>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
            'ActionsForm',
87
            $this->order
0 ignored issues
show
Documentation introduced by
$this->order is of type object<DataObject>|null, but the function expects a object<Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
        );
89
        $validator->setForm($form);
90
        Form::set_current_action('dopayment');
91
        $validator->php(array(
92
            'OrderID'       => $this->order->ID,
93
            'PaymentMethod' => 'Dummy',
94
            'type' => 'visa',
95
            'name' => 'Tester Mc. Testerson',
96
            'number' => '4242424242424242'
97
        ));
98
99
        $requiredCount = 0;
100
        foreach ($validator->getErrors() as $error){
101
            if($error['messageType'] == 'required'){
102
                $requiredCount++;
103
            }
104
        }
105
        // 3 required fields missing
106
        $this->assertEquals(3, $requiredCount);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
    }
108
109
    protected function stubGatewayFactory($stubGateway)
110
    {
111
        $factory = $this->getMockBuilder('Omnipay\Common\GatewayFactory')->getMock();
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
        $factory->expects($this->any())->method('create')->will($this->returnValue($stubGateway));
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
        return $factory;
114
    }
115
116
    protected function buildPaymentGatewayStub(
117
        $successValue,
118
        $transactionReference,
119
        $isRedirect = true
120
    ) {
121
        //--------------------------------------------------------------------------------------------------------------
122
        // request and response
123
124
        $mockResponse = $this->getMockBuilder('Omnipay\Common\Message\AbstractResponse')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
            ->disableOriginalConstructor()->getMock();
126
127
        $mockResponse->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
            ->method('isSuccessful')->will($this->returnValue($successValue));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130
        $mockResponse->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            ->method('isRedirect')->will($this->returnValue($isRedirect));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
        $mockResponse->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
            ->method('getRedirectResponse')->will($this->returnValue(
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
                new \Symfony\Component\HttpFoundation\RedirectResponse('http://paymentprovider/test/offsiteform')
136
            ));
137
138
        $mockResponse->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
            ->method('getTransactionReference')->will($this->returnValue($transactionReference));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
141
        $mockRequest = $this->getMockBuilder('Omnipay\Common\Message\AbstractRequest')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
            ->disableOriginalConstructor()->getMock();
143
144
        $mockRequest->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
            ->method('send')->will($this->returnValue($mockResponse));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
147
        $mockRequest->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            ->method('getTransactionReference')->will($this->returnValue($transactionReference));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
150
151
        //--------------------------------------------------------------------------------------------------------------
152
        // Build the gateway
153
154
        $stubGateway = $this->getMockBuilder('Omnipay\Common\AbstractGateway')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
            ->setMethods(array('purchase', 'supportsCompletePurchase', 'getName'))
156
            ->getMock();
157
158
        $stubGateway->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
            ->method('purchase')
160
            ->will($this->returnValue($mockRequest));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
162
163
        $stubGateway->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
            ->method('supportsCompletePurchase')
165
            ->will($this->returnValue($isRedirect));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<OrderActionsFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
167
        return $stubGateway;
168
    }
169
}
170