Skip to content

Commit

Permalink
Add Symfony5 support
Browse files Browse the repository at this point in the history
Drop support for PHP <= 7.0
  • Loading branch information
winzou committed Jun 27, 2020
1 parent 9c76471 commit 2448d9a
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 231 deletions.
21 changes: 1 addition & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
sudo: false

branches:
only:
- master

language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm

matrix:
fast_finish: true
include:
- php: 5.3
dist: precise
allow_failures:
- php: 5.3
dist: xenial
- php: 5.3
dist: trusty

before_script:
- if [[ $TRAVIS_PHP_VERSION != hhvm ]]; then phpenv config-rm xdebug.ini; fi;
- phpenv config-rm xdebug.ini;
- composer --no-interaction --prefer-source install

script:
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
}
],
"require": {
"php": ">=5.3.0",
"symfony/event-dispatcher": "~2.1|~3.0|~4.0",
"symfony/property-access": "~2.1|~3.0|~4.0",
"symfony/expression-language": "~2.4|~3.0|~4.0"
"php": "^7.1.3",
"symfony/event-dispatcher": "^4.3|^5.0",
"symfony/property-access": "^4.3|^5.0",
"symfony/expression-language": "^4.3|^5.0"
},
"suggest": {
"twig/twig": "Access the state machine in your twig templates (~1.0)"
"twig/twig": "Access the state machine in your twig templates (^2.10|^3.0)"
},
"require-dev": {
"phpspec/phpspec": "~2.0",
"twig/twig": "~1.0"
"phpspec/phpspec": "^5.0|^6.0",
"twig/twig": "^2.10|^3.0"
},
"autoload": {
"psr-0": { "SM": "src/" }
Expand Down
3 changes: 2 additions & 1 deletion spec/SM/Callback/CallbackSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand All @@ -22,7 +23,7 @@ function let(StateMachineInterface $sm)

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

function it_satisfies_simple_on(TransitionEvent $event)
Expand Down
3 changes: 2 additions & 1 deletion spec/SM/Callback/CascadeTransitionCallbackSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Callback\CascadeTransitionCallback;
use SM\Event\TransitionEvent;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
Expand All @@ -18,7 +19,7 @@ function let(FactoryInterface $factory)

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

function it_applies($factory, TransitionEvent $event, DummyObject $object, StateMachineInterface $sm)
Expand Down
36 changes: 4 additions & 32 deletions spec/SM/Extension/Twig/SMExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace spec\SM\Extension\Twig;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Extension\Twig\SMExtension;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use spec\SM\DummyObject;
use Twig\Extension\ExtensionInterface;

class SMExtensionSpec extends ObjectBehavior
{
Expand All @@ -18,40 +19,11 @@ function let(FactoryInterface $factory, StateMachineInterface $stateMachine)

function it_is_initializable()
{
$this->shouldHaveType('SM\Extension\Twig\SMExtension');
$this->shouldHaveType(SMExtension::class);
}

function it_is_a_twig_extension()
{
$this->shouldBeAnInstanceOf('\Twig_Extension');
}

function it_should_have_a_name()
{
$this->getName()->shouldReturn('sm');
}

function it_provide_sm_can_function(FactoryInterface $factory, StateMachineInterface $stateMachine)
{
$this->can($object = new DummyObject(), 'new', 'simple');

$factory->get($object, 'simple')->shouldHaveBeenCalled();
$stateMachine->can('new')->shouldHaveBeenCalled();
}

function it_provide_sm_getState_function(FactoryInterface $factory, StateMachineInterface $stateMachine)
{
$this->getState($object = new DummyObject(), 'simple');

$factory->get($object, 'simple')->shouldHaveBeenCalled();
$stateMachine->getState()->shouldHaveBeenCalled();
}

function it_provide_sm_getPossibleTransitions_function(FactoryInterface $factory, StateMachineInterface $stateMachine)
{
$this->getPossibleTransitions($object = new DummyObject(), 'simple');

$factory->get($object, 'simple')->shouldHaveBeenCalled();
$stateMachine->getPossibleTransitions()->shouldHaveBeenCalled();
$this->shouldImplement(ExtensionInterface::class);
}
}
14 changes: 8 additions & 6 deletions spec/SM/Factory/FactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
namespace spec\SM\Factory;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Callback\CallbackFactoryInterface;
use SM\Factory\Factory;
use SM\SMException;
use SM\StateMachine\StateMachine;
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'),
'graph1' => array('state_machine_class' => StateMachine::class, 'class' => DummyObject::class),
'graph2' => array('class' => DummyObject::class),
);

function let(EventDispatcherInterface $dispatcher, CallbackFactoryInterface $callbackFactory)
Expand All @@ -22,7 +24,7 @@ function let(EventDispatcherInterface $dispatcher, CallbackFactoryInterface $cal

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

function it_creates_statemachine(DummyObject $object)
Expand All @@ -34,11 +36,11 @@ function it_creates_statemachine(DummyObject $object)

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

function it_throws_exception_when_configuration_doesnt_exist(DummyObject $object)
{
$this->shouldThrow('SM\\SMException')->during('get', array($object, 'non-existing-graph'));
$this->shouldThrow(SMException::class)->during('get', array($object, 'non-existing-graph'));
}
}
44 changes: 24 additions & 20 deletions spec/SM/StateMachine/StateMachineSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use SM\Callback\CallbackFactoryInterface;
use SM\Callback\CallbackInterface;
use SM\Event\SMEvents;
use SM\Event\TransitionEvent;
use SM\SMException;
use SM\StateMachine\StateMachine;
use spec\SM\DummyObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class StateMachineSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -63,19 +66,19 @@ function let(DummyObject $object, EventDispatcherInterface $dispatcher, Callback

function it_is_initializable()
{
$this->shouldHaveType('SM\StateMachine\StateMachine');
$this->shouldHaveType(StateMachine::class);
}

function it_can($object, $dispatcher, $callbackFactory, CallbackInterface $guard)
{
$object->getState()->shouldBeCalled()->willReturn('checkout');
$object->setState(Argument::any())->shouldNotBeCalled();

$dispatcher->dispatch(SMEvents::TEST_TRANSITION, Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$dispatcher->dispatch(Argument::type(TransitionEvent::class), SMEvents::TEST_TRANSITION)->shouldBeCalled();

$callbackFactory->get($this->config['callbacks']['guard']['guard-confirm'])->shouldBeCalled()->willReturn($guard);

$guard->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled()->willReturn(true);
$guard->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled()->willReturn(true);

$this->can('create')->shouldReturn(true);
}
Expand All @@ -95,11 +98,11 @@ function it_is_guarded_and_can($object, $dispatcher, $callbackFactory, CallbackI
$object->getState()->shouldBeCalled()->willReturn('pending');
$object->setState(Argument::any())->shouldNotBeCalled();

$dispatcher->dispatch(SMEvents::TEST_TRANSITION, Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$dispatcher->dispatch(Argument::type(TransitionEvent::class), SMEvents::TEST_TRANSITION)->shouldBeCalled();

$callbackFactory->get($this->config['callbacks']['guard']['guard-confirm'])->shouldBeCalled()->willReturn($guard);

$guard->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled()->willReturn(true);
$guard->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled()->willReturn(true);

$this->can('confirm')->shouldReturn(true);
}
Expand All @@ -109,18 +112,18 @@ function it_is_guarded_and_cannot($object, $dispatcher, $callbackFactory, Callba
$object->getState()->shouldBeCalled()->willReturn('pending');
$object->setState(Argument::any())->shouldNotBeCalled();

$dispatcher->dispatch(SMEvents::TEST_TRANSITION, Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$dispatcher->dispatch(Argument::type(TransitionEvent::class), SMEvents::TEST_TRANSITION)->shouldBeCalled();

$callbackFactory->get($this->config['callbacks']['guard']['guard-confirm'])->shouldBeCalled()->willReturn($guard);

$guard->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled()->willReturn(false);
$guard->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled()->willReturn(false);

$this->can('confirm')->shouldReturn(false);
}

function it_throws_an_exception_if_transition_doesnt_exist_on_can()
{
$this->shouldThrow('SM\\SMException')->during('can', array('non-existing-transition'));
$this->shouldThrow(SMException::class)->during('can', array('non-existing-transition'));
}

function it_applies_transition(
Expand All @@ -135,19 +138,19 @@ function it_applies_transition(
$object->getState()->shouldBeCalled()->willReturn('checkout');
$object->setState('confirmed')->shouldBeCalled();

$dispatcher->dispatch(SMEvents::TEST_TRANSITION, Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$dispatcher->dispatch(SMEvents::PRE_TRANSITION, Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$dispatcher->dispatch(SMEvents::POST_TRANSITION, Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$dispatcher->dispatch(Argument::type(TransitionEvent::class), SMEvents::TEST_TRANSITION)->shouldBeCalled();
$dispatcher->dispatch(Argument::type(TransitionEvent::class), SMEvents::PRE_TRANSITION)->shouldBeCalled();
$dispatcher->dispatch(Argument::type(TransitionEvent::class), SMEvents::POST_TRANSITION)->shouldBeCalled();

$callbackFactory->get($this->config['callbacks']['guard']['guard-confirm'])->shouldBeCalled()->willReturn($guard);
$callbackFactory->get($this->config['callbacks']['before']['from-checkout'])->shouldBeCalled()->willReturn($callback1);
$callbackFactory->get($this->config['callbacks']['after']['on-confirm'])->shouldBeCalled()->willReturn($callback2);
$callbackFactory->get($this->config['callbacks']['after']['to-cancelled'])->shouldBeCalled()->willReturn($callback3);

$guard->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled()->willReturn(true);
$callback1->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$callback2->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$callback3->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled();
$guard->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled()->willReturn(true);
$callback1->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled();
$callback2->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled();
$callback3->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled();

$this->apply('confirm');
}
Expand All @@ -159,7 +162,7 @@ function it_throws_an_exception_if_transition_cannot_be_applied($object, $dispat

$dispatcher->dispatch(Argument::any())->shouldNotBeCalled();

$this->shouldThrow('SM\\SMException')->during('apply', array('confirm'));
$this->shouldThrow(SMException::class)->during('apply', array('confirm'));
}

function it_does_nothing_if_transition_cannot_be_applied_in_soft_mode($object, $dispatcher)
Expand All @@ -174,7 +177,7 @@ function it_does_nothing_if_transition_cannot_be_applied_in_soft_mode($object, $

function it_throws_an_exception_if_transition_doesnt_exist_on_apply()
{
$this->shouldThrow('SM\\SMException')->during('apply', array('non-existing-transition'));
$this->shouldThrow(SMException::class)->during('apply', array('non-existing-transition'));
}

function it_returns_current_state($object)
Expand All @@ -194,13 +197,14 @@ function it_returns_current_object($object)
$this->getObject()->shouldReturn($object);
}

function it_returns_possible_transitions($object, $callbackFactory, CallbackInterface $guard)
function it_returns_possible_transitions($object, $callbackFactory, CallbackInterface $guard, $dispatcher)
{
$object->getState()->shouldBeCalled()->willReturn('checkout');

$callbackFactory->get($this->config['callbacks']['guard']['guard-confirm'])->shouldBeCalled()->willReturn($guard);

$guard->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled()->willReturn(true);
$dispatcher->dispatch(Argument::type(TransitionEvent::class), SMEvents::TEST_TRANSITION)->shouldBeCalled();
$guard->__invoke(Argument::type(TransitionEvent::class))->shouldBeCalled()->willReturn(true);

$this->getPossibleTransitions()->shouldReturn(array('create', 'confirm'));
}
Expand Down
6 changes: 3 additions & 3 deletions src/SM/Callback/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __invoke(TransitionEvent $event)
/**
* {@inheritDoc}
*/
public function isSatisfiedBy(TransitionEvent $event)
public function isSatisfiedBy(TransitionEvent $event): bool
{
$config = $event->getConfig();

Expand All @@ -106,7 +106,7 @@ public function isSatisfiedBy(TransitionEvent $event)
*
* @return bool
*/
protected function isSatisfiedByClause($clause, $value)
protected function isSatisfiedByClause(string $clause, string $value): bool
{
if (0 < count($this->specs[$clause]) && !in_array($value, $this->specs[$clause])) {
return false;
Expand All @@ -125,7 +125,7 @@ protected function isSatisfiedByClause($clause, $value)
*
* @return callable
*/
protected function filterCallable($callable, TransitionEvent $event)
protected function filterCallable($callable, TransitionEvent $event): callable
{
if (is_array($callable) && isset($callable[0]) && is_string($callable[0]) && 'object' === substr($callable[0], 0, 6)) {
$object = $event->getStateMachine()->getObject();
Expand Down
7 changes: 5 additions & 2 deletions src/SM/Callback/CallbackFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class CallbackFactory implements CallbackFactoryInterface
*/
protected $class;

public function __construct($class)
/**
* @throws SMException
*/
public function __construct(string $class)
{
if (!class_exists($class)) {
throw new SMException(sprintf(
Expand All @@ -35,7 +38,7 @@ public function __construct($class)
/**
* {@inheritDoc}
*/
public function get(array $specs)
public function get(array $specs): CallbackInterface
{
if (!isset($specs['do'])) {
throw new SMException(sprintf(
Expand Down
6 changes: 5 additions & 1 deletion src/SM/Callback/CallbackFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace SM\Callback;

use SM\SMException;

interface CallbackFactoryInterface
{
/**
Expand All @@ -19,6 +21,8 @@ interface CallbackFactoryInterface
* @param array $specs
*
* @return CallbackInterface
*
* @throws SMException
*/
public function get(array $specs);
public function get(array $specs): CallbackInterface;
}
Loading

0 comments on commit 2448d9a

Please sign in to comment.