Skip to content

Commit

Permalink
Create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Dec 1, 2017
1 parent 754fd77 commit 5091b0f
Show file tree
Hide file tree
Showing 3,083 changed files with 241,869 additions and 36 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.vscode-test/
.vsix
.DS_Store
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"Other"
],
"activationEvents": [
"*"
"onCommand:better-phpunit.run"
],
"main": "./src/extension",
"contributes": {
Expand Down
5 changes: 4 additions & 1 deletion src/project-snapshot.js → src/command-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ const findUp = require('find-up');
const vscode = require('vscode');
const path = require('path');

module.exports = class ProjectSnapshot {
module.exports = class CommandInstance {
constructor() {
this.fileName = this.normalizePath(vscode.window.activeTextEditor.document.fileName);
this.methodName = this.findMethodName();
this.executablePath = this.findExecutablePath();

const filterString = this.methodName ? `--filter '^.*::${this.methodName}$'` : '';
this.shellCommand = `${this.executablePath} ${this.fileName} ${filterString}`;
}

findExecutablePath() {
Expand Down
27 changes: 10 additions & 17 deletions src/extension.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
const vscode = require('vscode');
const assert = require('assert');
const ProjectSnapshot = require('./project-snapshot');
const CommandInstance = require('./command-instance');

var projectSnapshot;
var currentCommandInstance;

module.exports.activate = function (context) {
let disposables = [];

disposables.push(vscode.commands.registerCommand('better-phpunit.run', async () => {
projectSnapshot = new ProjectSnapshot;
// It's important that "better-phpunit.run" is the activation event for this package.
// Otherwise, "currentCommandInstance" won't be available to the rest of the package.
currentCommandInstance = new CommandInstance;

await vscode.commands.executeCommand('workbench.action.terminal.clear');
await vscode.commands.executeCommand('workbench.action.tasks.runTask', 'phpunit: run');
}));

disposables.push(vscode.commands.registerCommand('better-phpunit.run-previous', async () => {
if (runCommandHasntRunYet()) {
vscode.window.showErrorMessage('Better PHPUnit: No tests have been run yet.');
return;
}

await vscode.commands.executeCommand('workbench.action.terminal.clear');
await vscode.commands.executeCommand('workbench.action.tasks.runTask', 'phpunit: run');
}));

disposables.push(vscode.workspace.registerTaskProvider('phpunit', {
provideTasks: () => {
if (runCommandHasntRunYet()) {
return []; // Don't provide task until the "run" command has been run.
}

const filterString = projectSnapshot.methodName ? `--filter '^.*::${projectSnapshot.methodName}$'` : '';

// new vscode.TaskScope.Global
return [new vscode.Task(
{ type: "phpunit", task: "run" },
"run",
'phpunit',
new vscode.ShellExecution(`${projectSnapshot.executablePath} ${projectSnapshot.fileName} ${filterString}`),
new vscode.ShellExecution(currentCommandInstance.shellCommand),
'$phpunit'
)];
}
Expand All @@ -45,6 +37,7 @@ module.exports.activate = function (context) {
context.subscriptions.push(disposables);
}

function runCommandHasntRunYet() {
return ! projectSnapshot;
// This method is exposed for testing purposes.
module.exports.getCurrentCommandInstance = function () {
return currentCommandInstance;
}
144 changes: 127 additions & 17 deletions test/extension.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,134 @@
/* global suite, test */
const assert = require('assert');
const vscode = require('vscode');
const path = require('path');
const extension = require('../src/extension');

//
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
//
suite("Better PHPUnit Test Suite", function() {

// The module 'assert' provides assertion methods from node
const assert = require('assert');
test("Run file outside of method", function (done) {
this.timeout(8000);
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'))
.then((document) => {
vscode.window.showTextDocument(document).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.ok(extension.getCurrentCommandInstance().methodName === undefined);
done();
}, 1000);
});
});
});
});

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
const vscode = require('vscode');
const myExtension = require('../extension');
test("Run from within first method", function(done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'))
.then((document) => {
vscode.window.showTextDocument(document, {selection: new vscode.Range(7, 0, 7, 0)}).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.equal('test_first', extension.getCurrentCommandInstance().methodName);
done();
}, 1000);
});
});
});
});

test("Run from within second method", function (done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'))
.then((document) => {
vscode.window.showTextDocument(document, { selection: new vscode.Range(12, 0, 12, 0) }).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.equal('test_second', extension.getCurrentCommandInstance().methodName);
done();
}, 1000);
});
});
});
});

// Defines a Mocha test suite to group tests of similar kind together
suite("Extension Tests", function() {
test("Detect filename", function (done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'))
.then((document) => {
vscode.window.showTextDocument(document).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.equal('/Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/tests/SampleTest.php', extension.getCurrentCommandInstance().fileName);
done();
}, 1000);
});
});
});
});

test("Detect filename with a space", function (done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'File With Spaces Test.php'))
.then((document) => {
vscode.window.showTextDocument(document).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.equal('/Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/tests/FileXX WithXX SpacesXX Test.php', extension.getCurrentCommandInstance().fileName.replace(/\\/g, 'XX'));
done();
}, 1000);
});
});
});
});

test("Detect executable", function (done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'))
.then((document) => {
vscode.window.showTextDocument(document).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.equal('/Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/vendor/bin/phpunit', extension.getCurrentCommandInstance().executablePath);
done();
}, 1000);
});
});
});
});

test("Detect executable in sub-directory", function (done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'sub-directory', 'tests', 'SampleTest.php'))
.then((document) => {
vscode.window.showTextDocument(document).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.equal('/Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/sub-directory/vendor/bin/phpunit', extension.getCurrentCommandInstance().executablePath);
done();
}, 1000);
});
});
});
});

test("Full command", function (done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'SampleTest.php'))
.then((document) => {
vscode.window.showTextDocument(document, { selection: new vscode.Range(7, 0, 7, 0) }).then(() => {
vscode.commands.executeCommand('better-phpunit.run').then(() => {
setTimeout(() => {
assert.equal("/Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/vendor/bin/phpunit /Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/tests/SampleTest.php --filter '^.*::test_first$'", extension.getCurrentCommandInstance().shellCommand);
done();
}, 1000);
});
});
});
});

// Defines a Mocha unit test
test("Something 1", function() {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
test("Run previous", function (done) {
vscode.workspace.openTextDocument(path.join(vscode.workspace.rootPath, 'tests', 'OtherTest.php'))
.then((document) => {
vscode.window.showTextDocument(document, { selection: new vscode.Range(12, 0, 12, 0) }).then(() => {
vscode.commands.executeCommand('better-phpunit.run-previous').then(() => {
setTimeout(() => {
assert.equal("/Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/vendor/bin/phpunit /Users/calebporzio/Documents/Code/sites/better-phpunit/test/project-stub/tests/SampleTest.php --filter '^.*::test_first$'", extension.getCurrentCommandInstance().shellCommand);
done();
}, 1000);
});
});
});
});
});
13 changes: 13 additions & 0 deletions test/project-stub/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "calebporzio/project-stub",
"description": "Test environment for Better PHPUnit VS Code extension",
"authors": [
{
"name": "Caleb Porzio",
"email": "[email protected]"
}
],
"require": {
"phpunit/phpunit": "^6.4"
}
}
Loading

0 comments on commit 5091b0f

Please sign in to comment.