The document provides details about how PhpSpec works under the hood and describes extension points that allow customizing PhpSpec's behavior. The key points are:
1. PhpSpec is a Symfony console application that initializes a service container and loads configuration from phpspec.yml.
2. The service container is used to register core services like IO, event dispatchers, generators, and commands.
3. Extensions can customize PhpSpec by registering additional services using common prefixes, replacing existing services, or adding new commands/formatters.
4. Examples describe object behavior in specifications, which are grouped into suites that are then located, loaded, and executed when running Php
2. About me
PHP Software engineer
KrakDevs meetups organizer
Huge fan of BDD/TDD
Part of Coduo
Web:
https://github.com/norzechowicz
https://twitter.com/norzechowicz
http://coduo.pl
http://krakdevs.pl
Norbert Orzechowicz
12. Create service container
<?php!
//phpspec/src/PhpSpec/Console/Application.php!
!
use SymfonyComponentConsoleApplication as BaseApplication;!
!
/**!
* The command line application entry point!
*/!
class Application extends BaseApplication!
{!
public function __construct($version)!
{!
$this->container = new ServiceContainer;!
parent::__construct('phpspec', $version);!
}!
}!
In the name of dependency injection!
13. Service Container
<?php!
///phpspec/src/PhpSpec/ServiceContainer.php!
namespace PhpSpec;!
!
class ServiceContainer!
{!
// Sets a object or a callback for the object creation. !
// A new object will be created every time!
public function set($id, $value);!
!
// Sets a object or a callback for the object creation. !
// The same object will be returned every time!
public function setShared($id, $callable);!
!
// Retrieves a service from the container!
public function get($id);!
!
// Retrieves a list of services of a given prefix!
public function getByPrefix($prefix);!
}!
Explanation of the most important methods
14. Register service example
<?php!
///phpspec/src/Acme/MyClass.php!
namespace Acme;!
!
class MyClass!
{!
public function setup(ServiceContainer $container)!
{!
$container->set('acme.service.foo', function ($c) {!
return new Foo();!
});!
!
$container->setShared('acme.service.bar', function ($c) {!
return new Bar();!
});!
!
$container->getByPrefix('acme.service');!
// will return acme.service.foo as Foo incance &
! ! ! // acme.service.bar as Bar instance!
}!
}!
35. Yes, functions that describe object behavior
(tests)
Suite
FooClassSpec.php - Specification
function it_is_awesome() - Example
function it_do_awesome_stuff() - Example
function it_is_famous() - Example
37. Run command simplified algorithm
1. Localize suite - Locator
2. Load suite - Loader
3. Run suite - Suite Runner
4. Run specifications - Specification Runner
5. Run examples - Example Runner
45. But what can I do with it?
Register event listeners as a services
Replace existing services
Register custom example maintainers
Register new fomatters
Register new code generators
Replace suite locator
Register new matchers
Register new commands
!
And many many more…
53. List of prefixes that you can use by default.
event_dispatcher.listeners - EventSubscriberInterfac
code_generator.generators - GeneratorInterfac
formatter.presenter.differ.engines - DifferEngineInterface
locator.locators - ResourceLocatorInterface
runner.maintaners - MaintainerInterface
console.commands - Command