forked from lox/pheasant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMappingTest.php
103 lines (82 loc) · 3.14 KB
/
MappingTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Pheasant\Tests;
use \Pheasant\Types;
use \Pheasant\Tests\Examples\Post;
class BasicMappingTest extends \Pheasant\Tests\MysqlTestCase
{
public function setUp()
{
parent::setUp();
$this->table('post', array(
'postid' => new Types\IntegerType(11, 'primary auto_increment'),
'title' => new Types\StringType(255, 'required'),
'subtitle' => new Types\StringType(255),
));
}
public function testBasicSaving()
{
$post = new Post('First post, bitches!');
$post->subtitle = 'Just because...';
$this->assertEquals((string) $post->postid, null);
$this->assertInstanceOf('\Pheasant\Identity', $post->identity());
$this->assertEquals(array('title','subtitle'), array_keys($post->changes()));
$this->assertFalse($post->isSaved());
$post->save();
$this->assertTrue($post->isSaved());
$this->assertEquals(array(), $post->changes());
$this->assertEquals($post->postid, 1);
$this->assertEquals($post->title, 'First post, bitches!');
$this->assertEquals($post->subtitle, 'Just because...');
$post->title = 'Another title, perhaps';
$this->assertTrue($post->isSaved());
$this->assertEquals(array('title'), array_keys($post->changes()));
$post->save();
$this->assertEquals(array(), $post->changes());
$this->assertEquals($post->title, 'Another title, perhaps');
}
public function testSequentialSave()
{
$post1 = new Post('First post');
$post2 = new Post('Second post');
$this->assertEquals($post1->title, 'First post');
$this->assertEquals($post2->title, 'Second post');
$post1->save();
$post2->save();
$this->assertEquals($post1->title, 'First post');
$this->assertEquals($post2->title, 'Second post');
}
public function testImport()
{
$posts = Post::import(array(
array('title'=>'First Post'),
array('title'=>'Second Post'),
));
$this->assertEquals(count($posts), 2);
$this->assertEquals($posts[0]->postid, 1);
$this->assertEquals($posts[1]->postid, 2);
$this->assertEquals($posts[0]->title, 'First Post');
$this->assertEquals($posts[1]->title, 'Second Post');
$this->assertTrue($posts[0]->isSaved());
$this->assertTrue($posts[1]->isSaved());
}
public function testPropertyReferences()
{
$post = new Post('first post');
$future = $post->postid;
$this->assertTrue(is_object($future));
$this->assertNull($future->value());
$this->assertNull($post->get('postid'));
$post->save();
$this->assertEquals($post->postid, 1);
$this->assertEquals($future->value(), 1);
$this->assertEquals($post->get('postid'), 1);
}
public function testDeleting()
{
$post = Post::create('first post');
$this->assertEquals($post->postid, 1);
$this->assertEquals($post->title, 'first post');
$post->delete();
$this->assertRowCount(0, "SELECT * FROM post WHERE postid=1");
}
}