Skip to content

Commit

Permalink
[bugfix] Codeception#47 Malformed UTF-8 characters, possibly incorrec…
Browse files Browse the repository at this point in the history
…tly encoded
  • Loading branch information
sm committed Mar 15, 2023
1 parent 0ff3582 commit 8dd6d2f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/Codeception/Lib/Driver/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ public function executeQuery($query, array $params): PDOStatement
$type = PDO::PARAM_BOOL;
} elseif (is_int($param)) {
$type = PDO::PARAM_INT;
} elseif ($this->isBinary($param)) {
$type = PDO::PARAM_LOB;
} else {
$type = PDO::PARAM_STR;
}
Expand Down Expand Up @@ -342,4 +344,9 @@ public function getOptions(): array
{
return $this->options;
}

protected function isBinary(string $string): bool
{
return false === mb_detect_encoding($string, null, true);
}
}
6 changes: 3 additions & 3 deletions src/Codeception/Module/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public function seeInDatabase(string $table, array $criteria = []): void
$this->assertGreaterThan(
0,
$res,
'No matching records found for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR) . ' in table ' . $table
'No matching records found for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE) . ' in table ' . $table
);
}

Expand All @@ -862,7 +862,7 @@ public function seeNumRecords(int $expectedNumber, string $table, array $criteri
'The number of found rows (%d) does not match expected number %d for criteria %s in table %s',
$actualNumber,
$expectedNumber,
json_encode($criteria, JSON_THROW_ON_ERROR),
json_encode($criteria, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE),
$table
)
);
Expand All @@ -874,7 +874,7 @@ public function dontSeeInDatabase(string $table, array $criteria = []): void
$this->assertLessThan(
1,
$count,
'Unexpectedly found matching records for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR) . ' in table ' . $table
'Unexpectedly found matching records for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE) . ' in table ' . $table
);
}

Expand Down
9 changes: 5 additions & 4 deletions tests/data/dumps/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ insert into `groups`(`id`,`name`,`enabled`,`created_at`) values (2,'jazzman',0,

CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` binary(16) DEFAULT NULL,
`name` varchar(30) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`is_active` bit(1) DEFAULT b'1',
Expand All @@ -24,13 +25,13 @@ CREATE TABLE `users` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (1,'davert','[email protected]', b'1','2012-02-01 21:17:04');
insert into `users`(`id`,`uuid`, `name`,`email`, `is_active`,`created_at`) values (1,0x11edc34b01d972fa9c1d0242ac120006,'davert','[email protected]', b'1','2012-02-01 21:17:04');

insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (2,'nick','[email protected]', b'1','2012-02-01 21:17:15');
insert into `users`(`id`,`uuid`, `name`,`email`, `is_active`,`created_at`) values (2,null,'nick','[email protected]', b'1','2012-02-01 21:17:15');

insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (3,'miles','[email protected]', b'1','2012-02-01 21:17:25');
insert into `users`(`id`,`uuid`, `name`,`email`, `is_active`,`created_at`) values (3,null,'miles','[email protected]', b'1','2012-02-01 21:17:25');

insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (4,'bird','[email protected]', b'0','2012-02-01 21:17:39');
insert into `users`(`id`,`uuid`, `name`,`email`, `is_active`,`created_at`) values (4,null,'bird','[email protected]', b'0','2012-02-01 21:17:39');



Expand Down
10 changes: 5 additions & 5 deletions tests/data/dumps/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ INSERT INTO "permissions" VALUES(5,3,2,'member');
INSERT INTO "permissions" VALUES(7,4,2,'admin');

DROP TABLE IF EXISTS "users";
CREATE TABLE "users" ("name" VARCHAR, "email" VARCHAR, "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP);
INSERT INTO "users" VALUES('davert','[email protected]','2012-02-01 21:17:04');
INSERT INTO "users" VALUES('nick','[email protected]','2012-02-01 21:17:15');
INSERT INTO "users" VALUES('miles','[email protected]','2012-02-01 21:17:25');
INSERT INTO "users" VALUES('bird','[email protected]','2012-02-01 21:17:39');
CREATE TABLE "users" ("name" VARCHAR, "uuid" BLOB DEFAULT NULL, "email" VARCHAR, "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP);
INSERT INTO "users" VALUES('davert',X'11edc34b01d972fa9c1d0242ac120006','[email protected]','2012-02-01 21:17:04');
INSERT INTO "users" VALUES('nick',null,'[email protected]','2012-02-01 21:17:15');
INSERT INTO "users" VALUES('miles',null,'[email protected]','2012-02-01 21:17:25');
INSERT INTO "users" VALUES('bird',null,'[email protected]','2012-02-01 21:17:39');

DROP TABLE IF EXISTS "empty_table";
CREATE TABLE "empty_table" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "field" VARCHAR);
Expand Down
Binary file modified tests/data/sqlite.db
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/unit/Codeception/Module/Db/AbstractDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,27 @@ public function testConnectionIsKeptForTheWholeSuite()
$this->module->_afterSuite();
}

public function testSeeInDatabaseWithBinary()
{
$this->module->seeInDatabase('users', ['uuid' => hex2bin('11edc34b01d972fa9c1d0242ac120006')]);
}

public function testSeeInDatabase()
{
$this->module->seeInDatabase('users', ['name' => 'davert']);
}

public function testCountInDatabase()
{
$this->module->seeNumRecords(1, 'users', ['uuid' => hex2bin('11edc34b01d972fa9c1d0242ac120006')]);
$this->module->seeNumRecords(1, 'users', ['name' => 'davert']);
$this->module->seeNumRecords(0, 'users', ['name' => 'davert', 'email' => '[email protected]']);
$this->module->seeNumRecords(0, 'users', ['name' => 'user1']);
}

public function testDontSeeInDatabase()
{
$this->module->dontSeeInDatabase('users', ['uuid' => hex2bin('ffffffffffffffffffffffffffffffff')]);
$this->module->dontSeeInDatabase('users', ['name' => 'user1']);
}

Expand Down

0 comments on commit 8dd6d2f

Please sign in to comment.