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 |
68
|
|
|
$product_category = $this->objFromFixture(ProductCategory::class, "electronics"); |
|
|
|
|
69
|
|
|
$new_product_model = new ProductModel( |
70
|
|
|
array( |
71
|
|
|
'Title' => 'Freighter', |
72
|
|
|
'Description' => 'Transportation across the Galaxy', |
73
|
|
|
//'ProductCategoryID' => $product_category->ID |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
$writeFailed = false; |
77
|
|
|
try { |
78
|
|
|
$new_product_model->write(); |
79
|
|
|
} catch (Exception $ex) { |
80
|
|
|
$writeFailed = true; |
81
|
|
|
} |
82
|
|
|
$this->assertTrue( |
83
|
|
|
$writeFailed, |
84
|
|
|
"ProductModel should not be writable, since it doesn't contain the required ProductCategoryID field" |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|