|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Book extends Model { |
|
4
|
|
|
const _PRIMARY_KEY_ = 'books.id'; |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
class ModelTest extends PHPUnit_Framework_TestCase { |
|
8
|
|
|
private $b1,$b2; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct(){ |
|
11
|
|
|
SQL::connect('sqlite::memory:'); |
|
12
|
|
|
SQL::exec("CREATE TABLE books ( |
|
13
|
|
|
id integer primary key, |
|
14
|
|
|
title text |
|
15
|
|
|
);"); |
|
16
|
|
|
|
|
17
|
|
|
$this->b1 = Book::create([ |
|
18
|
|
|
'id' => 1, |
|
19
|
|
|
'title' => 'My book', |
|
20
|
|
|
]); |
|
21
|
|
|
|
|
22
|
|
|
$this->b2 = Book::create([ |
|
23
|
|
|
'id' => 2, |
|
24
|
|
|
'title' => 'Necronomicon', |
|
25
|
|
|
]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testCreate(){ |
|
29
|
|
|
$results = SQL::value('SELECT title from books where id=1'); |
|
30
|
|
|
$this->assertEquals('My book',$results); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testSave(){ |
|
34
|
|
|
$this->b1->title = "My Awesome Book"; |
|
35
|
|
|
$this->b1->save(); |
|
36
|
|
|
$results = SQL::value('SELECT title from books where id=1'); |
|
37
|
|
|
$this->assertEquals('My Awesome Book',$results); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testLoad(){ |
|
41
|
|
|
$b2_loaded = Book::load(2); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertNotNull($b2_loaded); |
|
44
|
|
|
$this->assertNotFalse($b2_loaded); |
|
45
|
|
|
$this->assertEquals('Necronomicon',$b2_loaded->title); |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testRetrieveAll(){ |
|
50
|
|
|
$results = Book::all(); |
|
51
|
|
|
$this->assertEquals(2,count($results)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testWhereSimple(){ |
|
55
|
|
|
$results = json_encode(Book::where('id=2')); |
|
56
|
|
|
$this->assertEquals('[{"id":"2","title":"Necronomicon"}]',$results); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|