|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class modelTest |
|
5
|
|
|
*/ |
|
6
|
|
|
Class CategoryTest extends \PHPUnit_Framework_TestCase |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
public static function setUpBeforeClass() { |
|
14
|
|
|
// connect and setup db categorytest with table categories |
|
15
|
|
|
try { |
|
16
|
|
|
Freshsauce\Model\Model::connectDb('mysql:host=127.0.0.1;dbname=unit_test', 'unit_test_user', 'unit_test_password'); |
|
17
|
|
|
} catch (PDOException $e) { |
|
18
|
|
|
if ($e->getCode() != 0) { |
|
19
|
|
|
// throw it on |
|
20
|
|
|
throw $e; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$sql_setup = [ |
|
25
|
|
|
'DROP DATABASE IF EXISTS `categorytest`', |
|
26
|
|
|
'CREATE DATABASE `categorytest`', |
|
27
|
|
|
'USE `categorytest`', |
|
28
|
|
|
'CREATE TABLE `categories` ( |
|
29
|
|
|
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
|
30
|
|
|
`name` VARCHAR(120) DEFAULT NULL, |
|
31
|
|
|
`updated_at` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, |
|
32
|
|
|
`created_at` TIMESTAMP NULL DEFAULT NULL, |
|
33
|
|
|
PRIMARY KEY (`id`) |
|
34
|
|
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
foreach ($sql_setup as $sql) { |
|
38
|
|
|
Freshsauce\Model\Model::execute($sql); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function tearDownAfterClass() { |
|
47
|
|
|
Freshsauce\Model\Model::execute('DROP DATABASE IF EXISTS `categorytest`'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @covers ::save |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testCreate() { |
|
54
|
|
|
$_name = 'Fiction'; |
|
55
|
|
|
$category = new App\Model\Category(array( |
|
56
|
|
|
'name' => $_name |
|
57
|
|
|
)); |
|
58
|
|
|
|
|
59
|
|
|
$category->save(); // no Id so will insert |
|
60
|
|
|
|
|
61
|
|
|
$this->assertEquals($category->name, $_name); |
|
62
|
|
|
$this->assertNotEmpty($category->id); |
|
63
|
|
|
$this->assertNotEmpty($category->created_at); |
|
64
|
|
|
$this->assertNotEmpty($category->updated_at); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @covers ::getById |
|
69
|
|
|
*/ |
|
70
|
|
|
public function testCreateAndGetById() { |
|
71
|
|
|
$_name = 'SciFi'; |
|
72
|
|
|
$category = new App\Model\Category(array( |
|
73
|
|
|
'name' => $_name |
|
74
|
|
|
)); |
|
75
|
|
|
|
|
76
|
|
|
$category->save(); // no Id so will insert |
|
77
|
|
|
|
|
78
|
|
|
$this->assertEquals($category->name, $_name); |
|
79
|
|
|
$this->assertNotEmpty($category->id); |
|
80
|
|
|
|
|
81
|
|
|
// read category back into a new object |
|
82
|
|
|
$read_category = App\Model\Category::getById($category->id); |
|
83
|
|
|
$this->assertEquals($read_category->name, $_name); |
|
84
|
|
|
$this->assertEquals($read_category->id, $category->id); |
|
85
|
|
|
$this->assertNotEmpty($category->created_at); |
|
86
|
|
|
$this->assertNotEmpty($category->updated_at); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @covers ::save |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testCreateAndModify() { |
|
93
|
|
|
$_name = 'Literature'; |
|
94
|
|
|
$category = new App\Model\Category(array( |
|
95
|
|
|
'name' => $_name |
|
96
|
|
|
)); |
|
97
|
|
|
|
|
98
|
|
|
$category->save(); // no Id so will insert |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals($category->name, $_name); |
|
101
|
|
|
$this->assertNotEmpty($category->id); |
|
102
|
|
|
$this->assertNotEmpty($category->created_at); |
|
103
|
|
|
$this->assertNotEmpty($category->updated_at); |
|
104
|
|
|
|
|
105
|
|
|
$_id = $category->id; |
|
106
|
|
|
$_updated_at = $category->updated_at; |
|
107
|
|
|
|
|
108
|
|
|
$_new_name = 'Literature - great works'; |
|
109
|
|
|
$category->name = $_new_name; |
|
110
|
|
|
sleep(1); // to ensure updated_at time move forward a second at least |
|
111
|
|
|
$category->save(); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertEquals($category->name, $_new_name); |
|
114
|
|
|
$this->assertEquals($category->id, $_id); |
|
115
|
|
|
$this->assertNotEquals($category->updated_at, $_updated_at); |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @covers ::__callStatic |
|
121
|
|
|
* @covers ::fetchAllWhereMatchingSingleField |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testFetchAllWhere() { |
|
124
|
|
|
// Create some categories |
|
125
|
|
|
$_names = [ |
|
126
|
|
|
'Sports', |
|
127
|
|
|
'Politics', |
|
128
|
|
|
'Biography', |
|
129
|
|
|
'Cookbooks' |
|
130
|
|
|
]; |
|
131
|
|
|
foreach ($_names as $_name) { |
|
132
|
|
|
$category = new App\Model\Category(array( |
|
133
|
|
|
'name' => $_name |
|
134
|
|
|
)); |
|
135
|
|
|
$category->save(); // no Id so will insert |
|
136
|
|
|
} |
|
137
|
|
|
$categories = App\Model\Category::find_by_name($_names); |
|
138
|
|
|
$this->assertNotEmpty($categories); |
|
139
|
|
|
$this->assertContainsOnlyInstancesOf('App\Model\Category', $categories); |
|
140
|
|
|
$this->assertCount(count($_names), $categories); |
|
141
|
|
|
} |
|
142
|
|
|
} |