1: <?php
2: class ExceptionTest extends AMysql_TestCase
3: {
4:
5: public function testGetParams()
6: {
7: $amysql = $this->_amysql;
8: $data = array(
9: 'id' => 1,
10: 'string' => 'bla'
11: );
12: $amysql->insert($this->tableName, $data);
13: try {
14: $amysql->insert($this->tableName, $data);
15: $this->fail('An exception should be thrown');
16: } catch (AMysql_Exception $e) {
17: $this->assertEquals(
18: AMysql_Exception::CODE_DUPLICATE_ENTRY,
19: $e->getCode()
20: );
21: $props = $e->getParams();
22: $this->assertEquals(array('1', 'PRIMARY'), $props);
23: }
24: }
25:
26: public function testChildConstraintCode()
27: {
28: $this->setUpConstraintTables();
29: try {
30: $this->_amysql->insert('child', array(
31: 'id' => 1,
32: 'parent_id' => 1,
33: ));
34: $this->tearDownConstraintTables();
35: $this->fail('An exception should be thrown');
36: } catch (AMysql_Exception $e) {
37: $this->assertEquals(
38: AMysql_Exception::CODE_CHILD_FOREIGN_KEY_CONSTRAINT_FAILS,
39: $e->getCode()
40: );
41: $expected = '`' . AMYSQL_TEST_DB . '`.`child`, CONSTRAINT `bla` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)';
42: $this->assertEquals($expected, $e->getParams(0));
43: }
44: $this->tearDownConstraintTables();
45: }
46:
47: public function testParentConstraintCode()
48: {
49: $this->setUpConstraintTables();
50: try {
51: $this->_amysql->insert('parent', array(
52: 'id' => 1,
53: 'bla' => 1,
54: ));
55: $this->_amysql->insert('child', array(
56: 'id' => 1,
57: 'parent_id' => 1,
58: ));
59: $this->_amysql->delete('parent', 'id = ?', 1);
60: $this->tearDownConstraintTables();
61: $this->fail('An exception should be thrown');
62: } catch (AMysql_Exception $e) {
63: $this->assertEquals(
64: AMysql_Exception::CODE_PARENT_FOREIGN_KEY_CONSTRAINT_FAILS,
65: $e->getCode()
66: );
67: $expected = '`' . AMYSQL_TEST_DB . '`.`child`, CONSTRAINT `bla` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)';
68: $this->assertEquals($expected, $e->getParams(0));
69: }
70: $this->tearDownConstraintTables();
71: }
72:
73: public function setUpConstraintTables()
74: {
75: $q = array();
76: $q[] = <<<EOT
77: DROP TABLE IF EXISTS child
78: EOT;
79: $q[] = <<<EOT
80: DROP TABLE IF EXISTS parent
81: EOT;
82: $q[] = <<<EOT
83: --
84: -- Table structure for table `child`
85: --
86:
87: CREATE TABLE IF NOT EXISTS `child` (
88: `id` int(11) NOT NULL AUTO_INCREMENT,
89: `parent_id` int(11) NOT NULL,
90: PRIMARY KEY (`id`),
91: KEY `parent_id` (`parent_id`)
92: ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
93: EOT;
94: $q[] = <<<EOT
95: --
96: -- Table structure for table `parent`
97: --
98:
99: CREATE TABLE IF NOT EXISTS `parent` (
100: `id` int(11) NOT NULL AUTO_INCREMENT,
101: `bla` int(11) NOT NULL,
102: PRIMARY KEY (`id`)
103: ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
104: EOT;
105: $q[] = <<<EOT
106: --
107: -- Constraints for dumped tables
108: --
109:
110: --
111: -- Constraints for table `child`
112: --
113: ALTER TABLE `child`
114: ADD CONSTRAINT `bla` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`);
115: EOT;
116: foreach ($q as $query) {
117: $this->_amysql->query($query);
118: }
119:
120: }
121:
122: public function tearDownConstraintTables()
123: {
124: $q = array();
125: $q[] = <<<EOT
126: DROP TABLE IF EXISTS child
127: EOT;
128: $q[] = <<<EOT
129: DROP TABLE IF EXISTS parent
130: EOT;
131: foreach ($q as $query) {
132: $this->_amysql->query($query);
133: }
134: }
135: }
136: ?>
137: