Passed
Pull Request — master (#58)
by Nic
02:14
created

OrderFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 113
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTransactionData() 0 3 1
A getOrderDetails() 0 5 1
A testGetOrder() 0 16 1
A getOptions() 0 3 1
A setUp() 0 5 1
A getXML() 0 12 1
1
<?php
2
3
namespace Dynamic\Foxy\Orders\Tests\Factory;
4
5
use Dynamic\Foxy\Extension\Purchasable;
6
use Dynamic\Foxy\Model\FoxyHelper;
7
use Dynamic\Foxy\Model\Variation;
8
use Dynamic\Foxy\Orders\Model\Order;
9
use Dynamic\Foxy\Orders\Tests\TestOnly\Extension\TestVariationDataExtension;
10
use Dynamic\Foxy\Orders\Tests\TestOnly\Page\TestProduct;
11
use Dynamic\Foxy\Parser\Controller\FoxyController;
12
use SilverStripe\Control\Controller;
13
use SilverStripe\Control\Director;
14
use SilverStripe\Dev\FunctionalTest;
15
use SilverStripe\ORM\ArrayList;
16
use SilverStripe\ORM\FieldType\DBHTMLText;
17
use SilverStripe\Security\PasswordEncryptor;
18
use SilverStripe\Security\PasswordEncryptor_NotFoundException;
19
use SilverStripe\View\ArrayData;
20
21
class OrderFactoryTest extends FunctionalTest
22
{
23
    /**
24
     * @var string
25
     */
26
    private $dummy_key = 'abc123xzy';
27
28
    /**
29
     * @var string[]
30
     */
31
    protected static $fixture_file = [
32
        '../orders.yml',
33
        '../orderhistory.yml',
34
        '../customers.yml',
35
    ];
36
37
    /**
38
     * @var string[]
39
     */
40
    protected static $extra_dataobjects = [
41
        TestProduct::class,
42
    ];
43
44
    /**
45
     * @var \string[][]
46
     */
47
    protected static $required_extensions = [
48
        Variation::class => [
49
            TestVariationDataExtension::class,
50
        ],
51
        TestProduct::class => [
52
            Purchasable::class,
53
        ],
54
    ];
55
56
    /**
57
     *
58
     */
59
    protected function setUp()
60
    {
61
        Director::config()->merge('rules', ['foxy//$Action/$ID/$Name' => FoxyController::class]);
62
63
        parent::setUp();
64
    }
65
66
    /**
67
     * @return string
68
     * @throws PasswordEncryptor_NotFoundException
69
     */
70
    protected function getTransactionData()
71
    {
72
        return \rc4crypt::encrypt($this->dummy_key, $this->getXML()->getValue());
73
    }
74
75
    /**
76
     * @return DBHTMLText
77
     * @throws PasswordEncryptor_NotFoundException
78
     */
79
    protected function getXML()
80
    {
81
        return Controller::singleton()->renderWith(
82
            'TestData',
83
            [
84
                'OrderID' => 111111222222333333,
85
                'TransactionDate' => strtotime('now'),
86
                'Email' => '[email protected]',
87
                'HashType' => 'sha1_salted_suffix',
88
                'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt',
89
                'HashedPassword' => PasswordEncryptor::create_for_algorithm('sha1_v2.4')->encrypt('MyF00p@$$w0Rd', 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt'),
90
                'OrderDetails' => $this->getOrderDetails(),
91
            ]
92
        );
93
    }
94
95
    /**
96
     * @return ArrayList
97
     */
98
    protected function getOrderDetails()
99
    {
100
        return ArrayList::create([
101
            ArrayData::create([
102
                'Options' => $this->getOptions(),
103
            ]),
104
        ]);
105
    }
106
107
    /**
108
     * @return ArrayList
109
     */
110
    protected function getOptions()
111
    {
112
        return ArrayList::create();
113
    }
114
115
    /**
116
     * @throws PasswordEncryptor_NotFoundException
117
     */
118
    public function testGetOrder()
119
    {
120
        $currentKey = FoxyHelper::config()->get('secret');
121
        FoxyHelper::config()->set('secret', $this->dummy_key);
122
        $xmlString = <<<XML
123
{$this->getTransactionData()}
124
XML;
125
126
127
        $this->post('/foxy', ['FoxyData' => $xmlString], ['Content-Type' => 'application/octet-stream']);
128
129
        $order = Order::get()->filter('OrderID', 111111222222333333)->first();
130
131
        $this->assertInstanceOf(Order::class, $order);
132
133
        FoxyHelper::config()->set('secret', $currentKey);
134
    }
135
}
136