1: <?php
2:
3: namespace Alchemy\tests;
4: use Alchemy\orm\Session;
5: use Datetime;
6:
7:
8: class SessionIntegrationTest extends BaseTest {
9:
10: public function testModelRoundTrip() {
11: $engines = array(
12: $this->getSQLiteEngine(),
13: $this->getMySQLEngine(),
14: );
15:
16: foreach ($engines as $engine) {
17: $session = new Session($engine);
18: $session->ddl()->dropAll();
19: $session->ddl()->createAll();
20:
21:
22: $lang = new Language();
23: $lang->ISO2Code = 'es';
24: $lang->LatestChangeStamp = new DateTime("1984-01-01");
25: $session->add($lang);
26: $session->commit();
27:
28:
29: $objects = $session->objects('Alchemy\tests\Language');
30: $this->assertEquals(1, count($all = $objects->all()));
31: $this->assertEquals(1, count($one = $objects->one()));
32: $lang = $all[0];
33: $this->assertEquals(1, $lang->LanguageID);
34: $this->assertEquals('es', $lang->ISO2Code);
35: $this->assertEquals('1984-01-01', $lang->LatestChangeStamp->format('Y-m-d'));
36:
37:
38: $lang->LatestChangeStamp = new DateTime("1985-06-15");
39: $lang->save();
40: $session->commit();
41:
42:
43: $objects = $session->objects('Alchemy\tests\Language');
44: $this->assertEquals(1, count($one = $objects->one()));
45: $lang = $all[0];
46: $this->assertEquals(1, $lang->LanguageID);
47: $this->assertEquals('es', $lang->ISO2Code);
48: $this->assertEquals('1985-06-15', $lang->LatestChangeStamp->format('Y-m-d'));
49:
50:
51: $session->remove($lang);
52: $session->commit();
53:
54:
55: $objects = $session->objects('Alchemy\tests\Language');
56: $this->assertEquals(0, count($one = $objects->all()));
57: }
58: }
59:
60:
61: public function testZeroRows() {
62: $session = new Session($this->getSQLiteEngine());
63:
64: $session->ddl()->dropAll();
65: $session->ddl()->createAll();
66:
67: $objects = $session->objects('Alchemy\tests\Language');
68: $this->assertEquals(0, count($objects->all()));
69: $this->assertEquals(0, count($objects->first()));
70: $this->assertThrows("\Exception", array($objects, 'one'));
71: }
72:
73:
74: public function testMultipleRows() {
75: $session = new Session($this->getSQLiteEngine());
76:
77: $session->ddl()->dropAll();
78: $session->ddl()->createAll();
79:
80: $lang = new Language();
81: $lang->LanguageID = 10;
82: $lang->ISO2Code = 'es';
83: $lang->LatestChangeStamp = new DateTime("1984-01-01");
84: $session->add($lang);
85:
86: $lang = new Language();
87: $lang->LanguageID = 12;
88: $lang->ISO2Code = 'fr';
89: $lang->LatestChangeStamp = new DateTime("1984-01-01");
90: $session->add($lang);
91:
92: $session->commit();
93:
94: $objects = $session->objects('Alchemy\tests\Language');
95: $this->assertEquals(2, count($all = $objects->all()));
96: $this->assertEquals(1, count($first = $objects->first()));
97:
98: $this->assertInstanceOf('Alchemy\tests\Language', $first);
99: $this->assertEquals(10, $first->LanguageID);
100: $this->assertEquals('es', $first->ISO2Code);
101:
102: $this->assertThrows("Exception", array($objects, 'one'));
103: }
104: }
105: