Skip to content

Commit

Permalink
Add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reimic committed Mar 22, 2024
1 parent 228977b commit e613c47
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/WordPress/Blueprints/Model/BlueprintBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class BlueprintBuilder {

public function __construct() {
$this->blueprint = new Blueprint();
$this->blueprint->setConstants( new \ArrayObject() );
$this->blueprint->setSiteOptions( new \ArrayObject() );
}

public static function create() {
Expand Down
3 changes: 1 addition & 2 deletions src/WordPress/Blueprints/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ function run_blueprint( $json, $options = array() ) {
$environment,
new Runtime( $documentRoot )
);

/** @var $engine Engine */
$engine = $c['blueprint.engine'];
$compiledBlueprint = $engine->parseAndCompile( $json );

/** @var $engine Engine */
if ( $progressSubscriber ) {
if ( $progressType === 'steps' ) {
$compiledBlueprint->stepsProgressStage->events->addSubscriber( $progressSubscriber );
Expand Down
123 changes: 123 additions & 0 deletions tests/e2e/JsonBlueprintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace e2e;

use E2ETestCase;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use WordPress\Blueprints\ContainerBuilder;
use WordPress\Blueprints\Progress\DoneEvent;
use WordPress\Blueprints\Progress\ProgressEvent;
use function WordPress\Blueprints\run_blueprint;

class JsonBlueprintTest extends E2ETestCase {
/**
* @var string
*/
private $document_root;

/**
* @var EventSubscriberInterface
*/
private $subscriber;

/**
* @before
*/
public function before() {
$this->document_root = Path::makeAbsolute( 'test', sys_get_temp_dir() );

$this->subscriber = new class() implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return array(
ProgressEvent::class => 'onProgress',
DoneEvent::class => 'onDone',
);
}

protected $progress_bar;

public function __construct() {
ProgressBar::setFormatDefinition( 'custom', ' [%bar%] %current%/%max% -- %message%' );

$this->progress_bar = ( new SymfonyStyle(
new StringInput( '' ),
new ConsoleOutput()
) )->createProgressBar( 100 );
$this->progress_bar->setFormat( 'custom' );
$this->progress_bar->setMessage( 'Start' );
$this->progress_bar->start();
}

public function onProgress( ProgressEvent $event ) {
$this->progress_bar->setMessage( $event->caption );
$this->progress_bar->setProgress( (int) $event->progress );
}

public function onDone( DoneEvent $event ) {
$this->progress_bar->finish();
}
};
}

/**
* @after
*/
public function after() {
( new Filesystem() )->remove( $this->document_root );
}
public function testRunningJsonBlueprintWithWordPressVersion() {
$blueprint = '{"WordPressVersion":"https://wordpress.org/latest.zip"}';

$results = run_blueprint(
$blueprint,
array(
'environment' => ContainerBuilder::ENVIRONMENT_NATIVE,
'documentRoot' => $this->document_root . '/new-wp',
'progressSubscriber' => $this->subscriber,
)
);

// fails at downloadWordPress

$expected = array(
// 0 => new StepSuccess(),
// 1 => new StepSuccess(),
// 2 => new StepSuccess(),
// 3 => new StepSuccess(),
);

// @TODO fix expected
$this->assertEquals( $expected, $results );
}

public function testRunningJsonBlueprintWithSteps() {
$blueprint = '{"steps":[{"step":"mkdir","path":"dir"},{"step": "rm","path": "dir"}]}';

$results = run_blueprint(
$blueprint,
array(
'environment' => ContainerBuilder::ENVIRONMENT_NATIVE,
'documentRoot' => $this->document_root . '/new-wp',
'progressSubscriber' => $this->subscriber,
)
);

// fails at defineWpConfigConsts

$expected = array(
// 0 => new StepSuccess(),
// 1 => new StepSuccess(),
// 2 => new StepSuccess(),
// 3 => new StepSuccess(),
);

// @TODO fix expected
$this->assertEquals( $expected, $results );
}
}
125 changes: 125 additions & 0 deletions tests/e2e/PhpBlueprintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace e2e;

use E2ETestCase;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use WordPress\Blueprints\Compile\StepSuccess;
use WordPress\Blueprints\ContainerBuilder;
use WordPress\Blueprints\Model\BlueprintBuilder;
use WordPress\Blueprints\Model\DataClass\MkdirStep;
use WordPress\Blueprints\Model\DataClass\RmStep;
use WordPress\Blueprints\Progress\DoneEvent;
use WordPress\Blueprints\Progress\ProgressEvent;
use function WordPress\Blueprints\run_blueprint;

class PhpBlueprintTest extends E2ETestCase {
/**
* @var string
*/
private $document_root;

/**
* @var EventSubscriberInterface
*/
private $subscriber;

/**
* @before
*/
public function before() {
$this->document_root = Path::makeAbsolute( 'test', sys_get_temp_dir() );

$this->subscriber = new class() implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return array(
ProgressEvent::class => 'onProgress',
DoneEvent::class => 'onDone',
);
}

protected $progress_bar;

public function __construct() {
ProgressBar::setFormatDefinition( 'custom', ' [%bar%] %current%/%max% -- %message%' );

$this->progress_bar = ( new SymfonyStyle(
new StringInput( '' ),
new ConsoleOutput()
) )->createProgressBar( 100 );
$this->progress_bar->setFormat( 'custom' );
$this->progress_bar->setMessage( 'Start' );
$this->progress_bar->start();
}

public function onProgress( ProgressEvent $event ) {
$this->progress_bar->setMessage( $event->caption );
$this->progress_bar->setProgress( (int) $event->progress );
}

public function onDone( DoneEvent $event ) {
$this->progress_bar->finish();
}
};
}

/**
* @after
*/
public function after() {
( new Filesystem() )->remove( $this->document_root );
}

public function testRunningPhpBlueprintWithWordPressVersion() {
$blueprint = BlueprintBuilder::create()
->withWordPressVersion( 'https://wordpress.org/latest.zip' )
->toBlueprint();

$results = run_blueprint(
$blueprint,
array(
'environment' => ContainerBuilder::ENVIRONMENT_NATIVE,
'documentRoot' => $this->document_root . '/new-wp',
'progressSubscriber' => $this->subscriber,
)
);

$expected = array();

// @TODO fix expected
$this->assertEquals( $expected, $results );
}

public function testRunningPhpBlueprintWithSteps() {
$blueprint = BlueprintBuilder::create()
->addStep( ( new MkdirStep() )->setPath( 'dir1' ) )
->addStep( ( new RmStep() )->setPath( 'dir1' ) )
->addStep( ( new MkdirStep() )->setPath( 'dir2' ) )
->toBlueprint();

$results = run_blueprint(
$blueprint,
array(
'environment' => ContainerBuilder::ENVIRONMENT_NATIVE,
'documentRoot' => $this->document_root . '/new-wp',
'progressSubscriber' => $this->subscriber,
)
);

$expected = array();
array(
0 => new StepSuccess( new MkdirStep(), true ),
1 => new StepSuccess( new RmStep(), true ),
2 => new StepSuccess( new MkdirStep(), true ),
);

// @TODO fix expected
$this->assertEquals( $expected, $results );
}
}
5 changes: 5 additions & 0 deletions tests/e2e/configuration/E2ETestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Yoast\PHPUnitPolyfills\TestCases\XTestCase as TestCase;

abstract class E2ETestCase extends TestCase {}
13 changes: 7 additions & 6 deletions tests/unit/json_mapper/JsonMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ public function before() {
}

public function testCustomFactory() {
$mapper = new JsonMapper( array(
$custom_factories = array(
Item::class => function ( $json ) {
$item = new Item();
$item->name = $json->name;

return $item;
},
) );

$result = $mapper->hydrate(
json_decode( '{"name":"test","items":[{"name":"test"}]}' ),
Bag::class
);

$mapper = new JsonMapper($custom_factories);

$raw_json = '{"name":"test","items":[{"name":"test"}]}';

$result = $mapper->hydrate( json_decode( $raw_json ), Bag::class );

$expected = new Bag();
$expected->name = 'test';
$expected->items = [ new Item() ];
Expand Down

0 comments on commit e613c47

Please sign in to comment.