<?php /** * Somewhere within your application: * * This is just an example of what you can do. */ namespace FileSystem; class Space extends Directory { } $application->bind('FileSystem\\Space', function (User $user) { return new Space($user->allocatedSpacePath); }); // In your business logic: use FileSystem\File; use FileSystem\FileSystem; use FileSystem\Space; when('i want to make a directory inside my space', then(apply(a(function (FileSystem $fileSystem, Space $space, File $file) { $fileSystem->write($file, inside($space)); }))));
<?php use FileSystem\FileSystem; use FileSystem\File; when('i want to delete a file', then(apply(a(function (FileSystem $fileSystem, File $file) { $fileSystem->delete($file); echo $file->content; })))); /* |-------------------------------------------------------------------------- | Note: The file stays intact. |-------------------------------------------------------------------------- | | After you call delete with $file on the file system it goes | through the delete process of the file system, it is being | removed from the file tree and it is removed from the | parent inside the file tree. | | BUT: The file stays intact, all its data and even the parent | will stay intact meaning you can do with that file whatever | you want. */
then(function ($callback) { return $callback() === 'foo'; }); }); context('using a real context', function () { given('given_php', m::mock()->shouldReceive('get_value')->andReturn('foo')->getMock()); given('callback', new EnhancedCallback(function ($foo_var) { return $foo_var; })); then(function ($callback, $given_php) { $result = $callback($given_php) === 'foo'; m::close(); return $result; }); }); context('parameters', function () { given('given_php', m::mock()->shouldReceive('get_value')->twice()->andReturn(1)->getMock()); given('callback', new EnhancedCallback(function ($param_1, $param_2) { return $param_1 + $param_2; })); when('parameters', function ($given_php, EnhancedCallback $callback) { return $callback->parameters($given_php); }); then(function ($parameters) { return count($parameters) === 4; }); then(function ($parameters) { return isset($parameters[0], $parameters[1], $parameters['param_1'], $parameters['param_2']); }); }); });
<?php use FileSystem\File; use FileSystem\FileSystem; when('i want to rename a file', then(apply(a(function (FileSystem $fileSystem, File $file) { $fileSystem->rename($file); })))); /* |-------------------------------------------------------------------------- | Notes: renaming behind the scenes. |-------------------------------------------------------------------------- | | Behind the scenes in your technical code you call: | $file->renameTo ( 'new name' ); | | The business code here can choose to save it to the file | system as it has done in the above code. Other options | might include just ignoring the technical provided name | and renaming the file to something else. */
<?php require 'Stack.php'; describe('Stack', function () { Given('stack', function ($initial_contents) { return new Stack($initial_contents); }); context('with no items', function () { given('initial_contents', function () { return []; }); when(function (Stack $stack) { $stack->push(3); }); then(function (Stack $stack) { return $stack->size() === 1; }); }); context('with one item', function () { given('initial_contents', function () { return ['an item']; }); when(function (Stack $stack) { $stack->push('another item'); }); then(function (Stack $stack) { return $stack->size() === 2; }); }); });
<?php use FileSystem\Directory; use FileSystem\FileSystem; when('i want to make a new directory', then(apply(a(function (FileSystem $fileSystem, Directory $directory) { $fileSystem->make($directory); }))));
}); }); context('isolated', function () { then(function ($empty) { return empty($empty); }); }); }); describe('Natural failing assertions', function () { given('foo', 1); given('expected', 2); then(function ($foo, $expected) { return $foo + $foo + 2 * $foo === $expected; }); then(function () { return null == "HI" && true && 1; }); then(function ($foo) { return $foo != 1; }); context('with labels', function () { given('the number of offers', 'offers', 3); given('the price of each offer', 'price', 4); when('I multiply the number with the price', 'total', function ($offers, $price) { return $offers * $price; }); then('I should get 15', function ($total) { return $total === 15; }); }); });
}); }); describe('calling #reportEnd', function () { given('errors', array()); given('labels', array()); given('results', array()); given('an instance of the tap reporter', 'reporter', new TapReporter()); context('no tests were executed', function () { given('total', 0); when('reporter #reportEnd is called', 'result', function ($reporter, $total, $errors, $labels, $results) { ob_start(); $reporter->reportEnd($total, $errors, $labels, $results); return ob_get_clean(); }); then('result should be a valid string', function ($result) { return empty($result); }); }); context('11 tests with no errors was executed', function () { given('total', 11); when('reporter #reportEnd is called', 'result', function ($reporter, $total, $errors, $labels, $results) { ob_start(); $reporter->reportEnd($total, $errors, $labels, $results); return ob_get_clean(); }); then('result should be a valid string', function ($result) { return !(false === strpos($result, '1..11')); }); }); }); });
<?php use FileSystem\Directory; use FileSystem\File; use FileSystem\FileSystem; when('i want to move a file', then(apply(a(function (FileSystem $fileSystem, Directory $directory, File $file) { $fileSystem->move($file, to($directory)); }))));
return !(false === strpos($result, 'rendered error: 1')); }); then('error summaries should have been rendered', function ($result) { return !(false === strpos($result, 'Failed examples:')) && !(false === strpos($result, 'error summary')); }); }); context('5 tests with 2 errors and labels were executed', function () { given('total', 5); given('errors', array(new MockError(), new MockError())); given('labels', array('Error number 1', 'Error number 2')); given('results', array()); when('reporter #reportEnd is called', 'result', function ($reporter, $total, $errors, $labels, $results) { ob_start(); $reporter->reportEnd($total, $errors, $labels, $results); return ob_get_clean(); }); then('result should be a valid string', function ($result) { return !(false === strpos($result, '5 examples, 2 failures')); }); then('errors should have been rendered', function ($result) { return !(false === strpos($result, 'rendered error: 1')) && !(false === strpos($result, 'rendered error: 2')); }); then('labels should have been rendered', function ($result) { return !(false === strpos($result, 'Error number 1')) && !(false === strpos($result, 'Error number 2')); }); then('error summaries should have been rendered', function ($result) { return !(false === strpos($result, 'Failed examples:')) && !(false === strpos($result, 'error summary')); }); }); }); });
<?php /** * Somewhere within your application: * * This is just an example of what you can do. */ namespace FileSystem; class Space extends Directory { } $application->bind('FileSystem\\Space', function (User $user) { return new Space($user->allocatedSpacePath); }); // In your business logic: use FileSystem\Directory; use FileSystem\FileSystem; use FileSystem\Space; when('i want to make a directory inside my space', then(apply(a(function (FileSystem $fileSystem, Directory $directory, Space $space) { $fileSystem->make($directory, inside($space)); }))));
<?php /* |-------------------------------------------------------------------------- | Somewhere within your application. |-------------------------------------------------------------------------- | | This is just an example of what you can do. Input is the input the user of | your application provided. */ use FileSystem\File; $application->bind('FileSystem\\File', function (Input $input) { $file = new File($input->get('name')); $file->write($input->get('contents')); return $file; }); /* |-------------------------------------------------------------------------- | Your business logic. |-------------------------------------------------------------------------- */ use FileSystem\File; when('i want to read a file', then(apply(a(function (File $file) { echo $file->contents; }))));
<?php describe('Natural assertions', function () { when(function () { then(function () { }); }); then(function () { }); });
<?php namespace Business; class Document extends File { } class MyDocuments extends Directory { } /** ------------------------------------------------------------------------- */ use Business\Document; use Business\MyDocuments; use FileSystem\FileSystem; when('i want to write a new document', then(apply(a(function (FileSystem $fileSystem, MyDocuments $myDocuments, Document $document) { $fileSystem->write($document, inside($myDocuments)); }))));
<?php use FileSystem\File; use FileSystem\FileSystem; when('i want to write a file', then(apply(a(function (FileSystem $fileSystem, File $file) { $fileSystem->write($file); }))));