Completed
Branch master (e1b964)
by Antony
03:54 queued 01:55
created

SilvershopProductModelModelTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 47
c 1
b 0
f 0
dl 0
loc 73
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testNew() 0 28 1
A testRequiredFields() 0 38 3
1
<?php
2
3
namespace AntonyThorpe\SilverShopProductModel\Tests;
4
5
use Exception;
6
use SilverShop\Page\ProductCategory;
7
use AntonyThorpe\SilverShopProductModel\ProductModel;
8
use SilverStripe\Dev\SapphireTest;
9
10
class SilvershopProductModelModelTest extends SapphireTest
11
{
12
    protected static $fixture_file = 'vendor/silvershop/core/tests/php/Fixtures/shop.yml';
13
14
    public function testNew()
15
    {
16
        $product_category = $this->objFromFixture(ProductCategory::class, "electronics");
17
        $new_product_model = new ProductModel(
18
            array(
19
                'Title' => 'Freighter',
20
                'Description' => 'Transportation across the Galaxy',
21
                'ProductCategoryID' => $product_category->ID
22
            )
23
        );
24
        $id = $new_product_model->write();
25
26
        $product_model = ProductModel::get()->byID($id);
27
28
        $this->assertSame(
29
            'Freighter',
30
            $product_model->Title,
31
            'The Title is Freighter'
32
        );
33
        $this->assertSame(
34
            'Transportation across the Galaxy',
35
            $product_model->Description,
36
            'The Description is Transportation across the Galaxy'
37
        );
38
        $this->assertSame(
39
            $product_category->ID,
40
            (int) $product_model->ProductCategoryID,
41
            'The ProductCategoryID is ' . $product_category->ID
42
        );
43
    }
44
45
    public function testRequiredFields()
46
    {
47
        // create an instance that lacks the required Title field
48
        $product_category = $this->objFromFixture(ProductCategory::class, "electronics");
49
        $new_product_model = new ProductModel(
50
            array(
51
                //'Title' => 'Freighter',
52
                'Description' => 'Transportation across the Galaxy',
53
                'ProductCategoryID' => $product_category->ID
54
            )
55
        );
56
        $writeFailed = false;
57
        try {
58
            $new_product_model->write();
59
        } catch (Exception $ex) {
60
            $writeFailed = true;
61
        }
62
        $this->assertTrue(
63
            $writeFailed,
64
            "ProductModel should not be writable, since it doesn't contain the required Title field"
65
        );
66
67
        // create an instance that lacks the required ProductCategoryID field to test error
68
        $new_product_model = new ProductModel(
69
            array(
70
                'Title' => 'Freighter',
71
                'Description' => 'Transportation across the Galaxy'
72
            )
73
        );
74
        $writeFailed = false;
75
        try {
76
            $new_product_model->write();
77
        } catch (Exception $ex) {
78
            $writeFailed = true;
79
        }
80
        $this->assertTrue(
81
            $writeFailed,
82
            "ProductModel should not be writable, since it doesn't contain the required ProductCategoryID field"
83
        );
84
    }
85
}
86