Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
winzou committed May 26, 2014
0 parents commit 851b579
Show file tree
Hide file tree
Showing 24 changed files with 1,469 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
vendor
bin
composer.lock
18 changes: 18 additions & 0 deletions .travis.yml
@@ -0,0 +1,18 @@
branches:
only:
- master

language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- composer --no-interaction --prefer-source install

script:
- bin/phpspec run -f dot
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2014 Alexandre Bacco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
@@ -0,0 +1,19 @@
A very lightweight yet powerful PHP state machine
=================================================

Define your states, define your transitions and your callbacks: we do the rest.
The era of hard-coded states is over!

[![Build Status](https://travis-ci.org/winzou/state-machine.svg?branch=master)](https://travis-ci.org/winzou/state-machine)

Installation
---------------

### Installation (via composer)
```js
{
"require": {
"winzou/state-machine": "dev-master"
}
}
```
33 changes: 33 additions & 0 deletions composer.json
@@ -0,0 +1,33 @@
{
"name": "winzou/state-machine",
"description": "A very lightweight yet powerful PHP state machine",
"keywords": ["statemachine", "state", "event", "callback"],
"homepage": "https://github.com/winzou/StateMachine",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Alexandre Bacco",
"email": "alexandre.bacco@gmail.com",
"homepage": "http://alex.bacco.fr"
}
],
"require": {
"php": ">=5.3.0",
"symfony/event-dispatcher": "~2.1",
"symfony/property-access": "~2.1",
"symfony/expression-language": "~2.4"
},
"require-dev": {
"phpspec/phpspec": "~2.0"
},
"autoload": {
"psr-0": { "SM": "src/" }
},
"autoload-dev": {
"psr-4": { "spec\\": "spec/" }
},
"config": {
"bin-dir": "bin"
}
}
151 changes: 151 additions & 0 deletions spec/SM/Callback/CallbackSpec.php
@@ -0,0 +1,151 @@
<?php

namespace spec\SM\Callback;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Event\TransitionEvent;
use SM\StateMachine\StateMachineInterface;

class CallbackSpec extends ObjectBehavior
{
protected $specs = array();
protected $callable;
protected $sm;

function let(StateMachineInterface $sm)
{
$sm->getState()->willReturn('checkout');

$this->beConstructedWith($this->specs, $this->callable);
}

function it_is_initializable()
{
$this->shouldHaveType('SM\Callback\Callback');
}

function it_satisfies_simple_on(TransitionEvent $event)
{
$specs = array('on' => 'tested-transition');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('dummy'), 'dummy'));
$event->getTransition()->willReturn('tested-transition');
$event->getState()->willReturn('dummy');

$this->isSatisfiedBy($event)->shouldReturn(true);
}

function it_doesnt_satisfies_simple_on(TransitionEvent $event)
{
$specs = array('on' => 'tested-transition');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('dummy'), 'dummy'));
$event->getTransition()->willReturn('tested-transition-not-matching');

$this->isSatisfiedBy($event)->shouldReturn(false);
}

function it_satisfies_simple_from(TransitionEvent $event)
{
$specs = array('from' => 'tested-state');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('tested-state'), 'dummy'));
$event->getTransition()->willReturn('dummy');
$event->getState()->willReturn('tested-state');

$this->isSatisfiedBy($event)->shouldReturn(true);
}

function it_doesnt_satisfies_simple_from(TransitionEvent $event)
{
$specs = array('from' => 'tested-state');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('tested-state-not-matching'), 'dummy'));
$event->getTransition()->willReturn('dummy');
$event->getState()->willReturn('tested-state-not-matching');

$this->isSatisfiedBy($event)->shouldReturn(false);
}

function it_satisfies_simple_to(TransitionEvent $event)
{
$specs = array('to' => 'tested-state');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('dummy'), 'tested-state'));
$event->getTransition()->willReturn('dummy');
$event->getState()->willReturn('dummy');

$this->isSatisfiedBy($event)->shouldReturn(true);
}

function it_doesnt_satisfies_simple_to(TransitionEvent $event)
{
$specs = array('from' => 'tested-state');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('tested-state-not-matching'), 'dummy'));
$event->getTransition()->willReturn('dummy');
$event->getState()->willReturn('dummy');

$this->isSatisfiedBy($event)->shouldReturn(false);
}

function it_satisfies_complex_specs(TransitionEvent $event)
{
$specs = array('to' => 'tested-state-to', 'from' => 'tested-state-from', 'on' => 'tested-transition');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('tested-state-from'), 'tested-state-to'));
$event->getTransition()->willReturn('tested-transition');
$event->getState()->willReturn('tested-state-from');

$this->isSatisfiedBy($event)->shouldReturn(true);
}

function it_doesnt_satisfies_wrong_from(TransitionEvent $event)
{
$specs = array('to' => 'tested-state-to', 'from' => 'tested-wrong', 'on' => 'tested-transition');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('dummy'), 'tested-state-to'));
$event->getTransition()->willReturn('tested-transition');
$event->getState()->willReturn('dummy');

$this->isSatisfiedBy($event)->shouldReturn(false);
}

function it_doesnt_satisfies_wrong_to(TransitionEvent $event)
{
$specs = array('to' => 'tested-wrong', 'from' => 'tested-state-from', 'on' => 'tested-transition');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('tested-state-from'), 'dummy'));
$event->getTransition()->willReturn('tested-transition');
$event->getState()->willReturn('tested-state-from');

$this->isSatisfiedBy($event)->shouldReturn(false);
}

function it_doesnt_satisfies_wrong_on(TransitionEvent $event)
{
$specs = array('to' => 'tested-state-to', 'from' => 'tested-state-from', 'on' => 'tested-wrong');
$this->beConstructedWith($specs, $this->callable);

$event->getConfig()->willReturn($this->getConfig(array('tested-state-from'), 'tested-state-to'));
$event->getTransition()->willReturn('dummy');
$event->getState()->willReturn('tested-state-from');

$this->isSatisfiedBy($event)->shouldReturn(false);
}

protected function getConfig($from = array(), $to)
{
return array('from' => $from, 'to' => $to);
}
}
72 changes: 72 additions & 0 deletions spec/SM/Callback/CascadeTransitionCallbackSpec.php
@@ -0,0 +1,72 @@
<?php

namespace spec\SM\Callback;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Event\TransitionEvent;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use spec\SM\DummyObject;

class CascadeTransitionCallbackSpec extends ObjectBehavior
{
function let(FactoryInterface $factory)
{
$this->beConstructedWith($factory);
}

function it_is_initializable()
{
$this->shouldHaveType('SM\Callback\CascadeTransitionCallback');
}

function it_applies($factory, TransitionEvent $event, DummyObject $object, StateMachineInterface $sm)
{
$factory->get($object, 'graph')->willReturn($sm);

$sm->can('transition')->willReturn(true);
$sm->apply('transition', true)->shouldBeCalled();

$this->apply($object, $event, 'transition', 'graph');
}

function it_applies_with_default_graph(
$factory,
TransitionEvent $event,
DummyObject $object,
StateMachineInterface $sm1,
StateMachineInterface $sm2
) {
$event->getStateMachine()->willReturn($sm2);

$sm2->getGraph()->willReturn('graph');

$factory->get($object, 'graph')->willReturn($sm1);

$sm1->can('transition')->willReturn(true);
$sm1->apply('transition', true)->shouldBeCalled();

$this->apply($object, $event, 'transition');
}

function it_applies_with_default_graph_and_default_transition(
$factory,
TransitionEvent $event,
DummyObject $object,
StateMachineInterface $sm1,
StateMachineInterface $sm2
) {
$event->getStateMachine()->willReturn($sm2);
$event->getTransition()->willReturn('transition');

$sm2->getGraph()->willReturn('graph');

$factory->get($object, 'graph')->willReturn($sm1);

$sm1->can('transition')->willReturn(true);
$sm1->apply('transition', true)->shouldBeCalled();

$this->apply($object, $event);
}
}
16 changes: 16 additions & 0 deletions spec/SM/DummyObject.php
@@ -0,0 +1,16 @@
<?php

namespace spec\SM;

class DummyObject
{
public function getState()
{

}

public function setState()
{

}
}
44 changes: 44 additions & 0 deletions spec/SM/Factory/FactorySpec.php
@@ -0,0 +1,44 @@
<?php

namespace spec\SM\Factory;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Callback\CallbackFactoryInterface;
use spec\SM\DummyObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class FactorySpec extends ObjectBehavior
{
protected $configs = array(
'graph1' => array('state_machine_class' => 'SM\\StateMachine\\StateMachine', 'class' => 'spec\\SM\\DummyObject'),
'graph2' => array('class' => 'spec\\SM\\DummyObject'),
);

function let(EventDispatcherInterface $dispatcher, CallbackFactoryInterface $callbackFactory)
{
$this->beConstructedWith($this->configs, $dispatcher, $callbackFactory);
}

function it_is_initializable()
{
$this->shouldHaveType('SM\Factory\Factory');
}

function it_creates_statemachine(DummyObject $object)
{
$graph = 'graph1';

$this->get($object, $graph)->shouldReturnAnInstanceOf($this->configs[$graph]['state_machine_class']);
}

function it_creates_statemachine_with_default_class(DummyObject $object)
{
$this->get($object, 'graph2')->shouldReturnAnInstanceOf('SM\\StateMachine\\StateMachine');
}

function it_throws_exception_when_configuration_doesnt_exist(DummyObject $object)
{
$this->shouldThrow('SM\\SMException')->during('get', array($object, 'non-existing-graph'));
}
}

0 comments on commit 851b579

Please sign in to comment.