Skip to content

Commit

Permalink
Add e2e tests, extend version range in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
reimic committed Mar 22, 2024
1 parent 557dd5e commit 000b13f
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
php: [ '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ]
dependency_version: [ prefer-stable ]

name: ${{ matrix.os }} - PHP ${{ matrix.php }} - ${{ matrix.dependency_version }}
Expand Down
5 changes: 1 addition & 4 deletions composer-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "*",
"phpunit/phpunit": "6.5.14",
"squizlabs/php_codesniffer": "*",
"nette/php-generator": "*",
"jane-php/json-schema": "*",
Expand All @@ -20,9 +20,6 @@
"php-http/discovery": true,
"bamarni/composer-bin-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
},
"platform": {
"php": "7.4"
}
},
"autoload": {
Expand Down
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
8 changes: 4 additions & 4 deletions tests/Blueprints/Runner/Step/RmStepRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ class RmStepRunnerTest extends TestCase
/**
* @var string
*/
private string $documentRoot;
private $documentRoot;

/**
* @var Runtime
*/
private Runtime $runtime;
private $runtime;

/**
* @var RmStepRunner
*/
private RmStepRunner $step;
private $step;

/**
* @var Filesystem
*/
private Filesystem $fileSystem;
private $fileSystem;

/**
* @before
Expand Down
10 changes: 5 additions & 5 deletions tests/Blueprints/Runner/Step/UnzipStepRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ class UnzipStepRunnerTest extends TestCase {
/**
* @var string $document_root
*/
private string $document_root;
private $document_root;

/**
* @var Runtime $runtime
*/
private Runtime $runtime;
private $runtime;

/**
* @var UnzipStepRunner $step
*/
private UnzipStepRunner $step;
private $step;

/**
* @var Filesystem
*/
private Filesystem $file_system;
private $file_system;

/**
* @var Stub
*/
private Stub $resource_manager;
private $resource_manager;

/**
* @before
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 PHPUnit\Framework\TestCase;
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 TestCase {
/**
* @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 PHPUnit\Framework\TestCase;
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 TestCase {
/**
* @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 );
}
}
13 changes: 7 additions & 6 deletions tests/JsonMapper/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 000b13f

Please sign in to comment.