-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scenario Provisioning: Thoughts on new feature? #183
Comments
I think this is actually really cool and useful! I am open to the idea, but the first question I would ask: Can this be implemented via a plugin instead of merged into core? Peridot is pretty extensible, so if at all possible, a plugin would be preferred |
I haven't played around with the plugin/event system much, but I suspect it could be. I'll take a look and report back. |
Alrighty, I set up a new repo with a very rough proof-of-concept plugin pushed to /**
* Create a test and add it to the current suite
*
* @param $description
* @param $fn
*/
public function addTest($description, callable $fn = null, $pending = null)
{
$test = new Test($description, $fn);
if ($pending !== null) {
$test->setPending($pending);
}
$test->setFile($this->file);
$this->getCurrentSuite()->addTest($test);
$this->eventEmitter->emit('test.added', $test); // <-- new event
return $test;
} Slightly altered the scenario syntax: describe('convertValueToFizzBuzz($value)', function() {
context('when $value is divisible by 3 and 5', function() {
it('should return "FizzBuzz"', function() {
assert(convertValueToFizzBuzz($this->expected_fizzbuzz_value) === 'FizzBuzz');
});
inScenario( setUp(function() { $this->expected_fizzbuzz_value = 0; }) );
inScenario( setUp(function() { $this->expected_fizzbuzz_value = 15; }) );
inScenario( setUp(function() { $this->expected_fizzbuzz_value = 45; }) );
inScenario( setUp(function() { $this->expected_fizzbuzz_value = 60; }) );
});
}); This syntax allows for a more powerful scenario setup/teardown system and makes the plugin code which binds the scenarios to the test definition more readable and easy to follow (I think). Repo includes a
The method of hooking in the scenarios is a bit sketch but it seems to work. Essentially, a test execution follows the path in the core code:
We can break into this path by adding an additional setup and teardown function before the test runs:
Hopefully that makes some sense. Like I said, the code in the repo is very rough but if this direction makes sense, I'll go through and give it a thorough polishing (comments, tests, etc). Let me know what you think! |
I think this approach sounds fine. Sounds like an overall small footprint on core which I like :) From a plugin usability perspective, it seems like there needs to be care taken to make sure I would also make sure failures in a scenario's I am happy to accept a PR to core to support the new event. I'll update docs once it's added, or I can hold off until your plugin is fleshed out in case you have some more events you want to add. In general, seems like a super useful addition. Thanks! |
Re-read my last comment and realized how long it was, hopefully it didn't bore you too much ;) Definitely trying to minimize core impact! Yeah, I've considered how to ensure scenarios are bound directly to tests, have some ideas. I'll hold off on the PR until the plugin is a bit more fleshed out and polished. Thanks! |
No worries man! You are putting in unpaid time to make cool things with computers. I can at least give up a little time too discuss :) Thanks again! |
So I've been out of the PHP game for a few months and as a 'back-on-the-horse' exercise, I wanted to play around with Peridot as I really like the syntax (coming from a PHPUnit guy). While I was tinkering, I thought it'd be nice to have a feature wherein specific 'scenarios' are bound to a test, allowing the same test definition to run with different values (similar to data providers in PHPUnit). I wanted to take a stab at implementing this, so I forked the repo and implemented scenarios as a kind of proof of concept and was wondering what the Peridot powers that be think of the feature in general? Consider a FizzBuzz example:
A simple test for 'Fizz' conversion could be described as:
However, it would be nice to test other values as well. This could be accomplished by looping over a value array within the test, but this just adds noise to the test itself. What I had in mind was adding a function to the base DSL that would attach a scenario to the last-added test (the test would run once for each scenario):
The idea is that a scenario is an object which is basically a glorified array. Each scenario in turn is injected into the scope right after a given test's setup functions as
<test scope>->scenario
. The scenario is removed from the test scope immediately before the test teardown functions are invoked.Because each test would be run multiple times, multiple events would be emitted per-test. This leads to some duplicate reporting, but the duplication makes it very easy to see which scenario is causing the test to fail:
I know that this is a trivial example but hopefully it serves to illustrate the idea. Any thoughts?
Josh
The text was updated successfully, but these errors were encountered: