S
S
Stanislav2020-08-13 12:14:31
PHP
Stanislav, 2020-08-13 12:14:31

How to make Zend+SQLite+PHPUnit work with JSON?

The problem is the following. In production, we use PostgreSQL, which supports the JSON data type. Everything is great with it in tests, if we work with Symfony + Doctrine - it has its own custom types, which in tests do not require that the field in the database be specified as JSON. In other words, in tests we use SQLite, but for it Doctrine itself sees that it does not support JSON, creates fields of type string when mapping tables, and then works with them as if it were JSON, decoding or encoding it back and forth, where it is required.

But when we start working with Zend Expressive 3 + zendframework/zend-db, the problems start. We manually write the database config for SQLite in tests, where we cannot specify a JSON type field, and we have to use string. As a result, code like:

$row = $this->tableGateway->select(
  static function (Select $select) use ($blah) {
    $select->where->nest
      ->equalTo('blah', $blah)
      ->or
      ->equalTo('sum, 1);
  }
);

we will return an array in which the field, which is marked in the config as string, but which contains some JSON, will be a string. The application code, of course, does not expect this and crashes. In other words, the application code crashes with a TypeError, or, for example, with "Illegal string offset 'holder'" - that is, the application, accessing the supposedly array through "$arr['holder']", actually accesses the string, in which has JSON and in it an object with a "holder" field.

Question: How to make SQLite learn to work with JSON and zendframework / zend-db understand this and as well as give the fields we need as arrays (results of JSON conversion), and not as strings containing JSON, as well as accept arrays and write them in DB as JSON?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question