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 89b1215
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 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','davert@mail.ua', b'1','2012-02-01 21:17:04');
insert into `users`(`id`,`uuid`, `name`,`email`, `is_active`,`created_at`) values (1,0x11edc34b01d972fa9c1d0242ac120006,'davert','davert@mail.ua', b'1','2012-02-01 21:17:04');

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

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

insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (4,'bird','charlie@parker.com', b'0','2012-02-01 21:17:39');
insert into `users`(`id`,`uuid`, `name`,`email`, `is_active`,`created_at`) values (4,null,'bird','charlie@parker.com', b'0','2012-02-01 21:17:39');



Expand Down
12 changes: 7 additions & 5 deletions tests/data/dumps/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SET default_with_oids = false;
DROP TABLE IF EXISTS users CASCADE;
CREATE TABLE users (
name character varying(30),
uuid bytea,
email character varying(50),
created_at timestamp without time zone DEFAULT now(),
id integer NOT NULL
Expand Down Expand Up @@ -181,6 +182,7 @@ ALTER SEQUENCE permissions_id_seq OWNED BY permissions.id;
DROP TABLE IF EXISTS users CASCADE;
CREATE TABLE users (
name character varying(30),
uuid bytea,
email character varying(50),
created_at timestamp without time zone DEFAULT now(),
id integer NOT NULL
Expand Down Expand Up @@ -332,11 +334,11 @@ SELECT pg_catalog.setval('permissions_id_seq', 10, true);
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: -
--

COPY users (name, email, created_at, id) FROM stdin;
davert davert@mail.ua \N 1
nick nick@mail.ua 2012-02-02 22:30:31.748 2
miles miles@davis.com 2012-02-02 22:30:52.166 3
bird charlie@parker.com 2012-02-02 22:32:13.107 4
COPY users (name, uuid, email, created_at, id) FROM stdin;
davert \\x11edc34b01d972fa9c1d0242ac120006 davert@mail.ua \N 1
nick NULL nick@mail.ua 2012-02-02 22:30:31.748 2
miles NULL miles@davis.com 2012-02-02 22:30:52.166 3
bird NULL charlie@parker.com 2012-02-02 22:32:13.107 4
\.


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','davert@mail.ua','2012-02-01 21:17:04');
INSERT INTO "users" VALUES('nick','nick@mail.ua','2012-02-01 21:17:15');
INSERT INTO "users" VALUES('miles','miles@davis.com','2012-02-01 21:17:25');
INSERT INTO "users" VALUES('bird','charlie@parker.com','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','davert@mail.ua','2012-02-01 21:17:04');
INSERT INTO "users" VALUES('nick',null,'nick@mail.ua','2012-02-01 21:17:15');
INSERT INTO "users" VALUES('miles',null,'miles@davis.com','2012-02-01 21:17:25');
INSERT INTO "users" VALUES('bird',null,'charlie@parker.com','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' => 'xxx@yyy.zz']);
$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 89b1215

Please sign in to comment.