Skip to content

Commit

Permalink
wp-now: improve php tests and comment failing tests (#43)
Browse files Browse the repository at this point in the history
Fix wp-now tests.
Fix Mac Os wp-cli path error.
Fix php tests after playground mode was introduced.
  • Loading branch information
sejas authored Jun 1, 2023
1 parent 5503c3d commit fa50062
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 121 deletions.
6 changes: 4 additions & 2 deletions packages/wp-now/src/execute-wp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { disableOutput } from './output';
import getWpCliPath from './get-wp-cli-path';
import getWpNowConfig from './config';
import { DEFAULT_PHP_VERSION, DEFAULT_WORDPRESS_VERSION } from './constants';
import { dirname } from 'path';

/**
* This is an unstable API. Multiple wp-cli commands may not work due to a current limitation on php-wasm and pthreads.
Expand All @@ -24,10 +25,11 @@ export async function executeWPCli(args: string[]) {
const [, php] = phpInstances;

try {
php.useHostFilesystem();
const vfsWpCliPath = '/wp-cli/wp-cli.phar';
php.mount(dirname(getWpCliPath()), dirname(vfsWpCliPath));
await php.cli([
'php',
getWpCliPath(),
vfsWpCliPath,
`--path=${wpNowOptions.documentRoot}`,
...args,
]);
Expand Down
165 changes: 55 additions & 110 deletions packages/wp-now/src/tests/execute-php.spec.ts
Original file line number Diff line number Diff line change
@@ -1,131 +1,76 @@
import fs from 'fs-extra';
import path from 'path';
import { executePHP } from '../execute-php';
import getWpNowConfig from '../config';
import { runCli } from '../run-cli';

const exampleDir = path.join(__dirname, 'execute-php');

test('php file execution in index mode', async () => {
const resultFilePath = path.join(exampleDir, 'hello-world-result.txt');
// reset result file
fs.writeFileSync(resultFilePath, '');
const options = await getWpNowConfig({
path: exampleDir,
});
await executePHP(
['php', path.join(exampleDir, 'hello-world.php')],
options
);
const output = fs.readFileSync(resultFilePath, 'utf8');
expect(output).toBe('Hello World!');
});

test('php file execution for each PHP Version', async () => {
const resultFilePath = path.join(exampleDir, 'php-version-result.txt');
const options = await getWpNowConfig({
path: exampleDir,
});
await executePHP(['php', path.join(exampleDir, 'php-version.php')], {
...options,
phpVersion: '7.4',
});
let output = fs.readFileSync(resultFilePath, 'utf8');
expect(output.substring(0, 16)).toBe('PHP Version: 7.4');

await executePHP(['php', path.join(exampleDir, 'php-version.php')], {
...options,
phpVersion: '8.0',
});
output = fs.readFileSync(resultFilePath, 'utf8');
expect(output.substring(0, 16)).toBe('PHP Version: 8.0');

await executePHP(['php', path.join(exampleDir, 'php-version.php')], {
...options,
phpVersion: '8.2',
});
output = fs.readFileSync(resultFilePath, 'utf8');
expect(output.substring(0, 16)).toBe('PHP Version: 8.2');

fs.writeFileSync(resultFilePath, 'PHP Version: X.Y');
});

test('php throws an error if the first element is not the string "php"', async () => {
const options = await getWpNowConfig({
path: exampleDir,
});
try {
await executePHP(
['word-different-to-php', path.join(exampleDir, 'php-version.php')],
{
...options,
phpVersion: '7.4',
}
);
} catch (error) {
expect(error.message).toBe(
'The first argument to executePHP must be the string "php".'
);
}
});

describe('validate php arguments passed through yargs', () => {
describe('validate php execution', () => {
let output = '';
let consoleLogMock;
let processExitMock;
const argv = process.argv;
beforeEach(() => {
consoleLogMock = vi
.spyOn(console, 'log')
.mockImplementation((newLine: string) => {
output += `${newLine}\n`;
});
processExitMock = vi
.spyOn(process, 'exit')
.mockImplementation(() => null);
vi.spyOn(console, 'log').mockImplementation((newLine: string) => {
output += `${newLine}`;
});
});

afterEach(() => {
output = '';
process.argv = argv;
consoleLogMock.mockRestore();
processExitMock.mockRestore();
vi.restoreAllMocks();
});
test('php file execution in index mode', async () => {
const options = await getWpNowConfig({
path: exampleDir,
});
await executePHP(
['php', path.join(exampleDir, 'hello-world.php')],
options
);

test('php should receive the correct yargs arguments', async () => {
process.argv = ['node', 'wp-now', 'php', '--', '--version'];
await runCli();
expect(output).toMatch(/PHP 8\.0(.*)\(cli\)/i);
expect(processExitMock).toHaveBeenCalledWith(0);
expect(output).toMatch(/Hello World!/);
});
test('php file execution for each PHP Version', async () => {
const options = await getWpNowConfig({
path: exampleDir,
});
await executePHP(['php', path.join(exampleDir, 'php-version.php')], {
...options,
phpVersion: '7.4',
});
expect(output.substring(0, 16)).toBe('PHP Version: 7.4');

test('wp-now should change the php version', async () => {
process.argv = [
'node',
'wp-now',
'php',
'--php=7.4',
'--',
'--version',
];
await runCli();
expect(output).toMatch(/PHP 7\.4(.*)\(cli\)/i);
expect(processExitMock).toHaveBeenCalledWith(0);
});
output = '';
await executePHP(['php', path.join(exampleDir, 'php-version.php')], {
...options,
phpVersion: '8.0',
});
expect(output.substring(0, 16)).toBe('PHP Version: 8.0');

test('php should execute a file', async () => {
const filePath = path.join(exampleDir, 'print-php-version.php');
process.argv = ['node', 'wp-now', 'php', filePath];
await runCli();
expect(output).toMatch(/8\.0/i);
expect(processExitMock).toHaveBeenCalledWith(0);
output = '';
await executePHP(['php', path.join(exampleDir, 'php-version.php')], {
...options,
phpVersion: '8.2',
});
expect(output.substring(0, 16)).toBe('PHP Version: 8.2');
});

test('php should execute a file and change php version', async () => {
const filePath = path.join(exampleDir, 'print-php-version.php');
process.argv = ['node', 'wp-now', 'php', '--php=7.4', '--', filePath];
await runCli();
expect(output).toMatch(/7\.4/i);
expect(processExitMock).toHaveBeenCalledWith(0);
test('php throws an error if the first element is not the string "php"', async () => {
const options = await getWpNowConfig({
path: exampleDir,
});
try {
await executePHP(
[
'word-different-to-php',
path.join(exampleDir, 'php-version.php'),
],
{
...options,
phpVersion: '7.4',
}
);
} catch (error) {
expect(error.message).toBe(
'The first argument to executePHP must be the string "php".'
);
}
});
});

This file was deleted.

4 changes: 1 addition & 3 deletions packages/wp-now/src/tests/execute-php/hello-world.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<?php
$resultFile = fopen(__DIR__ . '/hello-world-result.txt', 'w');
fwrite($resultFile, 'Hello World!');
fclose($resultFile);
echo 'Hello World!';

This file was deleted.

4 changes: 1 addition & 3 deletions packages/wp-now/src/tests/execute-php/php-version.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<?php
$resultFile = fopen(__DIR__ . '/php-version-result.txt', 'w');
fwrite($resultFile, "PHP Version: " . phpversion());
fclose($resultFile);
echo "PHP Version: " . phpversion();

This file was deleted.

69 changes: 69 additions & 0 deletions packages/wp-now/src/tests/wp-now.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import crypto from 'crypto';
import getWpNowTmpPath from '../get-wp-now-tmp-path';
import getWpCliTmpPath from '../get-wp-cli-tmp-path';
import { executeWPCli } from '../execute-wp-cli';
import { runCli } from '../run-cli';

const exampleDir = __dirname + '/mode-examples';

Expand Down Expand Up @@ -512,6 +513,74 @@ describe('Test starting different modes', () => {

expect(themeName.text).toContain('Twenty Twenty-Three');
});

/**
* Test PHP integration test executing runCli.
*/
describe('validate php comand arguments passed through yargs', () => {
const phpExampleDir = path.join(__dirname, 'execute-php');
const argv = process.argv;
let output = '';
let processExitMock;
beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation((newLine: string) => {
output += `${newLine}\n`;
});
processExitMock = vi
.spyOn(process, 'exit')
.mockImplementation(() => null);
});

afterEach(() => {
output = '';
process.argv = argv;
vi.resetAllMocks();
});

test('php should receive the correct yargs arguments', async () => {
process.argv = ['node', 'wp-now', 'php', '--', '--version'];
await runCli();
expect(output).toMatch(/PHP 8\.0(.*)\(cli\)/i);
expect(processExitMock).toHaveBeenCalledWith(0);
});

test('wp-now should change the php version', async () => {
process.argv = [
'node',
'wp-now',
'php',
'--php=7.4',
'--',
'--version',
];
await runCli();
expect(output).toMatch(/PHP 7\.4(.*)\(cli\)/i);
expect(processExitMock).toHaveBeenCalledWith(0);
});

test('php should execute a file', async () => {
const filePath = path.join(phpExampleDir, 'php-version.php');
process.argv = ['node', 'wp-now', 'php', filePath];
await runCli();
expect(output).toMatch(/8\.0/i);
expect(processExitMock).toHaveBeenCalledWith(0);
});

test('php should execute a file and change php version', async () => {
const filePath = path.join(phpExampleDir, 'php-version.php');
process.argv = [
'node',
'wp-now',
'php',
'--php=7.4',
'--',
filePath,
];
await runCli();
expect(output).toMatch(/7\.4/i);
expect(processExitMock).toHaveBeenCalledWith(0);
});
});
});

/**
Expand Down

0 comments on commit fa50062

Please sign in to comment.