Skip to content

Commit d70b53f

Browse files
committed
Tests for transaction instrumentation and splitQuery
1 parent 1ba6c4d commit d70b53f

File tree

2 files changed

+421
-0
lines changed

2 files changed

+421
-0
lines changed

src/Instrumentation/MySqli/tests/Integration/MySqliInstrumentationTest.php

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,264 @@ public function test_mysqli_multi_query_procedural(): void
648648
$this->assertDatabaseAttributesForAllSpans($offset);
649649
}
650650

651+
public function test_mysqli_transaction_rollback_objective(): void
652+
{
653+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
654+
655+
$mysqli = new mysqli($this->mysqlHost, $this->user, $this->passwd, $this->database);
656+
657+
$offset = 0;
658+
$this->assertSame('mysqli::__construct', $this->storage->offsetGet($offset)->getName());
659+
660+
$mysqli->query('DROP TABLE IF EXISTS language;');
661+
$offset++;
662+
663+
$mysqli->query('CREATE TABLE IF NOT EXISTS language ( Code text NOT NULL, Speakers int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
664+
$offset++;
665+
666+
$mysqli->begin_transaction(name: 'supertransaction');
667+
$offset++;
668+
$this->assertSame('mysqli::begin_transaction', $this->storage->offsetGet($offset)->getName());
669+
$this->assertAttributes($offset, [
670+
'db.transaction.name' => 'supertransaction',
671+
]);
672+
673+
try {
674+
// Insert some values
675+
$mysqli->query("INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)");
676+
677+
$offset++;
678+
$this->assertSame('mysqli::query', $this->storage->offsetGet($offset)->getName());
679+
$this->assertAttributes($offset, [
680+
TraceAttributes::DB_STATEMENT => "INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)",
681+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
682+
]);
683+
684+
// Try to insert invalid values
685+
$language_code = 'FR';
686+
$native_speakers = 'Unknown';
687+
$stmt = $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');
688+
689+
$stmt->bind_param('ss', $language_code, $native_speakers);
690+
$stmt->execute(); // THROWS HERE
691+
692+
$this->fail('Should never reach this point');
693+
} catch (mysqli_sql_exception $exception) {
694+
$offset++;
695+
$this->assertSame('mysqli_stmt::execute', $this->storage->offsetGet($offset)->getName());
696+
$this->assertAttributes($offset, [
697+
TraceAttributes::DB_STATEMENT => 'INSERT INTO language(Code, Speakers) VALUES (?,?)',
698+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
699+
]);
700+
701+
$mysqli->rollback(name: 'supertransaction');
702+
$offset++;
703+
$this->assertSame('mysqli::rollback', $this->storage->offsetGet($offset)->getName());
704+
$this->assertAttributes($offset, [
705+
'db.transaction.name' => 'supertransaction',
706+
]);
707+
708+
}
709+
710+
$offset++;
711+
$this->assertCount($offset, $this->storage);
712+
$this->assertDatabaseAttributesForAllSpans($offset);
713+
}
714+
715+
public function test_mysqli_transaction_rollback_procedural(): void
716+
{
717+
mysqli_report(MYSQLI_REPORT_ERROR);
718+
719+
$mysqli = mysqli_connect($this->mysqlHost, $this->user, $this->passwd, $this->database);
720+
721+
$offset = 0;
722+
$this->assertSame('mysqli_connect', $this->storage->offsetGet($offset)->getName());
723+
724+
mysqli_query($mysqli, 'DROP TABLE IF EXISTS language;');
725+
$offset++;
726+
727+
mysqli_query($mysqli, 'CREATE TABLE IF NOT EXISTS language ( Code text NOT NULL, Speakers int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
728+
$offset++;
729+
730+
mysqli_begin_transaction($mysqli, name: 'supertransaction');
731+
$offset++;
732+
$this->assertSame('mysqli_begin_transaction', $this->storage->offsetGet($offset)->getName());
733+
$this->assertAttributes($offset, [
734+
'db.transaction.name' => 'supertransaction',
735+
]);
736+
737+
try {
738+
// Insert some values
739+
mysqli_query($mysqli, "INSERT INTO language(Code, Speakers) VALUES ('DE', 76000001)");
740+
741+
$offset++;
742+
$this->assertSame('mysqli_query', $this->storage->offsetGet($offset)->getName());
743+
$this->assertAttributes($offset, [
744+
TraceAttributes::DB_STATEMENT => "INSERT INTO language(Code, Speakers) VALUES ('DE', 76000001)",
745+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
746+
]);
747+
748+
// Try to insert invalid values
749+
$language_code = 'FR';
750+
$native_speakers = 'Unknown';
751+
$stmt = mysqli_prepare($mysqli, 'INSERT INTO language(Code, Speakers) VALUES (?,?)');
752+
753+
mysqli_stmt_bind_param($stmt, 'ss', $language_code, $native_speakers);
754+
755+
try {
756+
mysqli_stmt_execute($stmt);
757+
} catch (\PHPUnit\Framework\Error\Warning $e) {
758+
$offset++;
759+
$this->assertSame('mysqli_stmt_execute', $this->storage->offsetGet($offset)->getName());
760+
$this->assertAttributes($offset, [
761+
TraceAttributes::DB_STATEMENT => 'INSERT INTO language(Code, Speakers) VALUES (?,?)',
762+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
763+
]);
764+
765+
mysqli_rollback($mysqli, name: 'supertransaction');
766+
$offset++;
767+
$this->assertSame('mysqli_rollback', $this->storage->offsetGet($offset)->getName());
768+
$this->assertAttributes($offset, [
769+
'db.transaction.name' => 'supertransaction',
770+
]);
771+
}
772+
} catch (mysqli_sql_exception $exception) {
773+
$this->fail('Should never reach this point');
774+
}
775+
776+
$offset++;
777+
$this->assertCount($offset, $this->storage);
778+
$this->assertDatabaseAttributesForAllSpans($offset);
779+
}
780+
781+
public function test_mysqli_transaction_commit_objective(): void
782+
{
783+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
784+
785+
$mysqli = new mysqli($this->mysqlHost, $this->user, $this->passwd, $this->database);
786+
787+
$offset = 0;
788+
$this->assertSame('mysqli::__construct', $this->storage->offsetGet($offset)->getName());
789+
790+
$mysqli->query('DROP TABLE IF EXISTS language;');
791+
$offset++;
792+
793+
$mysqli->query('CREATE TABLE IF NOT EXISTS language ( Code text NOT NULL, Speakers int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
794+
$offset++;
795+
796+
$mysqli->begin_transaction(name: 'supertransaction');
797+
$offset++;
798+
$this->assertSame('mysqli::begin_transaction', $this->storage->offsetGet($offset)->getName());
799+
$this->assertAttributes($offset, [
800+
'db.transaction.name' => 'supertransaction',
801+
]);
802+
803+
try {
804+
// Insert some values
805+
$mysqli->query("INSERT INTO language(Code, Speakers) VALUES ('DE', 76000001)");
806+
807+
$offset++;
808+
$this->assertSame('mysqli::query', $this->storage->offsetGet($offset)->getName());
809+
$this->assertAttributes($offset, [
810+
TraceAttributes::DB_STATEMENT => "INSERT INTO language(Code, Speakers) VALUES ('DE', 76000001)",
811+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
812+
]);
813+
814+
// Try to insert invalid values
815+
$language_code = 'FR';
816+
$native_speakers = 66000002;
817+
$stmt = $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');
818+
819+
$stmt->bind_param('ss', $language_code, $native_speakers);
820+
$stmt->execute();
821+
822+
$offset++;
823+
$this->assertSame('mysqli_stmt::execute', $this->storage->offsetGet($offset)->getName());
824+
$this->assertAttributes($offset, [
825+
TraceAttributes::DB_STATEMENT => 'INSERT INTO language(Code, Speakers) VALUES (?,?)',
826+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
827+
]);
828+
829+
$mysqli->commit(name: 'supertransaction');
830+
831+
$offset++;
832+
$this->assertSame('mysqli::commit', $this->storage->offsetGet($offset)->getName());
833+
$this->assertAttributes($offset, [
834+
'db.transaction.name' => 'supertransaction',
835+
]);
836+
} catch (mysqli_sql_exception $exception) {
837+
$this->fail('Unexpected exception was thrown: ' . $exception->getMessage());
838+
}
839+
840+
$offset++;
841+
$this->assertCount($offset, $this->storage);
842+
$this->assertDatabaseAttributesForAllSpans($offset);
843+
}
844+
845+
public function test_mysqli_transaction_commit_procedural(): void
846+
{
847+
mysqli_report(MYSQLI_REPORT_ERROR);
848+
849+
$mysqli = mysqli_connect($this->mysqlHost, $this->user, $this->passwd, $this->database);
850+
851+
$offset = 0;
852+
$this->assertSame('mysqli_connect', $this->storage->offsetGet($offset)->getName());
853+
854+
mysqli_query($mysqli, 'DROP TABLE IF EXISTS language;');
855+
$offset++;
856+
857+
mysqli_query($mysqli, 'CREATE TABLE IF NOT EXISTS language ( Code text NOT NULL, Speakers int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
858+
$offset++;
859+
860+
mysqli_begin_transaction($mysqli, name: 'supertransaction');
861+
$offset++;
862+
$this->assertSame('mysqli_begin_transaction', $this->storage->offsetGet($offset)->getName());
863+
$this->assertAttributes($offset, [
864+
'db.transaction.name' => 'supertransaction',
865+
]);
866+
867+
try {
868+
// Insert some values
869+
mysqli_query($mysqli, "INSERT INTO language(Code, Speakers) VALUES ('DE', 76000001)");
870+
871+
$offset++;
872+
$this->assertSame('mysqli_query', $this->storage->offsetGet($offset)->getName());
873+
$this->assertAttributes($offset, [
874+
TraceAttributes::DB_STATEMENT => "INSERT INTO language(Code, Speakers) VALUES ('DE', 76000001)",
875+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
876+
]);
877+
878+
// Try to insert invalid values
879+
$language_code = 'FR';
880+
$native_speakers = 66000002;
881+
$stmt = mysqli_prepare($mysqli, 'INSERT INTO language(Code, Speakers) VALUES (?,?)');
882+
883+
mysqli_stmt_bind_param($stmt, 'ss', $language_code, $native_speakers);
884+
mysqli_stmt_execute($stmt);
885+
886+
$offset++;
887+
$this->assertSame('mysqli_stmt_execute', $this->storage->offsetGet($offset)->getName());
888+
$this->assertAttributes($offset, [
889+
TraceAttributes::DB_STATEMENT => 'INSERT INTO language(Code, Speakers) VALUES (?,?)',
890+
TraceAttributes::DB_OPERATION_NAME => 'INSERT',
891+
]);
892+
893+
mysqli_commit($mysqli, name: 'supertransaction');
894+
895+
$offset++;
896+
$this->assertSame('mysqli_commit', $this->storage->offsetGet($offset)->getName());
897+
$this->assertAttributes($offset, [
898+
'db.transaction.name' => 'supertransaction',
899+
]);
900+
} catch (mysqli_sql_exception $exception) {
901+
$this->fail('Unexpected exception was thrown: ' . $exception->getMessage());
902+
}
903+
904+
$offset++;
905+
$this->assertCount($offset, $this->storage);
906+
$this->assertDatabaseAttributesForAllSpans($offset);
907+
}
908+
651909
public function test_mysqli_stmt_execute_objective(): void
652910
{
653911
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

0 commit comments

Comments
 (0)