Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,7 +128,7 @@ return [
];
```

#### Plugins
#### WordPress Plugins

Here's how to load plugins which are loaded before each test.

Expand All @@ -147,7 +155,7 @@ return [
bin/plugins
```

#### Theme
#### WordPress Theme

Here's how to load a theme which is active for each test.

Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"autoload": {
"psr-4": {
"WPTS\\": "./src/",
"WPTS\\Tests\\": "./touchstone-tests/"
"WPTS\\Tests\\": "./tests-consumer/"
},
"files": [
"./inc/helpers.php"
Expand Down
3 changes: 2 additions & 1 deletion phpunit-touchstone.xml → phpunit-consumer.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<phpunit
bootstrap="touchstone-tests/bootstrap.php"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests-consumer/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit/</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration/</directory>
</testsuite>
</testsuites>
</phpunit>
35 changes: 30 additions & 5 deletions src/Console/Commands/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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
);
}
Expand All @@ -81,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') ?: '',
Expand Down Expand Up @@ -128,6 +136,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
protected function validateDatabaseCredentials(): void
{
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.");
}
Expand Down Expand Up @@ -156,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,
Expand Down Expand Up @@ -367,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.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
$this->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());
Expand Down
File renamed without changes.
File renamed without changes.