@@ -75,36 +75,112 @@ public function testConnectMethodReturnsConnectionInterface(): void
7575 $ connection ->disconnect ();
7676 }
7777
78- /**
79- * @todo Implement testBeginTransaction().
80- */
81- public function testBeginTransaction (): never
78+ public function testBeginTransaction (): void
8279 {
83- // Remove the following lines when you implement this test.
84- $ this ->markTestIncomplete (
85- 'This test has not been implemented yet. '
86- );
80+ $ connection = $ this ->getAdapter ()->getDriver ()->getConnection ();
81+ $ connection ->connect ();
82+
83+ self ::assertTrue ($ connection ->isConnected ());
84+ self ::assertFalse ($ connection ->inTransaction ());
85+
86+ $ result = $ connection ->beginTransaction ();
87+
88+ self ::assertInstanceOf (Connection::class, $ result );
89+ self ::assertTrue ($ connection ->inTransaction ());
90+
91+ $ connection ->rollback ();
92+ self ::assertFalse ($ connection ->inTransaction ());
93+ $ connection ->disconnect ();
8794 }
8895
89- /**
90- * @todo Implement testCommit().
91- */
92- public function testCommit (): never
96+ public function testCommit (): void
9397 {
94- // Remove the following lines when you implement this test.
95- $ this ->markTestIncomplete (
96- 'This test has not been implemented yet. '
97- );
98+ $ connection = $ this ->getAdapter ()->getDriver ()->getConnection ();
99+ $ connection ->connect ();
100+ self ::assertTrue ($ connection ->isConnected ());
101+
102+ $ connection ->beginTransaction ();
103+ self ::assertTrue ($ connection ->inTransaction ());
104+
105+ $ connection ->execute ("INSERT INTO test (name, value) VALUES ('tx_commit', 'test') " );
106+
107+ $ result = $ connection ->commit ();
108+ self ::assertInstanceOf (Connection::class, $ result );
109+ self ::assertFalse ($ connection ->inTransaction ());
110+
111+ $ result = $ connection ->execute ("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_commit' " );
112+ self ::assertSame (1 , $ result ->getResource ()->fetchColumn ());
113+
114+ $ connection ->execute ("DELETE FROM test WHERE name = 'tx_commit' " );
115+ $ connection ->disconnect ();
98116 }
99117
100- /**
101- * @todo Implement testRollback().
102- */
103- public function testRollback (): never
118+ public function testRollback (): void
104119 {
105- // Remove the following lines when you implement this test.
106- $ this ->markTestIncomplete (
107- 'This test has not been implemented yet. '
108- );
120+ $ connection = $ this ->getAdapter ()->getDriver ()->getConnection ();
121+ $ connection ->connect ();
122+ self ::assertTrue ($ connection ->isConnected ());
123+
124+ $ connection ->beginTransaction ();
125+ self ::assertTrue ($ connection ->inTransaction ());
126+
127+ $ connection ->execute ("INSERT INTO test (name, value) VALUES ('tx_rollback', 'test') " );
128+
129+ $ result = $ connection ->rollback ();
130+ self ::assertInstanceOf (Connection::class, $ result );
131+ self ::assertFalse ($ connection ->inTransaction ());
132+
133+ $ result = $ connection ->execute ("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_rollback' " );
134+ self ::assertSame (0 , $ result ->getResource ()->fetchColumn ());
135+
136+ $ connection ->disconnect ();
137+ }
138+
139+ public function testAutocommitRestoredAfterCommit (): void
140+ {
141+ /** @var Connection $connection */
142+ $ connection = $ this ->getAdapter ()->getDriver ()->getConnection ();
143+ $ connection ->connect ();
144+ self ::assertTrue ($ connection ->isConnected ());
145+
146+ $ connection ->beginTransaction ();
147+ self ::assertTrue ($ connection ->inTransaction ());
148+ $ connection ->commit ();
149+ self ::assertFalse ($ connection ->inTransaction ());
150+
151+ $ connection ->execute ("INSERT INTO test (name, value) VALUES ('tx_autocommit', 'test') " );
152+
153+ $ connection ->disconnect ();
154+
155+ $ connection ->connect ();
156+ $ result = $ connection ->execute ("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_autocommit' " );
157+ self ::assertSame (1 , $ result ->getResource ()->fetchColumn ());
158+
159+ $ connection ->execute ("DELETE FROM test WHERE name = 'tx_autocommit' " );
160+ $ connection ->disconnect ();
161+ }
162+
163+ public function testAutocommitRestoredAfterRollback (): void
164+ {
165+ /** @var Connection $connection */
166+ $ connection = $ this ->getAdapter ()->getDriver ()->getConnection ();
167+ $ connection ->connect ();
168+ self ::assertTrue ($ connection ->isConnected ());
169+
170+ $ connection ->beginTransaction ();
171+ self ::assertTrue ($ connection ->inTransaction ());
172+ $ connection ->rollback ();
173+ self ::assertFalse ($ connection ->inTransaction ());
174+
175+ $ connection ->execute ("INSERT INTO test (name, value) VALUES ('tx_autocommit_rb', 'test') " );
176+
177+ $ connection ->disconnect ();
178+
179+ $ connection ->connect ();
180+ $ result = $ connection ->execute ("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_autocommit_rb' " );
181+ self ::assertSame (1 , $ result ->getResource ()->fetchColumn ());
182+
183+ $ connection ->execute ("DELETE FROM test WHERE name = 'tx_autocommit_rb' " );
184+ $ connection ->disconnect ();
109185 }
110186}
0 commit comments