SilvershopProductModelModelTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 39
c 2
b 0
f 0
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testRequiredFields() 0 31 3
A testNew() 0 22 1
1
<?php
2
3
namespace AntonyThorpe\SilverShopProductModel\Tests;
4
5
use Exception;
6
use SilverShop\Page\ProductCategory;
0 ignored issues
show
Bug introduced by
The type SilverShop\Page\ProductCategory 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...
7
use AntonyThorpe\SilverShopProductModel\ProductModel;
8
use SilverStripe\Dev\SapphireTest;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Dev\SapphireTest 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...
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(): void
15
    {
16
        $product_category = $this->objFromFixture(ProductCategory::class, "electronics");
17
        $new_product_model = ProductModel::create(['Title' => 'Freighter', 'Description' => 'Transportation across the Galaxy', 'ProductCategoryID' => $product_category->ID]);
18
        $id = $new_product_model->write();
19
20
        $product_model = ProductModel::get()->byID($id);
21
22
        $this->assertSame(
23
            'Freighter',
24
            $product_model->Title,
25
            'The Title is Freighter'
26
        );
27
        $this->assertSame(
28
            'Transportation across the Galaxy',
29
            $product_model->Description,
30
            'The Description is Transportation across the Galaxy'
31
        );
32
        $this->assertSame(
33
            (int) $product_category->ID,
34
            $product_model->ProductCategoryID,
35
            'The ProductCategoryID is ' . $product_category->ID
36
        );
37
    }
38
39
    public function testRequiredFields(): void
40
    {
41
        // create an instance that lacks the required Title field
42
        $product_category = $this->objFromFixture(ProductCategory::class, "electronics");
43
        $new_product_model = ProductModel::create([
44
            //'Title' => 'Freighter',
45
            'Description' => 'Transportation across the Galaxy',
46
            'ProductCategoryID' => $product_category->ID,
47
        ]);
48
        $writeFailed = false;
49
        try {
50
            $new_product_model->write();
51
        } catch (Exception $ex) {
52
            $writeFailed = true;
53
        }
54
        $this->assertTrue(
55
            $writeFailed,
56
            "ProductModel should not be writable, since it doesn't contain the required Title field"
57
        );
58
59
        // create an instance that lacks the required ProductCategoryID field to test error
60
        $new_product_model = ProductModel::create(['Title' => 'Freighter', 'Description' => 'Transportation across the Galaxy']);
61
        $writeFailed = false;
62
        try {
63
            $new_product_model->write();
64
        } catch (Exception) {
65
            $writeFailed = true;
66
        }
67
        $this->assertTrue(
68
            $writeFailed,
69
            "ProductModel should not be writable, since it doesn't contain the required ProductCategoryID field"
70
        );
71
    }
72
}
73