From 9c541bc42a01a05d75f8f16ed1a2ff7a2c569e74 Mon Sep 17 00:00:00 2001 From: Seb Kay Date: Mon, 31 Jan 2022 07:14:46 +0000 Subject: [PATCH 1/6] Add "db-socket" option to "setup" command --- src/Console/Commands/Setup.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Console/Commands/Setup.php b/src/Console/Commands/Setup.php index 9188ba8..43b3855 100644 --- a/src/Console/Commands/Setup.php +++ b/src/Console/Commands/Setup.php @@ -15,7 +15,7 @@ class Setup extends Command protected static $defaultName = 'setup'; protected \GuzzleHttp\Client $httpClient; - protected string $tmp_dir; + protected string $tmp_dir = ''; protected string $wpZipFilename = 'wordpress.zip'; protected string $wpDirectoryName = 'wordpress'; protected string $wpTestsZipFilename = 'wordpress-develop.zip'; @@ -45,6 +45,13 @@ protected function configure(): void "What's the host name of the database?" ); + $this->addOption( + 'db-socket', + null, + InputOption::VALUE_OPTIONAL, + "What's the socket path for connecting to the database?" + ); + $this->addOption( 'db-name', null, @@ -70,7 +77,7 @@ protected function configure(): void 'skip-db-creation', null, InputOption::VALUE_OPTIONAL, - "Skip the creation of the database if it already exists", + "Skip creation of the database because it already exists?", false ); } From d0ff2d0822825428b69d00422f2bec147c39f947 Mon Sep 17 00:00:00 2001 From: Seb Kay Date: Mon, 31 Jan 2022 07:49:27 +0000 Subject: [PATCH 2/6] Allow connection to database via host OR socket --- src/Console/Commands/Setup.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/Setup.php b/src/Console/Commands/Setup.php index 43b3855..e4068dc 100644 --- a/src/Console/Commands/Setup.php +++ b/src/Console/Commands/Setup.php @@ -41,7 +41,7 @@ protected function configure(): void $this->addOption( 'db-host', null, - InputOption::VALUE_REQUIRED, + InputOption::VALUE_OPTIONAL, "What's the host name of the database?" ); @@ -88,6 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->db_creds = [ 'host' => $input->getOption('db-host') ?: '', + 'socket' => $input->getOption('db-socket') ?: '', 'name' => $input->getOption('db-name') ?: '', 'user' => $input->getOption('db-user') ?: '', 'pass' => $input->getOption('db-pass') ?: '', @@ -139,6 +140,10 @@ protected function validateDatabaseCredentials(): void throw new \InvalidArgumentException("Please provide a database host."); } + if ($this->db_creds['socket'] && $this->db_creds['host'] == '') { + throw new \InvalidArgumentException("Please provide a database host, alongside a unix socket."); + } + if ($this->db_creds['name'] == '') { throw new \InvalidArgumentException("Please provide a database name."); } @@ -163,12 +168,20 @@ protected function connectToHost(InputInterface $input, OutputInterface &$output { $output->writeln(\WPTS\CMD_ICONS['loading'] . ' Testing connection...'); - $db_string = "mysql:host={$this->db_creds['host']};charset=UTF8"; + $db_string = "mysql:"; + + if ($input->getOption('db-socket')) { + $db_string .= "unix_socket={$this->db_creds['socket']};"; + } else { + $db_string .= "host={$this->db_creds['host']};"; + } if ($input->getOption('skip-db-creation')) { - $db_string .= ";dbname={$this->db_creds['name']}"; + $db_string .= "dbname={$this->db_creds['name']};"; } + $db_string .= "charset=UTF8"; + try { $db_connection = new \PDO( $db_string, @@ -180,6 +193,8 @@ protected function connectToHost(InputInterface $input, OutputInterface &$output $this->db_connection = $db_connection; } catch (\PDOException $e) { + \ray($e)->red(); + switch ($e->getCode()) { case '1045': throw new \Exception("Couldn't connect to host. Is the username or password incorrect?"); From 48abf3b2992f464842a12ef6ba2a52bf71db3fc6 Mon Sep 17 00:00:00 2001 From: Seb Kay Date: Mon, 31 Jan 2022 07:55:15 +0000 Subject: [PATCH 3/6] Add socket to WP tests config file (if applicable) --- src/Console/Commands/Setup.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Console/Commands/Setup.php b/src/Console/Commands/Setup.php index e4068dc..26394eb 100644 --- a/src/Console/Commands/Setup.php +++ b/src/Console/Commands/Setup.php @@ -41,7 +41,7 @@ protected function configure(): void $this->addOption( 'db-host', null, - InputOption::VALUE_OPTIONAL, + InputOption::VALUE_REQUIRED, "What's the host name of the database?" ); @@ -136,14 +136,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int */ protected function validateDatabaseCredentials(): void { - if ($this->db_creds['host'] == '') { - throw new \InvalidArgumentException("Please provide a database host."); - } - if ($this->db_creds['socket'] && $this->db_creds['host'] == '') { throw new \InvalidArgumentException("Please provide a database host, alongside a unix socket."); } + if ($this->db_creds['host'] == '') { + throw new \InvalidArgumentException("Please provide a database host."); + } + if ($this->db_creds['name'] == '') { throw new \InvalidArgumentException("Please provide a database name."); } @@ -193,8 +193,6 @@ protected function connectToHost(InputInterface $input, OutputInterface &$output $this->db_connection = $db_connection; } catch (\PDOException $e) { - \ray($e)->red(); - switch ($e->getCode()) { case '1045': throw new \Exception("Couldn't connect to host. Is the username or password incorrect?"); @@ -389,7 +387,12 @@ protected function configureWordPressTestFiles(InputInterface $input, OutputInte $transformer->update('constant', 'DB_NAME', $this->db_creds['name']); $transformer->update('constant', 'DB_USER', $this->db_creds['user']); $transformer->update('constant', 'DB_PASSWORD', $this->db_creds['pass']); - $transformer->update('constant', 'DB_HOST', $this->db_creds['host']); + + if ($this->db_creds['host'] && $this->db_creds['socket']) { + $transformer->update('constant', 'DB_HOST', "{$this->db_creds['host']}:{$this->db_creds['socket']}"); + } else { + $transformer->update('constant', 'DB_HOST', $this->db_creds['host']); + } } catch (\Throwable $e) { throw new \Exception("There was an error configuring the WordPress test files."); } From c735233d6a18822f5e30d1525381b17c88f8adbe Mon Sep 17 00:00:00 2001 From: Seb Kay Date: Mon, 31 Jan 2022 17:40:36 +0000 Subject: [PATCH 4/6] Update README.md --- README.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6b2f9de..ac5fd9e 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,22 @@ composer require sebkay/touchstone --dev Running the setup process downloads and installs both WordPress and the official WordPress test files in your temp directory. -Here's the command needed to run the setup process: - ```shell -# Command -./vendor/bin/touchstone setup --db-host=[HOST] --db-name=[DATABASE NAME] --db-user=[DATABASE USER] --db-pass=[DATABASE PASSWORD] --skip-db-creation=[FALSE] +# Options +./vendor/bin/touchstone setup --db-host=[HOST] --db-socket=[SOCKET PATH] --db-name=[DATABASE NAME] --db-user=[DATABASE USERNAME] --db-pass=[DATABASE PASSWORD] --skip-db-creation=[TRUE/FALSE] +``` +#### Regular Connection + +```shell # Example -./vendor/bin/touchstone setup --db-host=127.0.0.1:8889 --db-name=touchstone_tests --db-user=root --db-pass=root --skip-db-creation=true +./vendor/bin/touchstone setup --db-host=127.0.0.1:8889 --db-name=touchstone_tests --db-user=root --db-pass=root +``` + +#### via a Socket + +```shell +./vendor/bin/touchstone setup --db-host=127.0.0.1:8889 --db-socket="/path/to/mysqld.sock" --db-name=touchstone_tests --db-user=root --db-pass=root ``` ### 2.) Creating Tests @@ -120,7 +128,7 @@ return [ ]; ``` -#### Plugins +#### WordPress Plugins Here's how to load plugins which are loaded before each test. @@ -147,7 +155,7 @@ return [ bin/plugins ``` -#### Theme +#### WordPress Theme Here's how to load a theme which is active for each test. @@ -173,7 +181,7 @@ To do so add the following to your `composer.json` file: ```json ... "scripts": { - "touchstone:setup": "./vendor/bin/touchstone setup --db-host=[HOST] --db-name=[DATABASE NAME] --db-user=[DATABASE USER] --db-pass=[DATABASE PASSWORD] --skip-db-creation=[FALSE]", + "touchstone:setup": "./vendor/bin/touchstone setup --db-host=[HOST] --db-name=[DATABASE NAME] --db-user=[DATABASE USER] --db-pass=[DATABASE PASSWORD] --skip-db-creation=true", "touchstone:test": "./vendor/bin/touchstone test", "touchstone:unit": "./vendor/bin/touchstone test --type=unit", "touchstone:integration": "./vendor/bin/touchstone test --type=integration" From 0326b1972b5e00f8802721d67c0557de3710465f Mon Sep 17 00:00:00 2001 From: Seb Kay Date: Mon, 31 Jan 2022 17:43:47 +0000 Subject: [PATCH 5/6] Rename things to use "consumer" to make it more clear what's part of the package and what's used for the consumer of the package --- composer.json | 2 +- phpunit-touchstone.xml => phpunit-consumer.xml | 2 +- src/Settings.php | 2 +- .../Integration/IntegrationTest.php | 0 {touchstone-tests => tests-consumer}/Unit/UnitTest.php | 0 {touchstone-tests => tests-consumer}/bootstrap.php | 0 6 files changed, 3 insertions(+), 3 deletions(-) rename phpunit-touchstone.xml => phpunit-consumer.xml (78%) rename {touchstone-tests => tests-consumer}/Integration/IntegrationTest.php (100%) rename {touchstone-tests => tests-consumer}/Unit/UnitTest.php (100%) rename {touchstone-tests => tests-consumer}/bootstrap.php (100%) diff --git a/composer.json b/composer.json index 5294767..fc1e27d 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "autoload": { "psr-4": { "WPTS\\": "./src/", - "WPTS\\Tests\\": "./touchstone-tests/" + "WPTS\\Tests\\": "./tests-consumer/" }, "files": [ "./inc/helpers.php" diff --git a/phpunit-touchstone.xml b/phpunit-consumer.xml similarity index 78% rename from phpunit-touchstone.xml rename to phpunit-consumer.xml index efc1e8b..41f4ae0 100644 --- a/phpunit-touchstone.xml +++ b/phpunit-consumer.xml @@ -1,6 +1,6 @@ consumerRootPath = \exec('pwd') . '/'; $this->tempDirectory = \sys_get_temp_dir() . '/'; $this->wpTestFilesDirectory = $this->tempDirectory() . 'wordpress-tests-lib'; - $this->phpunitConfigPath = $this->appRootPath() . 'phpunit-touchstone.xml'; + $this->phpunitConfigPath = $this->appRootPath() . 'phpunit-consumer.xml'; $this->phpunitExecutablePath = $this->consumerRootPath() . 'vendor/bin/phpunit'; $this->consumerSettings = new ConsumerSettings($this->consumerRootPath()); diff --git a/touchstone-tests/Integration/IntegrationTest.php b/tests-consumer/Integration/IntegrationTest.php similarity index 100% rename from touchstone-tests/Integration/IntegrationTest.php rename to tests-consumer/Integration/IntegrationTest.php diff --git a/touchstone-tests/Unit/UnitTest.php b/tests-consumer/Unit/UnitTest.php similarity index 100% rename from touchstone-tests/Unit/UnitTest.php rename to tests-consumer/Unit/UnitTest.php diff --git a/touchstone-tests/bootstrap.php b/tests-consumer/bootstrap.php similarity index 100% rename from touchstone-tests/bootstrap.php rename to tests-consumer/bootstrap.php From 0b2ac77baa9397edc8775fd36efcdd328aaa0ad5 Mon Sep 17 00:00:00 2001 From: Seb Kay Date: Mon, 31 Jan 2022 17:45:23 +0000 Subject: [PATCH 6/6] Fix package tests not running --- phpunit-consumer.xml | 1 + phpunit.xml | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/phpunit-consumer.xml b/phpunit-consumer.xml index 41f4ae0..113f2b9 100644 --- a/phpunit-consumer.xml +++ b/phpunit-consumer.xml @@ -1,5 +1,6 @@ + + + ./src + + + + + ./tests/Unit/ + + + ./tests/Integration/ + +