1: <?php
2:
3: namespace Alchemy\tests;
4: use Alchemy\util\promise\Promise;
5: use Alchemy\core\schema\Foreign;
6: use Alchemy\core\schema\Integer;
7: use Alchemy\core\schema\Column;
8: use Alchemy\core\schema\Table;
9:
10:
11: class ForeignTest extends BaseTest {
12:
13: public function testCopy() {
14: $table = Table::Core('Table', array('columns' => array(
15: 'Int' => Column::Integer(array(11, 'null' => true)) )));
16:
17: $fk = Column::Foreign('self.Int');
18:
19: $col = $fk->copy(array(), $table, 'FK');
20:
21:
22: $this->assertEquals("Integer", $col->getType());
23: $this->assertEquals($table, $col->getTable());
24: $this->assertEquals(11, $col->getArg(0));
25: $this->assertEquals(53, $col->decode('53'));
26:
27:
28: $this->assertTrue($table->getColumn('Int')->isNullable());
29: $this->assertFalse($col->isNullable());
30:
31:
32: $this->assertEquals(array($table->getColumn('Int')), $col->getForeignKey()->listSources());
33:
34:
35: $fk = Column::Foreign(array($col, 'null' => true));
36: $colB = $fk->copy();
37: $this->assertEquals("Integer", $colB->getType());
38: $this->assertEquals(array($col), $colB->getForeignKey()->listSources());
39: $this->assertEquals(11, $colB->getArg(0));
40: $this->assertTrue($colB->isNullable());
41: }
42:
43:
44: public function testRegisteredTable() {
45: $table = Table::Core('Table', array('columns' => array(
46: 'Int' => Column::Integer(11) )));
47:
48: $fk = Column::Foreign('Table.Int');
49:
50: $table->register();
51:
52: $col = $fk->copy();
53: $this->assertEquals(array($table->getColumn('Int')), $col->getForeignKey()->listSources());
54: }
55:
56:
57: public function testSelfReferencingTable() {
58: $table = Table::Core('Table', array('columns' => array(
59: 'Int' => Column::Integer(11),
60: 'FK' => Column::Foreign(array('self.Int', 'null' => true)) )));
61:
62: $col = $table->getColumn('FK');
63: $this->assertEquals("Integer", $col->getType());
64: $this->assertEquals($table, $col->getTable());
65: $this->assertEquals(11, $col->getArg(0));
66:
67:
68: $col = Column::Foreign('self.Int');
69: $this->assertThrows("Exception", array($col, 'copy'));
70: }
71:
72:
73: public function testSelfReferencingTablePromise() {
74:
75: $table = new Promise(function() use (&$table) {
76: return Table::Core('Table', array('columns' => array(
77: 'PK' => Column::Integer(11),
78: 'FK' => Column::Foreign($table->getColumn('PK')) )));
79: }, "Alchemy\core\schema\Table");
80:
81: $this->assertInstanceOf("Alchemy\util\promise\Promise", $table->getColumn('FK'));
82: $this->assertEquals(11, $table->getColumn('FK')->getArg(0));
83:
84:
85: $sources = $table->getColumn('FK')->getForeignKey()->listSources();
86: $this->assertInstanceOf("Alchemy\util\promise\Promise", $sources[0]);
87: }
88: }
89: