Skip to content

Commit 1858892

Browse files
cybdsm
andauthored
Fixed encoding of binary values in assertion messages
Co-authored-by: sm <[email protected]>
1 parent a5b0a19 commit 1858892

File tree

7 files changed

+34
-17
lines changed

7 files changed

+34
-17
lines changed

src/Codeception/Lib/Driver/Db.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ public function executeQuery($query, array $params): PDOStatement
294294
$type = PDO::PARAM_BOOL;
295295
} elseif (is_int($param)) {
296296
$type = PDO::PARAM_INT;
297+
} elseif ($this->isBinary($param)) {
298+
$type = PDO::PARAM_LOB;
297299
} else {
298300
$type = PDO::PARAM_STR;
299301
}
@@ -342,4 +344,9 @@ public function getOptions(): array
342344
{
343345
return $this->options;
344346
}
347+
348+
protected function isBinary(string $string): bool
349+
{
350+
return false === mb_detect_encoding($string, null, true);
351+
}
345352
}

src/Codeception/Module/Db.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ public function seeInDatabase(string $table, array $criteria = []): void
845845
$this->assertGreaterThan(
846846
0,
847847
$res,
848-
'No matching records found for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR) . ' in table ' . $table
848+
'No matching records found for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE) . ' in table ' . $table
849849
);
850850
}
851851

@@ -871,7 +871,7 @@ public function seeNumRecords(int $expectedNumber, string $table, array $criteri
871871
'The number of found rows (%d) does not match expected number %d for criteria %s in table %s',
872872
$actualNumber,
873873
$expectedNumber,
874-
json_encode($criteria, JSON_THROW_ON_ERROR),
874+
json_encode($criteria, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE),
875875
$table
876876
)
877877
);
@@ -883,7 +883,7 @@ public function dontSeeInDatabase(string $table, array $criteria = []): void
883883
$this->assertLessThan(
884884
1,
885885
$count,
886-
'Unexpectedly found matching records for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR) . ' in table ' . $table
886+
'Unexpectedly found matching records for criteria ' . json_encode($criteria, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE) . ' in table ' . $table
887887
);
888888
}
889889

tests/data/dumps/mysql.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ insert into `groups`(`id`,`name`,`enabled`,`created_at`) values (2,'jazzman',0,
1616

1717
CREATE TABLE `users` (
1818
`id` int(11) NOT NULL AUTO_INCREMENT,
19+
`uuid` binary(16) DEFAULT NULL,
1920
`name` varchar(30) DEFAULT NULL,
2021
`email` varchar(255) DEFAULT NULL,
2122
`is_active` bit(1) DEFAULT b'1',
@@ -24,13 +25,13 @@ CREATE TABLE `users` (
2425
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2526

2627

27-
insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (1,'davert','[email protected]', b'1','2012-02-01 21:17:04');
28+
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');
2829

29-
insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (2,'nick','[email protected]', b'1','2012-02-01 21:17:15');
30+
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');
3031

31-
insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (3,'miles','[email protected]', b'1','2012-02-01 21:17:25');
32+
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');
3233

33-
insert into `users`(`id`,`name`,`email`, `is_active`,`created_at`) values (4,'bird','[email protected]', b'0','2012-02-01 21:17:39');
34+
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');
3435

3536

3637

tests/data/dumps/postgres.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ SET default_with_oids = false;
2828
DROP TABLE IF EXISTS users CASCADE;
2929
CREATE TABLE users (
3030
name character varying(30),
31+
uuid bytea,
3132
email character varying(50),
3233
created_at timestamp without time zone DEFAULT now(),
3334
id integer NOT NULL
@@ -181,6 +182,7 @@ ALTER SEQUENCE permissions_id_seq OWNED BY permissions.id;
181182
DROP TABLE IF EXISTS users CASCADE;
182183
CREATE TABLE users (
183184
name character varying(30),
185+
uuid bytea,
184186
email character varying(50),
185187
created_at timestamp without time zone DEFAULT now(),
186188
id integer NOT NULL
@@ -332,11 +334,11 @@ SELECT pg_catalog.setval('permissions_id_seq', 10, true);
332334
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: -
333335
--
334336

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

342344

tests/data/dumps/sqlite.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ INSERT INTO "permissions" VALUES(5,3,2,'member');
1111
INSERT INTO "permissions" VALUES(7,4,2,'admin');
1212

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

2020
DROP TABLE IF EXISTS "empty_table";
2121
CREATE TABLE "empty_table" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "field" VARCHAR);

tests/data/sqlite.db

0 Bytes
Binary file not shown.

tests/unit/Codeception/Module/Db/AbstractDbTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,27 @@ public function testConnectionIsKeptForTheWholeSuite()
6464
$this->module->_afterSuite();
6565
}
6666

67+
public function testSeeInDatabaseWithBinary()
68+
{
69+
$this->module->seeInDatabase('users', ['uuid' => hex2bin('11edc34b01d972fa9c1d0242ac120006')]);
70+
}
71+
6772
public function testSeeInDatabase()
6873
{
6974
$this->module->seeInDatabase('users', ['name' => 'davert']);
7075
}
7176

7277
public function testCountInDatabase()
7378
{
79+
$this->module->seeNumRecords(1, 'users', ['uuid' => hex2bin('11edc34b01d972fa9c1d0242ac120006')]);
7480
$this->module->seeNumRecords(1, 'users', ['name' => 'davert']);
7581
$this->module->seeNumRecords(0, 'users', ['name' => 'davert', 'email' => '[email protected]']);
7682
$this->module->seeNumRecords(0, 'users', ['name' => 'user1']);
7783
}
7884

7985
public function testDontSeeInDatabase()
8086
{
87+
$this->module->dontSeeInDatabase('users', ['uuid' => hex2bin('ffffffffffffffffffffffffffffffff')]);
8188
$this->module->dontSeeInDatabase('users', ['name' => 'user1']);
8289
}
8390

0 commit comments

Comments
 (0)