Skip to content

Commit

Permalink
Detect phpunit.xml configuration when executing test in sub-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Jan 15, 2018
1 parent 513bb8f commit fde2e7c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/phpunit-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = class PhpUnitCommand {

this.lastOutput = this.runFullSuite
? `${this.binary}${this.suffix}`
: `${this.binary} ${this.file} ${this.filter}${this.suffix}`;
: `${this.binary} ${this.file} ${this.filter}${this.configuration}${this.suffix}`;

return this.lastOutput;
}
Expand All @@ -31,6 +31,12 @@ module.exports = class PhpUnitCommand {
return this.method ? `--filter '^.*::${this.method}$'` : '';
}

get configuration() {
return this.subDirectory
? ` --configuration ${this._normalizePath(path.join(this.subDirectory, 'phpunit.xml'))}`
: '';
}

get suffix() {
let suffix = vscode.workspace.getConfiguration('better-phpunit').get('commandSuffix');

Expand All @@ -42,14 +48,18 @@ module.exports = class PhpUnitCommand {
return vscode.workspace.getConfiguration('better-phpunit').get('phpunitBinary')
}

// find the closest phpunit.xml file in the project (for projects with multiple "vendor/phpunit"s).
return this.subDirectory
? this._normalizePath(path.join(this.subDirectory, 'vendor', 'bin', 'phpunit'))
: this._normalizePath(path.join(vscode.workspace.rootPath, 'vendor', 'bin', 'phpunit'));
}

get subDirectory() {
// find the closest phpunit.xml file in the project (for projects with multiple "vendor/bin/phpunit"s).
let phpunitDotXml = findUp.sync('phpunit.xml', { cwd: vscode.window.activeTextEditor.document.fileName });

let rootDirectory = phpunitDotXml
return path.dirname(phpunitDotXml) !== vscode.workspace.rootPath
? path.dirname(phpunitDotXml)
: vscode.workspace.rootPath;

return this._normalizePath(path.join(rootDirectory, 'vendor', 'bin', 'phpunit'));
: null;
}

get method() {
Expand Down
13 changes: 13 additions & 0 deletions test/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ describe("Better PHPUnit Test Suite", function () {
});
});

it("Detect configuration in sub-directory", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'sub-directory', 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand('better-phpunit.run');

await timeout(waitToAssertInSeconds, () => {
assert.equal(
extension.getGlobalCommandInstance().configuration,
' --configuration /Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/sub-directory/phpunit.xml'
);
});
});

it("Check full command", async () => {
let document = await vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'));
await vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) });
Expand Down
2 changes: 1 addition & 1 deletion test/project-stub/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
colors="false"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
Expand Down
1 change: 1 addition & 0 deletions test/project-stub/sub-directory/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
Expand Down

0 comments on commit fde2e7c

Please sign in to comment.