Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Nov 9, 2020
1 parent a244b79 commit faca342
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 22 deletions.
7 changes: 4 additions & 3 deletions Core/Executor/MigrationDefinitionExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Kaliop\eZMigrationBundle\Core\Executor;

use JmesPath\Env as JmesPath;
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException;
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
use Kaliop\eZMigrationBundle\API\MatcherInterface;
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
use Kaliop\eZMigrationBundle\API\MigrationGeneratorInterface;
use JmesPath\Env as JmesPath;
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
use Symfony\Component\Yaml\Yaml;

class MigrationDefinitionExecutor extends AbstractExecutor
Expand Down Expand Up @@ -121,6 +121,7 @@ public function save($dsl, $context)
} else {
$definition = $dsl['migration_steps'];
}

$definition = $this->resolveReferencesRecursively($definition);

$fileName = $this->referenceResolver->resolveReference($dsl['file']);
Expand Down
69 changes: 50 additions & 19 deletions Core/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php


namespace Kaliop\eZMigrationBundle\Core\Executor;

use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface;
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException;
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\VarDumper\VarDumper;
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface;

class ReferenceExecutor extends AbstractExecutor
{
Expand Down Expand Up @@ -62,8 +63,12 @@ protected function set($dsl, $context)
throw new InvalidStepDefinitionException("Invalid step definition: miss 'value' for setting reference");
}

// this makes sense since we started supporting embedded refs...
$value = $this->referenceResolver->resolveReference($dsl['value']);
if (!isset($dsl['resolve_references']) || $dsl['resolve_references']) {
// this makes sense since we started supporting embedded refs...
$value = $this->referenceResolver->resolveReference($dsl['value']);
} else {
$value = $dsl['value'];
}

if (0 === strpos($value, '%env(') && ')%' === substr($value, -2) && '%env()%' !== $value) {
/// @todo find out how to use Sf components to resolve this value instead of doing it by ourselves...
Expand Down Expand Up @@ -188,24 +193,50 @@ protected function dump($dsl, $context)
if (!$this->referenceResolver->isReference($dsl['identifier'])) {
throw new \Exception("Invalid step definition: identifier '{$dsl['identifier']}' is not a reference");
}
/// @todo improve handling of the case $context['output'] is set - atm it does not seem to work with unit tests...
if (isset($context['output']) && $context['output'] instanceof OutputInterface) {
if ($context['output']->isQuiet()) {
return $this->referenceResolver->resolveReference($dsl['identifier']);
}
ob_start();
if (isset($context['output']) && $context['output'] instanceof OutputInterface && $context['output']->isQuiet()) {
return $this->referenceResolver->resolveReference($dsl['identifier']);
}

if (isset($dsl['label'])) {
echo $dsl['label'];
$label = $dsl['label'];
} else {
VarDumper::dump($dsl['identifier']);
$label = $this->dumpVar($dsl['identifier']);
}
$value = $this->referenceResolver->resolveReference($dsl['identifier']);
VarDumper::dump($value);
$value = $this->dumpVar($this->referenceResolver->resolveReference($dsl['identifier']));

if (isset($context['output']) && $context['output'] instanceof OutputInterface) {
$context['output']->write(ob_get_contents(), false, OutputInterface::OUTPUT_RAW|OutputInterface::VERBOSITY_NORMAL);
ob_end_clean();
$context['output']->write($label . $value, false, OutputInterface::OUTPUT_RAW|OutputInterface::VERBOSITY_NORMAL);
} else {
echo $label . $value;
}

return $value;
}

/**
* Similar to VarDumper::dump(), but returns the output
* @param mixed $var
* @return string
* @throws \ErrorException
*/
protected function dumpVar($var)
{
$cloner = new VarCloner();
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
$output = '';

$dumper->dump(
$cloner->cloneVar($var),
function ($line, $depth) use (&$output) {
// A negative depth means "end of dump"
if ($depth >= 0) {
// Adds a two spaces indentation to the line
/// @todo should we use NBSP for html dumping?
$output .= str_repeat(' ', $depth) . $line . "\n";
}
}
);

return $output;
}
}
3 changes: 3 additions & 0 deletions Resources/doc/DSL/MigrationDefinitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
match: { location_id: 2 }
attributes: { }
- etc...
# If you want to use the string 'reference:something' in the _generated_ migration, at the moment you need to
# use a workaround: define a reference in the current migration which resolves to the string 'reference:something',
# as is done in the test file UnitTestOk038_migrationdefinition.yml
file: string # path to the file to be generated. Mandatory.
# The filename must end with an extension of either .yml or .json
if: # Optional. If set, the migration step will be skipped unless the condition is matched
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/DSL/References.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# It can be an environment variable by using the syntax '%env(name)%'
# NB: in this case variables set by Symfony's .env mechanism are not supported
overwrite: true|false # Optional, default false. If not set, and the reference already exists, an exception is thrown
resolve_references: true|false # Optional, default true. Use this to avoid reference resolution when 'value' is string

-
# Load a whole set of references from a json or yaml file
Expand Down
47 changes: 47 additions & 0 deletions Tests/dsl/misc/UnitTestOK038_migrationdefinition.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-
type: reference
mode: set
identifier: kmb_038_dirty_hack
value: 'reference:kmb_038_matched_objs'
resolve_references: false

-
type: migration_definition
mode: save
file: /tmp/unit_test_generated_migration_038_1.json
migration_steps:
-
type: content
mode: load
match: { location_id: 2 }
references:
-
identifier: kmb_038_matched_objs
attribute: count
-
type: assert
target: reference
identifier: reference:kmb_038_dirty_hack
test:
equals: 1
-
type: reference
mode: dump
identifier: reference:kmb_038_dirty_hack
allow_null_results: true

### @todo generate a content-create migration instead, to be then deleted ?

-
type: migration_definition
mode: generate
file: /tmp/unit_test_generated_migration_038_2.json
migration_type: content_type
migration_mode: update
match:
type: contenttype_identifier
value: folder
references:
-
identifier: kmb_038_matched_cti
attribute: '[0].match.contenttype_identifier'
26 changes: 26 additions & 0 deletions Tests/phpunit/04_MigrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ public function testSetRef()
$this->deleteMigration($filePath);
}

public function testMigrationGeneration()
{
$filePath = $this->dslDir.'/misc/UnitTestOK038_migrationdefinition.yml';
$filePath2 = '/tmp/unit_test_generated_migration_038_1.json';
$filePath3 = '/tmp/unit_test_generated_migration_038_2.json';

// Make sure migration is not in the db: delete it, ignoring errors
$this->prepareMigration($filePath);
$output = $this->runCommand('kaliop:migration:migrate', array('--path' => array($filePath), '-n' => true, '-u' => true));

$this->prepareMigration($filePath2);
$output = $this->runCommand('kaliop:migration:migrate', array('--path' => array($filePath2), '-n' => true, '-u' => true));

$this->assertContains('kmb_038_matched_objs', $output);

// this one we can not execute - we only parse for correctness
$this->prepareMigration($filePath3);

$this->deleteMigration($filePath);
$this->deleteMigration($filePath2);
$this->deleteMigration($filePath3);

unlink($filePath2);
unlink($filePath3);
}

public function testCancelledExecution()
{
$filePath = $this->dslDir.'/misc/UnitTestOK019_cancel.yml';
Expand Down
9 changes: 9 additions & 0 deletions WHATSNEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ Version 5.13.0
identifier: myReference
value: '%env(PWD)%'

* New: : taught the `reference/set` migration step _not_ to resolve environment variables at all, eg:

-
type: reference
mode: set
identifier: a_funny_string
value: 'reference:or_not_to_reference'
resolve_references: false

* New: taught the SQL migration step, when specified in yaml format, to resolve references embedded in the sql statement
(issue #199), eg:

Expand Down

0 comments on commit faca342

Please sign in to comment.