From fa5006217304d9ff1d1b6675c39fd2b430dae594 Mon Sep 17 00:00:00 2001 From: Antonio Sejas Date: Thu, 1 Jun 2023 13:11:14 +0100 Subject: [PATCH] wp-now: improve php tests and comment failing tests (#43) Fix wp-now tests. Fix Mac Os wp-cli path error. Fix php tests after playground mode was introduced. --- packages/wp-now/src/execute-wp-cli.ts | 6 +- packages/wp-now/src/tests/execute-php.spec.ts | 165 ++++++------------ .../tests/execute-php/hello-world-result.txt | 1 - .../src/tests/execute-php/hello-world.php | 4 +- .../tests/execute-php/php-version-result.txt | 1 - .../src/tests/execute-php/php-version.php | 4 +- .../tests/execute-php/print-php-version.php | 1 - packages/wp-now/src/tests/wp-now.spec.ts | 69 ++++++++ 8 files changed, 130 insertions(+), 121 deletions(-) delete mode 100644 packages/wp-now/src/tests/execute-php/hello-world-result.txt delete mode 100644 packages/wp-now/src/tests/execute-php/php-version-result.txt delete mode 100644 packages/wp-now/src/tests/execute-php/print-php-version.php diff --git a/packages/wp-now/src/execute-wp-cli.ts b/packages/wp-now/src/execute-wp-cli.ts index 5d313b43..c5d8ec1f 100644 --- a/packages/wp-now/src/execute-wp-cli.ts +++ b/packages/wp-now/src/execute-wp-cli.ts @@ -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. @@ -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, ]); diff --git a/packages/wp-now/src/tests/execute-php.spec.ts b/packages/wp-now/src/tests/execute-php.spec.ts index f1ed1894..de98e757 100644 --- a/packages/wp-now/src/tests/execute-php.spec.ts +++ b/packages/wp-now/src/tests/execute-php.spec.ts @@ -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".' + ); + } }); }); diff --git a/packages/wp-now/src/tests/execute-php/hello-world-result.txt b/packages/wp-now/src/tests/execute-php/hello-world-result.txt deleted file mode 100644 index c57eff55..00000000 --- a/packages/wp-now/src/tests/execute-php/hello-world-result.txt +++ /dev/null @@ -1 +0,0 @@ -Hello World! \ No newline at end of file diff --git a/packages/wp-now/src/tests/execute-php/hello-world.php b/packages/wp-now/src/tests/execute-php/hello-world.php index 82b0dc4f..f34c0a11 100644 --- a/packages/wp-now/src/tests/execute-php/hello-world.php +++ b/packages/wp-now/src/tests/execute-php/hello-world.php @@ -1,4 +1,2 @@ { 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); + }); + }); }); /**