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 30, 2024
1 parent 228977b commit c7b1723
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 70 deletions.
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"prefer-stable": true,
"require": {
"ext-json": "*",
"php": ">=7.0"
"ext-json": "*"
},
"require-dev": {
"yoast/phpunit-polyfills": "2.0.0"
Expand All @@ -20,9 +19,9 @@
"Pimple\\": "src/pimple/pimple/src/Pimple",
"Psr\\Container\\": "src/psr/container",
"Psr\\SimpleCache\\": "src/psr/simple-cache",
"Symfony\\Component\\Process\\": "src/Symfony/process",
"Symfony\\Component\\EventDispatcher\\": "src/Symfony/event-dispatcher",
"Symfony\\Component\\filesystem\\": "src/Symfony/filesystem",
"Symfony\\Component\\Process\\": "src/symfony/process",
"Symfony\\Component\\EventDispatcher\\": "src/symfony/event-dispatcher",
"Symfony\\Component\\filesystem\\": "src/symfony/filesystem",
"Opis\\JsonSchema\\": "src/opis/json-schema/src",
"Opis\\String\\": "src/opis/string/src",
"Opis\\Uri\\": "src/opis/uri/src"
Expand All @@ -38,5 +37,8 @@
"classmap": [
"tests/"
]
},
"scripts": {
"test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit --testdox" ]
}
}
6 changes: 3 additions & 3 deletions dev/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"Pimple\\": "../src/pimple/pimple/src/Pimple",
"Psr\\Container\\": "../src/psr/container",
"Psr\\SimpleCache\\": "../src/psr/simple-cache",
"Symfony\\Component\\Process\\": "../src/Symfony/process",
"Symfony\\Component\\EventDispatcher\\": "../src/Symfony/event-dispatcher",
"Symfony\\Component\\Filesystem\\": "../src/Symfony/filesystem",
"Symfony\\Component\\Process\\": "../src/symfony/process",
"Symfony\\Component\\EventDispatcher\\": "../src/symfony/event-dispatcher",
"Symfony\\Component\\Filesystem\\": "../src/symfony/filesystem",
"Opis\\JsonSchema\\": "../src/opis/json-schema/src",
"Opis\\String\\": "../src/opis/string/src",
"Opis\\Uri\\": "../src/opis/uri/src"
Expand Down
37 changes: 9 additions & 28 deletions examples/json_string_compiling.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,34 @@
<?php

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 WordPress\Blueprints\ContainerBuilder;
use WordPress\Blueprints\Progress\DoneEvent;
use WordPress\Blueprints\Progress\ProgressEvent;
use function WordPress\Blueprints\run_blueprint;

if ( getenv( 'USE_PHAR' ) ) {
require __DIR__ . '/dist/blueprints.phar';
// require __DIR__ . './../dist/blueprints.phar';
} else {
require 'vendor/autoload.php';
require './../vendor/autoload.php';
}

$blueprint = '{"WordPressVersion":"https://wordpress.org/latest.zip","steps":[{"step":"mkdir","path":"dir"},{"step": "rm","path": "dir"}]}';
$blueprint = '{"WordPressVersion":"https://wordpress.org/latest.zip"}';

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

protected $progress_bar;

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

$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 onProgress( ProgressEvent $event ) {}

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

$results = run_blueprint(
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
bootstrap="vendor/autoload.php"
colors="true"
beStrictAboutOutputDuringTests="true"
cacheResultFile=".cache"
>
<testsuites>
<testsuite name="default">
Expand Down
4 changes: 1 addition & 3 deletions src/WordPress/AsyncHttp/async_http_streams.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ function stream_http_prepare_request_bytes( $url ) {
REQUEST;

// @TODO: Add support for Accept-Encoding: gzip

return str_replace( "\n", "\r\n", $request ) . "\r\n\r\n";
return $request . "\r\n\r\n";
}

/**
Expand Down Expand Up @@ -262,7 +261,6 @@ function streams_http_response_await_headers( $streams ) {
foreach ( $headers as $k => $header ) {
$headers[ $k ] = parse_http_headers( $header );
}

return $headers;
}

Expand Down
4 changes: 2 additions & 2 deletions src/WordPress/Blueprints/Compile/BlueprintCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function expandShorthandsIntoSteps( $blueprint ) {
$additional_steps[] = ( new RunWordPressInstallerStep() )
->setOptions( new WordPressInstallationOptions() );
}
if ( $blueprint->constants ) {
if ( $blueprint->constants && $blueprint->constants->count() > 0 ) {
$step = new DefineWpConfigConstsStep();
$step->consts = $blueprint->constants;
$additional_steps[] = $step;
Expand All @@ -84,7 +84,7 @@ protected function expandShorthandsIntoSteps( $blueprint ) {
$additional_steps[] = $step;
}
}
if ( $blueprint->siteOptions ) {
if ( $blueprint->siteOptions && $blueprint->siteOptions->count() > 0) {
$step = new SetSiteOptionsStep();
$step->setOptions( $blueprint->siteOptions );
$additional_steps[] = $step;
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
17 changes: 11 additions & 6 deletions src/WordPress/Blueprints/Runner/Step/MkdirStepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

namespace WordPress\Blueprints\Runner\Step;

use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use WordPress\Blueprints\BlueprintException;
use WordPress\Blueprints\Model\DataClass\MkdirStep;


class MkdirStepRunner extends BaseStepRunner {

/**
* @param \WordPress\Blueprints\Model\DataClass\MkdirStep $input
* @param MkdirStep $input
*/
function run( $input ) {
// @TODO: Treat $input->path as relative path to the document root (unless it's absolute)
$success = mkdir( $input->path );
if ( ! $success ) {
throw new \Exception( "Failed to create directory at {$input->path}" );
function run( MkdirStep $input ) {
$resolvedPath = $this->getRuntime()->resolvePath( $input->path );
$fileSystem = new Filesystem();
try {
$fileSystem->mkdir( $resolvedPath );
} catch ( IOException $exception ) {
throw new BlueprintException( "Failed to create a directory at \"$resolvedPath\"", 0, $exception );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WordPress\Blueprints\Runner\Step;

use PHPUnit\Exception;
use WordPress\Blueprints\Model\DataClass\RunWordPressInstallerStep;
use WordPress\Blueprints\Progress\Tracker;

Expand All @@ -11,21 +12,28 @@ class RunWordPressInstallerStepRunner extends BaseStepRunner {
* @param \WordPress\Blueprints\Progress\Tracker $tracker
*/
function run( $input, $tracker ) {
return $this->getRuntime()->runShellCommand(
array(
'php',
'wp-cli.phar',
'--allow-root',
'core',
'install',
'--url=http://localhost:8081',
'--title=Playground Site',
'--admin_user=' . $input->options->adminUsername,
'--admin_password=' . $input->options->adminPassword,
'[email protected]',
),
$this->getRuntime()->getDocumentRoot()
);
var_dump( $input );
try {
$result = $this->getRuntime()->runShellCommand(
array(
'php',
'wp-cli.phar',
'--allow-root',
'core',
'install',
'--url=http://localhost:8081',
'--title=Playground Site',
'--admin_user=' . $input->options->adminUsername,
'--admin_password=' . $input->options->adminPassword,
'[email protected]',
),
$this->getRuntime()->getDocumentRoot()
);
return trim( $result );
} catch ( Exception $exception ) {
var_dump( $exception );
}
return null;
}

public function getDefaultCaption( $input ) {
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
119 changes: 119 additions & 0 deletions tests/e2e/JsonBlueprintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace e2e;

use e2e\resources\TestResourceClassSimpleSubscriber;
use E2ETestCase;
use Opis\JsonSchema\Filters\DataExistsFilter;
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\DataClass\DefineWpConfigConstsStep;
use WordPress\Blueprints\Model\DataClass\DownloadWordPressStep;
use WordPress\Blueprints\Model\DataClass\InstallSqliteIntegrationStep;
use WordPress\Blueprints\Model\DataClass\MkdirStep;
use WordPress\Blueprints\Model\DataClass\RmStep;
use WordPress\Blueprints\Model\DataClass\RunWordPressInstallerStep;
use WordPress\Blueprints\Model\DataClass\SetSiteOptionsStep;
use WordPress\Blueprints\Model\DataClass\UrlResource;
use WordPress\Blueprints\Model\DataClass\WordPressInstallationOptions;
use WordPress\Blueprints\Model\DataClass\WriteFileStep;
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 TestResourceClassSimpleSubscriber();
}

/**
* @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,
'progressSubscriber' => $this->subscriber,
)
);

$word_press_zip = ( new UrlResource() )
->setResource( 'url' )
->setUrl('https://wordpress.org/latest.zip');
$download_word_press_step = ( new DownloadWordPressStep() )
->setWordPressZip( $word_press_zip );

$sqlite_plugin_zip = ( new UrlResource() )
->setResource('url' )
->setUrl( 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip' );
$install_sqlite_integration_step = ( new InstallSqliteIntegrationStep() )
->setSqlitePluginZip( $sqlite_plugin_zip );

$wp_cli = ( new UrlResource() )
->setResource('url' )
->setUrl('https://playground.wordpress.net/wp-cli.phar' );
$write_file_step = ( new WriteFileStep() )
->setPath( 'wp-cli.phar' )
->setData( $wp_cli );

$run_word_press_installer_step = ( new RunWordPressInstallerStep() )
->setOptions( new WordPressInstallationOptions() );

$expected = array(
0 => new StepSuccess( $download_word_press_step, null ),
1 => new StepSuccess( $install_sqlite_integration_step, null ),
2 => new StepSuccess( $write_file_step, null ),
3 => new StepSuccess( $run_word_press_installer_step, 'Success: WordPress installed successfully.' )
);

//@TODO Assert WP files exist

self::assertEquals( $expected, $results );
}

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

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

$expected = array(
0 => new StepSuccess( ( new MkdirStep() )->setPath( 'dir1' ), null ),
1 => new StepSuccess( ( new RmStep() )->setPath( 'dir1' ), null ),
2 => new StepSuccess( ( new MkdirStep() )->setPath( 'dir2' ), null ),
);

self::assertDirectoryDoesNotExist( Path::makeAbsolute( 'dir1', $this->document_root ) );
self::assertDirectoryExists( Path::makeAbsolute( 'dir2', $this->document_root ) );
self::assertEquals( $expected, $results );
}
}
Loading

0 comments on commit c7b1723

Please sign in to comment.