-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
8 changed files
with
130 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters