ShopPaymentTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 74
c 1
b 0
f 0
dl 0
loc 155
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 32 1
A testManualPayment() 0 3 1
A getHttpRequest() 0 7 2
A testOffsitePaymentWithGatewayCallback() 0 38 1
A setMockHttpResponse() 0 15 3
A testOffsitePayment() 0 3 1
A testOnsitePayment() 0 3 1
A getHttpClient() 0 15 3
1
<?php
2
3
namespace SilverShop\Tests\Checkout;
4
5
use GuzzleHttp\Psr7\Message;
6
use SilverShop\Cart\ShoppingCart;
7
use SilverShop\Checkout\OrderProcessor;
8
use SilverShop\Model\Order;
9
use SilverShop\Page\CartPage;
10
use SilverShop\Page\CheckoutPage;
11
use SilverShop\Page\Product;
12
use SilverShop\Tests\ShopTest;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\Dev\FunctionalTest;
16
use SilverStripe\Dev\TestSession;
17
use SilverStripe\Omnipay\Model\Payment;
18
use SilverStripe\Omnipay\Tests\Service\TestGatewayFactory;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Omnipay\Tes...vice\TestGatewayFactory 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...
19
20
class ShopPaymentTest extends FunctionalTest
21
{
22
    protected static $fixture_file = [
23
        __DIR__ . '/../Fixtures/Pages.yml',
24
        __DIR__ . '/../Fixtures/shop.yml',
25
    ];
26
    public static $disable_theme = true;
27
    protected $autoFollowRedirection = false;
28
29
    /** @var \GuzzleHttp\Handler\MockHandler */
30
    protected $mockHandler = null;
31
32
    public function setUp(): void
33
    {
34
        parent::setUp();
35
        ShoppingCart::singleton()->clear();
36
        ShopTest::setConfiguration();
37
38
        //set supported gateways
39
        Config::modify()->set(
40
            Payment::class,
41
            'allowed_gateways',
42
            [
43
                'Dummy', //onsite
44
                'Manual', //manual
45
                'PaymentExpress_PxPay', //offsite
46
                'PaymentExpress_PxPost' //onsite
47
            ]
48
        )->set(
49
            Injector::class,
50
            'Omnipay\Common\GatewayFactory',
51
            [
52
                'class' => TestGatewayFactory::class
53
            ]
54
        );
55
56
        TestGatewayFactory::$httpClient = $this->getHttpClient();
57
        TestGatewayFactory::$httpRequest = $this->getHttpRequest();
58
59
        //publish products
60
        $this->logInWithPermission('ADMIN');
61
        $this->objFromFixture(Product::class, "socks")->publishSingle();
62
        $this->objFromFixture(CheckoutPage::class, "checkout")->publishSingle();
63
        $this->objFromFixture(CartPage::class, "cart")->publishSingle();
64
    }
65
66
    /**
67
     * @doesNotPerformAssertions
68
     */
69
    public function testManualPayment()
70
    {
71
        $this->markTestIncomplete("Process a manual payment");
72
    }
73
74
    /**
75
     * @doesNotPerformAssertions
76
     */
77
    public function testOnsitePayment()
78
    {
79
        $this->markTestIncomplete("Process an onsite payment");
80
    }
81
82
    /**
83
     * @doesNotPerformAssertions
84
     */
85
    public function testOffsitePayment()
86
    {
87
        $this->markTestIncomplete("Process an off-site payment");
88
    }
89
90
    public function testOffsitePaymentWithGatewayCallback()
91
    {
92
        //set up cart
93
        $cart = ShoppingCart::singleton()
94
            ->setCurrent($this->objFromFixture(Order::class, "cart"))
95
            ->current();
96
        //collect checkout details
97
        $cart->update(
98
            [
99
                'FirstName' => 'Foo',
100
                'Surname' => 'Bar',
101
                'Email' => '[email protected]',
102
            ]
103
        );
104
        $cart->write();
105
        //pay for order with external gateway
106
        $processor = OrderProcessor::create($cart);
107
        $this->setMockHttpResponse('paymentexpress/tests/Mock/PxPayPurchaseSuccess.txt');
108
        $response = $processor->makePayment("PaymentExpress_PxPay", []);
109
        //gateway responds (in a different session)
110
        $oldsession = $this->mainSession;
111
        $this->mainSession = new TestSession();
112
        ShoppingCart::singleton()->clear();
113
        $this->setMockHttpResponse('paymentexpress/tests/Mock/PxPayCompletePurchaseSuccess.txt');
114
        $this->getHttpRequest()->query->replace(['result' => 'abc123']);
115
        $identifier = $response->getPayment()->Identifier;
116
117
        //bring back client session
118
        $this->mainSession = $oldsession;
119
        // complete the order
120
        $response = $this->get("paymentendpoint/$identifier/complete", $oldsession->session());
121
122
        //reload cart as new order
123
        $order = Order::get()->byId($cart->ID);
124
        $this->assertFalse($order->isCart(), "order is no longer in cart");
125
        $this->assertTrue($order->isPaid(), "order is paid");
126
        $this->assertNull($this->mainSession->session()->get("shoppingcartid"), "cart session id should be removed");
127
        $this->assertNotEquals(404, $response->getStatusCode(), "We shouldn't get page not found");
128
    }
129
130
    protected $payment;
131
    protected $httpClient;
132
    protected $httpRequest;
133
134
    protected function getHttpClient()
135
    {
136
        if (null === $this->httpClient) {
137
            if ($this->mockHandler === null) {
138
                $this->mockHandler = new \GuzzleHttp\Handler\MockHandler();
139
            }
140
141
            $guzzle = new \GuzzleHttp\Client([
142
                'handler' => $this->mockHandler,
143
            ]);
144
145
            $this->httpClient = new \Omnipay\Common\Http\Client(new \Http\Adapter\Guzzle7\Client($guzzle));
146
        }
147
148
        return $this->httpClient;
149
    }
150
151
    protected function getHttpRequest()
152
    {
153
        if (null === $this->httpRequest) {
154
            $this->httpRequest = new \Symfony\Component\HttpFoundation\Request;
155
        }
156
157
        return $this->httpRequest;
158
    }
159
160
    protected function setMockHttpResponse($paths)
161
    {
162
        if ($this->mockHandler === null) {
163
            throw new \Exception('HTTP client not initialised before adding mock response.');
164
        }
165
166
        $testspath = BASE_PATH . '/vendor/omnipay';
167
168
        foreach ((array)$paths as $path) {
169
            $this->mockHandler->append(
170
                Message::parseResponse(file_get_contents("{$testspath}/{$path}"))
171
            );
172
        }
173
174
        return $this->mockHandler;
175
    }
176
}
177