Skip to content

Commit

Permalink
Merge pull request #133 from woocommerce/24-02/custom-e2e-tests
Browse files Browse the repository at this point in the history
Custom E2E Tests 1 - Renaming `Environment` to `Backend`
  • Loading branch information
Luc45 authored Mar 14, 2024
2 parents fa16baa + d3b9cf4 commit 23e4000
Show file tree
Hide file tree
Showing 25 changed files with 502 additions and 519 deletions.
26 changes: 13 additions & 13 deletions src/src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
namespace QIT_CLI;

class Auth {
/** @var Environment $environment */
protected $environment;
/** @var Cache $cache */
protected $cache;

public function __construct( Environment $environment ) {
$this->environment = $environment;
public function __construct( Cache $cache ) {
$this->cache = $cache;
}

/**
* @return string|null base64 encoded string of user:application_password, or null if not defined.
*/
public function get_partner_auth() {
// Migrate "application_password" to "qit_token" if it exists.
if ( empty( $this->environment->get_cache()->get( 'qit_token' ) ) && ! empty( $this->environment->get_cache()->get( 'application_password' ) ) ) {
$this->environment->get_cache()->set( 'qit_token', $this->environment->get_cache()->get( 'application_password' ), - 1 );
if ( empty( $this->cache->get( 'qit_token' ) ) && ! empty( $this->cache->get( 'application_password' ) ) ) {
$this->cache->set( 'qit_token', $this->cache->get( 'application_password' ), - 1 );
}

$user = $this->environment->get_cache()->get( 'user' );
$qit_token = $this->environment->get_cache()->get( 'qit_token' );
$user = $this->cache->get( 'user' );
$qit_token = $this->cache->get( 'qit_token' );

if ( ! empty( $user ) && ! empty( $qit_token ) ) {
return base64_encode( sprintf( '%s:%s', $user, $qit_token ) );
Expand All @@ -33,7 +33,7 @@ public function get_partner_auth() {
* @return string|null MANAGER_SECRET, or null if not defined.
*/
public function get_manager_secret() {
return $this->environment->get_cache()->get( 'manager_secret' );
return $this->cache->get( 'manager_secret' );
}

/**
Expand All @@ -43,8 +43,8 @@ public function get_manager_secret() {
* @return void
*/
public function set_partner_auth( $user, $qit_token ): void {
$this->environment->get_cache()->set( 'user', $user, - 1 );
$this->environment->get_cache()->set( 'qit_token', $qit_token, - 1 );
$this->cache->set( 'user', $user, - 1 );
$this->cache->set( 'qit_token', $qit_token, - 1 );
}

/**
Expand All @@ -53,10 +53,10 @@ public function set_partner_auth( $user, $qit_token ): void {
* @return void
*/
public function set_manager_secret( $manager_secret ): void {
$this->environment->get_cache()->set( 'manager_secret', $manager_secret, - 1 );
$this->cache->set( 'manager_secret', $manager_secret, - 1 );
}

public function delete_manager_secret(): void {
$this->environment->get_cache()->delete( 'manager_secret' );
$this->cache->delete( 'manager_secret' );
}
}
21 changes: 5 additions & 16 deletions src/src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,19 @@ class Cache {
/** @var array<scalar|array<scalar>> $cache */
protected $cache = [];

/** @var Environment $environment */
protected $environment;

/** @var bool */
protected $did_init = false;

/** @var string */
protected $cache_file_path;

public function __construct() {
$this->cache_file_path = $this->make_cache_path_for_environment( Config::get_current_environment() );
$this->init_cache();
}

public function get_cache_file_path(): string {
return $this->cache_file_path;
}

public function make_cache_path_for_environment( string $environment ): string {
return Config::get_qit_dir() . ".env-$environment.json";
public function make_cache_path( string $manager_backend ): string {
return Config::get_qit_dir() . ".env-$manager_backend.json";
}

/**
Expand All @@ -38,10 +31,6 @@ public function make_cache_path_for_environment( string $environment ): string {
* @return void
*/
public function set( string $key, $value, int $expire ): void {
if ( ! $this->did_init ) {
throw new \LogicException( 'Cache not initialized.' );
}

if ( $expire !== - 1 ) {
$expire = time() + $expire;
}
Expand Down Expand Up @@ -107,8 +96,8 @@ public function delete( string $key ) {
*
* @throws \RuntimeException When could not read the cache file.
*/
protected function init_cache(): void {
$this->did_init = true;
public function init_cache(): void {
$this->cache_file_path = static::make_cache_path( Config::get_current_manager_backend() );

if ( ! file_exists( $this->cache_file_path ) ) {
return;
Expand All @@ -123,7 +112,7 @@ protected function init_cache(): void {
$cache = json_decode( $data, true );

if ( ! is_array( $cache ) ) {
throw new \RuntimeException( 'Could not parse cache file. Resetting environment, please remove the current Partner/Environment and add it again.' );
throw new \RuntimeException( 'Could not parse cache file. Resetting environment, please remove the current Partner/Manager backend and add it again.' );
}

$this->cache = $cache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace QIT_CLI\Commands\Environment;
namespace QIT_CLI\Commands\Backend;

use QIT_CLI\Auth;
use QIT_CLI\Environment;
use QIT_CLI\Cache;
use QIT_CLI\ManagerBackend;
use QIT_CLI\WooExtensionsList;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -13,22 +14,26 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

class AddEnvironment extends Command {
protected static $defaultName = 'env:add'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
class AddBackend extends Command {
protected static $defaultName = 'backend:add'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

/** @var Auth $auth */
protected $auth;

/** @var WooExtensionsList $woo_extensions_list */
protected $woo_extensions_list;

/** @var Environment $environment */
protected $environment;
/** @var ManagerBackend $manager_backend */
protected $manager_backend;

public function __construct( Environment $environment, Auth $auth, WooExtensionsList $woo_extensions_list ) {
$this->environment = $environment;
/** @var Cache $cache */
protected $cache;

public function __construct( ManagerBackend $manager_backend, Cache $cache, Auth $auth, WooExtensionsList $woo_extensions_list ) {
$this->manager_backend = $manager_backend;
$this->auth = $auth;
$this->woo_extensions_list = $woo_extensions_list;
$this->cache = $cache;
parent::__construct();
}

Expand All @@ -41,11 +46,11 @@ protected function configure() {
}

protected function execute( InputInterface $input, OutputInterface $output ): int {
$qit_secret = $input->getOption( 'qit_secret' );
$manager_url = $input->getOption( 'manager_url' );
$environment = $input->getOption( 'environment' );
$qit_secret = $input->getOption( 'qit_secret' );
$manager_url = $input->getOption( 'manager_url' );
$manager_backend = $input->getOption( 'environment' );

if ( empty( $environment ) && ( ! empty( $manager_url ) || ! empty( $qit_secret ) ) ) {
if ( empty( $manager_backend ) && ( ! empty( $manager_url ) || ! empty( $qit_secret ) ) ) {
throw new \UnexpectedValueException( 'When passing Manager URL/QIT Secret as a parameter, you need to also provide a --environment.' );
}

Expand All @@ -66,12 +71,12 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
case 'Production (Default)':
$manager_url = 'https://qit.woo.com';
$secret_store_id = '(Secret ID: 10523)';
$environment = Environment::$allowed_environments['production'];
$manager_backend = ManagerBackend::$allowed_manager_backends['production'];
break;
case 'Staging (Only for QIT development)':
$manager_url = 'https://stagingcompatibilitydashboard.wpcomstaging.com';
$secret_store_id = '(Secret ID: 10522)';
$environment = Environment::$allowed_environments['staging'];
$manager_backend = ManagerBackend::$allowed_manager_backends['staging'];
break;
case 'Local':
$question = new Question( "<question>What's the URL of the Manager you'd like to use? (Default: http://qit.test:8081)</question> ", 'http://qit.test:8081' );
Expand All @@ -84,14 +89,14 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
return $manager_url;
} );

$manager_url = $this->getHelper( 'question' )->ask( $input, $output, $question );
$environment = Environment::$allowed_environments['local'];
$manager_url = $this->getHelper( 'question' )->ask( $input, $output, $question );
$manager_backend = ManagerBackend::$allowed_manager_backends['local'];
break;
}
}

if ( $this->environment->environment_exists( $environment ) ) {
if ( ! $this->getHelper( 'question' )->ask( $input, $output, new ConfirmationQuestion( "<question>Environment '$environment' is already configured. Continue? (y/n) </question>", false ) ) ) {
if ( $this->manager_backend->manager_backend_exists( $manager_backend ) ) {
if ( ! $this->getHelper( 'question' )->ask( $input, $output, new ConfirmationQuestion( "<question>ManagerBackend '$manager_backend' is already configured. Continue? (y/n) </question>", false ) ) ) {
return Command::SUCCESS;
}
}
Expand All @@ -106,15 +111,15 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
}

try {
$this->environment->create_environment( $environment );
$this->manager_backend->add_manager_backend( $manager_backend );
} catch ( \Exception $e ) {
$output->writeln( "<error>{$e->getMessage()}</error>" );

return Command::FAILURE;
}

$this->auth->set_manager_secret( $qit_secret );
$this->environment->get_cache()->set( 'manager_url', $manager_url, - 1 );
$this->cache->set( 'manager_url', $manager_url, - 1 );

$output->writeln( "Validating your Manager Secret against $manager_url..." );
try {
Expand All @@ -124,7 +129,7 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
}
} catch ( \Exception $e ) {
$this->auth->delete_manager_secret();
$this->environment->get_cache()->delete( 'manager_url' );
$this->cache->delete( 'manager_url' );
$output->writeln( sprintf( '<error>We could not authenticate to %s using the provided Manager Secret.</error>', escapeshellarg( $manager_url ) ) );

return Command::FAILURE;
Expand All @@ -142,7 +147,7 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
TEXT;

$output->writeln( $easter_egg );
$output->writeln( "<comment>New environment '$environment' has been successfully setup. The CLI has switched to it.</comment>" );
$output->writeln( "<comment>New environment '$manager_backend' has been successfully setup. The CLI has switched to it.</comment>" );

return Command::SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

namespace QIT_CLI\Commands\Environment;
namespace QIT_CLI\Commands\Backend;

use QIT_CLI\Config;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CurrentEnvironment extends Command {
protected static $defaultName = 'env:current'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
class CurrentBackend extends Command {
protected static $defaultName = 'backend:current'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

protected function configure() {
$this
->setDescription( 'Prints the current environment.' );
}

protected function execute( InputInterface $input, OutputInterface $output ): int {
$output->writeln( Config::get_current_environment() );
$output->writeln( Config::get_current_manager_backend() );

return Command::SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

namespace QIT_CLI\Commands\Environment;
namespace QIT_CLI\Commands\Backend;

use QIT_CLI\Environment;
use QIT_CLI\ManagerBackend;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RemoveEnvironment extends Command {
protected static $defaultName = 'env:remove'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
class RemoveBackend extends Command {
protected static $defaultName = 'backend:remove'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

/** @var Environment $environment */
protected $environment;
/** @var ManagerBackend $manager_backend */
protected $manager_backend;

public function __construct( Environment $environment ) {
$this->environment = $environment;
public function __construct( ManagerBackend $manager_backend ) {
$this->manager_backend = $manager_backend;
parent::__construct();
}

Expand All @@ -26,10 +26,10 @@ protected function configure() {
}

protected function execute( InputInterface $input, OutputInterface $output ): int {
$environment = $input->getArgument( 'environment' );
$manager_backend = $input->getArgument( 'environment' );

try {
$this->environment->remove_environment( $environment );
$this->manager_backend->remove_manager_backend( $manager_backend );
} catch ( \InvalidArgumentException $e ) {
$output->writeln( sprintf( '<comment>%s</comment>', $e->getMessage() ) );

Expand All @@ -40,7 +40,7 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
return Command::SUCCESS;
}

$output->writeln( "<info>Environment '$environment' unset successfully.</info>" );
$output->writeln( "<info>ManagerBackend '$manager_backend' unset successfully.</info>" );

return Command::SUCCESS;
}
Expand Down
Loading

0 comments on commit 23e4000

Please sign in to comment.