Completed
Pull Request — 2.0 (#506)
by Roman
17:36
created

OrderModifierTest::testModifierFailure()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
rs 8.8571
cc 3
eloc 21
nc 4
nop 0
1
<?php
2
3
/**
4
 * @package    shop
5
 * @subpackage tests
6
 */
7
class OrderModifierTest extends FunctionalTest
8
{
9
    public static $fixture_file   = 'silvershop/tests/fixtures/shop.yml';
10
    public static $disable_theme  = true;
11
    public static $use_draft_site = true;
12
13
    protected $extraDataObjects = array('OrderModifierTest_TestModifier');
14
15
    public function setUp()
16
    {
17
        parent::setUp();
18
        ShopTest::setConfiguration();
19
        Order::config()->modifiers = array(
20
            "FlatTaxModifier",
21
        );
22
        FlatTaxModifier::config()->rate = 0.25;
23
        FlatTaxModifier::config()->name = "GST";
24
25
        $this->mp3player = $this->objFromFixture('Product', 'mp3player');
0 ignored issues
show
Bug introduced by
The property mp3player 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...
26
        $this->socks = $this->objFromFixture('Product', 'socks');
0 ignored issues
show
Bug introduced by
The property socks 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...
27
        $this->mp3player->publish('Stage', 'Live');
28
        $this->socks->publish('Stage', 'Live');
29
    }
30
31
    public function testModifierCalculation()
32
    {
33
        $order = $this->createOrder();
34
        $this->assertEquals(510, $order->calculate(), "Total with 25% tax");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderModifierTest>.

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...
35
36
        //remove modifiers
37
        Order::config()->modifiers = null;
38
        $order->calculate();
39
        $this->assertEquals(408, $order->calculate(), "Total with no modification");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderModifierTest>.

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...
40
    }
41
42
    public function testModifierFailure()
43
    {
44
        if (!ShopTools::DBConn()->supportsTransactions()) {
45
            $this->markTestSkipped(
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<OrderModifierTest>.

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...
46
                'The Database doesn\'t support transactions.'
47
            );
48
        }
49
50
        Config::inst()->update('Order', 'modifiers', array(
51
            'OrderModifierTest_TestModifier'
52
        ));
53
        $order = $this->createOrder();
54
        $order->calculate();
55
        $order->write();
56
57
        // 408 from items + 10 from modifier + 25% from tax
58
        $this->assertEquals('522.5', $order->Total);
0 ignored issues
show
Documentation introduced by
The property Total does not exist on object<Order>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

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

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...
59
60
        $this->assertEquals(array('10', '104.5'), $order->Modifiers()->column('Amount'));
0 ignored issues
show
Documentation Bug introduced by
The method Modifiers does not exist on object<Order>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderModifierTest>.

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...
61
62
        OrderModifierTest_TestModifier::$value = 42;
63
64
        try {
65
            // Calculate will now fail!
66
            $order->calculate();
67
        } catch (Exception $e){}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
69
        // reload order from DB
70
        $order = Order::get()->byID($order->ID);
71
72
        // Order Total should not have changed
73
        $this->assertEquals('522.5', $order->Total);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderModifierTest>.

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...
74
75
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<OrderModifierTest>.

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...
76
            array('10', '104.5'),
77
            $order->Modifiers()->column('Amount'),
78
            'Modifiers aren\'t allowed to change upon failure'
79
        );
80
    }
81
82
    public function createOrder()
83
    {
84
        $order = new Order();
85
        $order->write();
86
        $item1a = $this->mp3player->createItem(2);
87
        $item1a->write();
88
        $order->Items()->add($item1a);
0 ignored issues
show
Documentation Bug introduced by
The method Items does not exist on object<Order>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
89
        $item1b = $this->socks->createItem();
90
        $item1b->write();
91
        $order->Items()->add($item1b);
0 ignored issues
show
Documentation Bug introduced by
The method Items does not exist on object<Order>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
92
        return $order;
93
    }
94
}
95
96
class OrderModifierTest_TestModifier extends OrderModifier implements TestOnly
97
{
98
    public static $value = 10;
99
    private $willFail = false;
100
101
    public function value($incoming)
102
    {
103
        if (self::$value === 42) {
104
            $this->willFail = true;
105
        }
106
        return self::$value;
107
    }
108
109
    protected function onAfterWrite()
110
    {
111
        parent::onAfterWrite();
112
        if ($this->willFail) {
113
            user_error('Modifier failure!');
114
        }
115
    }
116
}
117