Hi everyone,
I’m struggling a bit with writing a unit test for an app I am developing.
Concretely, I have several tables that I create in a child class of SimpleMigrationStep linked with foreign keys (like it is also indicated in the App Development Tutorial).
For a unit test I do not want to use mockups, but would like to create a SQLite DB as test fixture. Of course I could create all the tables also in my test setup (more or less manually) with PDO, but I do not want to repeat myself and risk to make errors/differences to how my tables are defined in the SimpleMigrationStep
.
So I simply would like to reuse the SimpleMigrationStep’s changeSchema
method in my test fixture.
However, the changeSchema method needs the parameter schemaClosure
of type Closure
implementing the ISchemaWrapper
interface.
I checked the Nextcloud GitHub repository as well as the repositories of other apps. But I am not able to find how I can properly instantiate a SchemaWrapper
, Connection
or ConnectionFactory
by injecting a PDO object that I create.
The following code does not work, but should illustrate what I want to achieve. The test fixture I will then reuse in the setup method of my Unit test.
<?php
namespace OCA\myApp\tests\Unit;
use PDO;
require_once __DIR__ . '/../../lib/Migration/Version000100Date20230121122000.php';
use OCA\myApp\Migration\Version000100Date20230121122000;
use OCA\DB\ConnectionFactory;
class TestFixture {
# ---------------------------------------------------------------------------
private function createSchema(): PDO {
print('create new PDO object');
$pdo = new PDO('sqlite::memory:');
print('PDO object created');
$migration = new Version000100Date20230121122000();
print('CREATED new Migration');
# we cannot give a pdo directly, but have to create a schemaClosure, how?
$connection = new ConnectionFactory($pdo);
$schemaClosure = new SchemaWrapper($connection)
return $migration->createSchema($schemaClosure, []);
}
}
The following is a snippet of my SimpleMigrationStep:
class Version000100Date20230121122000 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param ISchemaWrapper $schemaISchemaWrapper
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
$this->createSchema($schemaClosure, $options);
}
public function createSchema(Closure $schemaClosure, array $options) {
$schema = $schemaClosure();
....
}
Any help is appreciated!
Thanks in advance!
Sven
PS: Of course I also checked the Community if a similar topic already exists but couldn’t find anything matching.