Completed
Pull Request — 2.0 (#461)
by Roman
27:38
created

ShopPaymentTest::setUpOnce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class ShopPaymentTest extends FunctionalTest
4
{
5
    protected static $fixture_file  = array(
6
        'silvershop/tests/fixtures/Pages.yml',
7
        'silvershop/tests/fixtures/shop.yml',
8
    );
9
    public static    $disable_theme = true;
10
11
    public function setUpOnce()
12
    {
13
        parent::setUpOnce();
14
        // clear session
15
        ShoppingCart::singleton()->clear();
16
    }
17
18
    public function setUp()
19
    {
20
        parent::setUp();
21
        ShopTest::setConfiguration();
22
23
        //set supported gateways
24
        Payment::config()->allowed_gateways = array(
25
            'Dummy', //onsite
26
            'Manual', //manual
27
            'PaymentExpress_PxPay', //offsite
28
            'PaymentExpress_PxPost' //onsite
29
        );
30
31
        PaymentService::set_http_client($this->getHttpClient());
32
        PaymentService::set_http_request($this->getHttpRequest());
33
34
        //publish products
35
        $this->objFromFixture("Product", "socks")->publish('Stage', 'Live');
36
        $this->objFromFixture("CheckoutPage", "checkout")->publish('Stage', 'Live');
37
        $this->objFromFixture("CartPage", "cart")->publish('Stage', 'Live');
38
    }
39
40
    public function testManualPayment()
41
    {
42
        $this->markTestIncomplete("Process a manual payment");
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<ShopPaymentTest>.

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...
43
    }
44
45
    public function testOnsitePayment()
46
    {
47
        $this->markTestIncomplete("Process an onsite payment");
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<ShopPaymentTest>.

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...
48
    }
49
50
    public function testOffsitePayment()
51
    {
52
        $this->markTestIncomplete("Process an off-site payment");
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<ShopPaymentTest>.

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...
53
    }
54
55
    public function testOffsitePaymentWithGatewayCallback()
56
    {
57
        //set up cart
58
        $cart = ShoppingCart::singleton()
59
            ->setCurrent($this->objFromFixture("Order", "cart"))
0 ignored issues
show
Documentation introduced by
$this->objFromFixture('Order', 'cart') 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...
60
            ->current();
61
        //collect checkout details
62
        $cart->update(
63
            array(
64
                'FirstName' => 'Foo',
65
                'Surname'   => 'Bar',
66
                'Email'     => '[email protected]',
67
            )
68
        );
69
        $cart->write();
70
        //pay for order with external gateway
71
        $processor = OrderProcessor::create($cart);
72
        $this->setMockHttpResponse('paymentexpress/tests/Mock/PxPayPurchaseSuccess.txt');
73
        $response = $processor->makePayment("PaymentExpress_PxPay", array());
74
        //gateway responds (in a different session)
75
        $oldsession = $this->mainSession;
76
        $this->mainSession = new TestSession();
77
        ShoppingCart::singleton()->clear();
78
        $this->setMockHttpResponse('paymentexpress/tests/Mock/PxPayCompletePurchaseSuccess.txt');
79
        $this->getHttpRequest()->query->replace(array('result' => 'abc123'));
80
        $identifier = $response->getPayment()->Identifier;
81
        $response = $this->get("paymentendpoint/$identifier/complete");
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
        //reload cart as new order
83
        $order = Order::get()->byId($cart->ID);
84
        $this->assertFalse($order->isCart(), "order is no longer in cart");
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<ShopPaymentTest>.

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...
85
        $this->assertTrue($order->isPaid(), "order is paid");
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ShopPaymentTest>.

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...
86
        //bring back client session
87
        $this->mainSession = $oldsession;
88
        $response = $this->get("paymentendpoint/$identifier/complete");
89
        $this->assertNull(Session::get("shoppingcartid"), "cart session id should be removed");
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<ShopPaymentTest>.

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...
90
        $this->assertNotEquals(404, $response->getStatusCode(), "We shouldn't get page not found");
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<ShopPaymentTest>.

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...
91
92
        $this->markTestIncomplete("Should assert other things");
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<ShopPaymentTest>.

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...
93
    }
94
95
    protected $payment;
96
    protected $httpClient;
97
    protected $httpRequest;
98
99
    protected function getHttpClient()
100
    {
101
        if (null === $this->httpClient) {
102
            $this->httpClient = new Guzzle\Http\Client;
103
        }
104
105
        return $this->httpClient;
106
    }
107
108
    public function getHttpRequest()
109
    {
110
        if (null === $this->httpRequest) {
111
            $this->httpRequest = new Symfony\Component\HttpFoundation\Request;
112
        }
113
114
        return $this->httpRequest;
115
    }
116
117
    protected function setMockHttpResponse($paths)
118
    {
119
        $testspath = BASE_PATH . '/vendor/omnipay';
120
        $mock = new Guzzle\Plugin\Mock\MockPlugin(null, true);
121
        $this->getHttpClient()->getEventDispatcher()->removeSubscriber($mock);
122
        foreach ((array)$paths as $path) {
123
            $mock->addResponse($testspath . '/' . $path);
124
        }
125
        $this->getHttpClient()->getEventDispatcher()->addSubscriber($mock);
126
127
        return $mock;
128
    }
129
}
130