Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added .gitignore

Added test configuration

Updated tests and namespace
  • Loading branch information...
commit 9d304011ed5483dd69208a557c5430d6fb27c1e6 1 parent 61fe2f0
Jérémy Romey authored May 17, 2012
2  .gitignore
... ...
@@ -0,0 +1,2 @@
  1
+vendor
  2
+composer.lock
2  Action/ActionInterface.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Action;
  3
+namespace FreeAgent\WorkflowBundle\Action;
4 4
 
5 5
 interface ActionInterface
6 6
 {
11  Action/Example.php
... ...
@@ -1,11 +0,0 @@
1  
-<?php
2  
-
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Action;
4  
-
5  
-class Example implements ActionInterface
6  
-{
7  
-    public function run($model)
8  
-    {
9  
-        return true;
10  
-    }
11  
-}
4  DependencyInjection/Configuration.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle\DependencyInjection;
  3
+namespace FreeAgent\WorkflowBundle\DependencyInjection;
4 4
 
5 5
 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6 6
 use Symfony\Component\Config\Definition\ConfigurationInterface;
@@ -47,8 +47,8 @@ public function getConfigTreeBuilder()
47 47
                             ->end()
48 48
                         ->end()
49 49
                     ->end()
  50
+                ->end()
50 51
             ->end()
51  
-        ->end()
52 52
         ;
53 53
 
54 54
         return $treeBuilder;
2  DependencyInjection/FreeAgentWorkflowExtension.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle\DependencyInjection;
  3
+namespace FreeAgent\WorkflowBundle\DependencyInjection;
4 4
 
5 5
 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 6
 use Symfony\Component\Config\FileLocator;
2  FreeAgentWorkflowBundle.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle;
  3
+namespace FreeAgent\WorkflowBundle;
4 4
 
5 5
 use Symfony\Component\HttpKernel\Bundle\Bundle;
6 6
 
64  Manager/Manager.php
... ...
@@ -1,17 +1,18 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Manager;
  3
+namespace FreeAgent\WorkflowBundle\Manager;
4 4
 
5 5
 use Symfony\Component\DependencyInjection\Container;
6  
-use FreeAgent\Bundle\WorkflowBundle\Model\ModelInterface;
  6
+use FreeAgent\WorkflowBundle\Model\ModelInterface;
7 7
 
8 8
 class Manager
9 9
 {
10  
-    private $model;
11  
-    private $workflow;
12  
-    private $steps   = array();
13  
-    private $actions = array();
14  
-    private $container;
  10
+    protected $model;
  11
+    protected $workflow;
  12
+    protected $steps   = array();
  13
+    protected $actions = array();
  14
+    protected $container;
  15
+    protected $canReachStep = array();
15 16
 
16 17
     /**
17 18
      * [__construct description]
@@ -129,6 +130,8 @@ public function reachStep($stepName)
129 130
 
130 131
             $this->runStepActions($stepName);
131 132
 
  133
+            $this->canReachStep = array();
  134
+
132 135
             return true;
133 136
         }
134 137
 
@@ -142,36 +145,43 @@ public function reachStep($stepName)
142 145
      */
143 146
     public function canReachStep($stepName)
144 147
     {
145  
-        if ($stepName != $this->getCurrentStepName())
146  
-        {
147  
-            $step        = $this->getStep($stepName);
148  
-            $currentStep = $this->getCurrentStep();
149  
-
150  
-            if (!array_key_exists('possible_next_steps', $currentStep)) {
  148
+        if (!array_key_exists($stepName, $this->canReachStep)) {
151 149
 
152  
-                return false;
153  
-            }
  150
+            $this->canReachStep[$stepName] = false;
154 151
 
155  
-            if (in_array($stepName, $currentStep['possible_next_steps'])) {
  152
+            if ($stepName != $this->getCurrentStepName())
  153
+            {
  154
+                $step        = $this->getStep($stepName);
  155
+                $currentStep = $this->getCurrentStep();
156 156
 
157  
-                if (!array_key_exists('validators', $step)) {
158  
-                    return true;
159  
-                } else {
160  
-                    foreach ($step['validators'] as $validator) {
161  
-                        $validator = $this->container->get($validator);
  157
+                if (array_key_exists('possible_next_steps', $currentStep)) {
  158
+                    if (in_array($stepName, $currentStep['possible_next_steps'])) {
162 159
 
163  
-                        if (false == $validator->validate($this->getModel())) {
  160
+                        if (!array_key_exists('validators', $step)) {
  161
+                            $this->canReachStep[$stepName] = true;
  162
+                        } else {
  163
+                            foreach ($step['validators'] as $validator) {
  164
+                                $validator = $this->getValidator($validator);
164 165
 
165  
-                            return false;
  166
+                                $this->canReachStep[$stepName] = false == $validator->validate($this->getModel()) ? false : true;
  167
+                            }
166 168
                         }
167  
-
168  
-                        return true;
169 169
                     }
170 170
                 }
171 171
             }
172 172
         }
173 173
 
174  
-        return false;
  174
+        return $this->canReachStep[$stepName];
  175
+    }
  176
+
  177
+    public function getValidator($validator)
  178
+    {
  179
+        return $this->container->get($validator);
  180
+    }
  181
+
  182
+    public function getAction($action)
  183
+    {
  184
+        return $this->container->get($action);
175 185
     }
176 186
 
177 187
     /**
@@ -184,7 +194,7 @@ public function runStepActions()
184 194
         if (array_key_exists('actions', $currentStep)) {
185 195
 
186 196
             foreach ($step['actions'] as $action) {
187  
-                $action = $this->container->get($action);
  197
+                $action = $this->getAction($action);
188 198
 
189 199
                 if (false == $action->run($this->getModel())) {
190 200
 
29  Model/Example.php
... ...
@@ -1,29 +0,0 @@
1  
-<?php
2  
-
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Model;
4  
-
5  
-class Example implements ModelInterface
6  
-{
7  
-    private $workflow_step_name;
8  
-    private $workflow_name = 'example';
9  
-
10  
-    public function getWorkflowName()
11  
-    {
12  
-        return $this->workflow_name;
13  
-    }
14  
-
15  
-    public function setWorkflowName($workflowName)
16  
-    {
17  
-        return $this->workflow_name = $workflowName;
18  
-    }
19  
-
20  
-    public function setWorkflowStepName($stepName)
21  
-    {
22  
-        $this->workflow_step_name = $stepName;
23  
-    }
24  
-
25  
-    public function getWorkflowStepName()
26  
-    {
27  
-        return $this->workflow_step_name;
28  
-    }
29  
-}
2  Model/ModelInterface.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Model;
  3
+namespace FreeAgent\WorkflowBundle\Model;
4 4
 
5 5
 interface ModelInterface
6 6
 {
8  Resources/config/services.xml
@@ -5,10 +5,10 @@
5 5
     xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6 6
 
7 7
     <parameters>
8  
-        <parameter key="free_agent_workflow.manager.class">FreeAgent\Bundle\WorkflowBundle\Manager\Manager</parameter>
9  
-        <parameter key="free_agent_workflow.workflow.exemple.class">FreeAgent\Bundle\WorkflowBundle\Workflow\Example</parameter>
10  
-        <parameter key="free_agent_workflow.validator.exemple.class">FreeAgent\Bundle\WorkflowBundle\Validator\Example</parameter>
11  
-        <parameter key="free_agent_workflow.action.exemple.class">FreeAgent\Bundle\WorkflowBundle\Action\Example</parameter>
  8
+        <parameter key="free_agent_workflow.manager.class">FreeAgent\WorkflowBundle\Manager\Manager</parameter>
  9
+        <parameter key="free_agent_workflow.workflow.exemple.class">FreeAgent\WorkflowBundle\Workflow\Example</parameter>
  10
+        <parameter key="free_agent_workflow.validator.exemple.class">FreeAgent\WorkflowBundle\Validator\Example</parameter>
  11
+        <parameter key="free_agent_workflow.action.exemple.class">FreeAgent\WorkflowBundle\Action\Example</parameter>
12 12
     </parameters>
13 13
 
14 14
     <services>
151  Tests/Manager/ManagerTest.php
... ...
@@ -1,18 +1,155 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Tests\Manager;
  3
+namespace FreeAgent\WorkflowBundle\Tests\Manager;
4 4
 
5  
-use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  5
+use Symfony\Component\DependencyInjection\Container;
  6
+use FreeAgent\WorkflowBundle\Manager\Manager;
  7
+use FreeAgent\WorkflowBundle\Model\ModelInterface;
  8
+use FreeAgent\WorkflowBundle\Action\ActionInterface;
  9
+use FreeAgent\WorkflowBundle\Validator\ValidatorInterface;
6 10
 
7  
-use FreeAgent\Bundle\WorkflowBundle\Model\Example as ModelExample;
  11
+class ModelExample implements ModelInterface
  12
+{
  13
+    private $workflow_step_name;
  14
+    private $workflow_name = 'example';
  15
+
  16
+    public function getWorkflowName()
  17
+    {
  18
+        return $this->workflow_name;
  19
+    }
  20
+
  21
+    public function setWorkflowName($workflowName)
  22
+    {
  23
+        return $this->workflow_name = $workflowName;
  24
+    }
  25
+
  26
+    public function setWorkflowStepName($stepName)
  27
+    {
  28
+        $this->workflow_step_name = $stepName;
  29
+    }
  30
+
  31
+    public function getWorkflowStepName()
  32
+    {
  33
+        return $this->workflow_step_name;
  34
+    }
  35
+}
8 36
 
9  
-class ManagerTest extends WebTestCase
  37
+class ActionExample implements ActionInterface
10 38
 {
11  
-    private function getManager()
  39
+    public function run($model)
  40
+    {
  41
+        return true;
  42
+    }
  43
+}
  44
+
  45
+class ValidatorExample implements ValidatorInterface
  46
+{
  47
+    public function validate($model)
  48
+    {
  49
+        return true;
  50
+    }
  51
+}
  52
+
  53
+class ManagerForTest extends Manager
  54
+{
  55
+    public function configureWorkflow($workflowName)
  56
+    {
  57
+        $this->workflow = array(
  58
+            'default_step' => 'draft',
  59
+            'steps' => array(
  60
+                'draft' => array(
  61
+                    'label' => 'Draft',
  62
+                    'actions' => array(
  63
+                        'free_agent_workflow.action.example',
  64
+                    ),
  65
+                    'validators' => array(
  66
+                        'free_agent_workflow.validator.example',
  67
+                        'free_agent_workflow.validator.example',
  68
+                    ),
  69
+                    'possible_next_steps' => array(
  70
+                        'removed',
  71
+                        'validated',
  72
+                    ),
  73
+                ),
  74
+                'removed' => array(
  75
+                    'label' => 'Removed',
  76
+                    'actions' => array(
  77
+                        'free_agent_workflow.action.example',
  78
+                    ),
  79
+                    'validators' => array(
  80
+                        'free_agent_workflow.validator.example',
  81
+                        'free_agent_workflow.validator.example',
  82
+                    ),
  83
+                    'possible_next_steps' => array(
  84
+                        'draft',
  85
+                    ),
  86
+                ),
  87
+                'validated' => array(
  88
+                    'label' => 'Validated',
  89
+                    'actions' => array(
  90
+                        'free_agent_workflow.action.example',
  91
+                    ),
  92
+                    'validators' => array(
  93
+                        'free_agent_workflow.validator.example',
  94
+                    ),
  95
+                    'possible_next_steps' => array(
  96
+                        'published',
  97
+                        'removed',
  98
+                        'draft',
  99
+                    ),
  100
+                ),
  101
+                'published' => array(
  102
+                    'label' => 'Published',
  103
+                    'actions' => array(
  104
+                        'free_agent_workflow.action.example',
  105
+                    ),
  106
+                    'validators' => array(
  107
+                        'free_agent_workflow.validator.example',
  108
+                        'free_agent_workflow.validator.example',
  109
+                    ),
  110
+                    'possible_next_steps' => array(
  111
+                        'unpublished',
  112
+                        'removed',
  113
+                        'draft',
  114
+                    ),
  115
+                ),
  116
+                'unpublished' => array(
  117
+                    'label' => 'Unpublished',
  118
+                    'actions' => array(
  119
+                        'free_agent_workflow.action.example',
  120
+                    ),
  121
+                    'validators' => array(
  122
+                        'free_agent_workflow.validator.example',
  123
+                        'free_agent_workflow.validator.example',
  124
+                    ),
  125
+                    'possible_next_steps' => array(
  126
+                        'published',
  127
+                        'removed',
  128
+                        'draft',
  129
+                    ),
  130
+                ),
  131
+            ),
  132
+        );
  133
+
  134
+        return $this->getWorkflow();
  135
+    }
  136
+
  137
+    public function getValidator($validator)
12 138
     {
13  
-        $client = static::createClient();
  139
+        return new ValidatorExample();
  140
+    }
14 141
 
15  
-        $manager = $client->getContainer()->get('free_agent_workflow.manager');
  142
+    public function getAction($action)
  143
+    {
  144
+        return new ActionExample();
  145
+    }
  146
+}
  147
+
  148
+class ManagerTest extends \PHPUnit_Framework_TestCase
  149
+{
  150
+    private function getManager()
  151
+    {
  152
+        $manager = new ManagerForTest(new Container());
16 153
 
17 154
         $model = new ModelExample();
18 155
 
15  Tests/autoload.php.dist
... ...
@@ -0,0 +1,15 @@
  1
+<?php
  2
+
  3
+$vendorDir = __DIR__.'/../vendor';
  4
+require_once $vendorDir.'/.composer/autoload.php';
  5
+
  6
+spl_autoload_register(function($class) {
  7
+    if (0 === strpos($class, 'FreeAgent\\WorkflowBundle\\')) {
  8
+        $path = __DIR__.'/../'.implode('/', array_slice(explode('\\', $class), 2)).'.php';
  9
+        if (!stream_resolve_include_path($path)) {
  10
+            return false;
  11
+        }
  12
+        require_once $path;
  13
+        return true;
  14
+    }
  15
+});
7  Tests/bootstrap.php
... ...
@@ -0,0 +1,7 @@
  1
+<?php
  2
+
  3
+if (file_exists($file = __DIR__.'/autoload.php')) {
  4
+    require_once $file;
  5
+} elseif (file_exists($file = __DIR__.'/autoload.php.dist')) {
  6
+    require_once $file;
  7
+}
11  Validator/Example.php
... ...
@@ -1,11 +0,0 @@
1  
-<?php
2  
-
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Validator;
4  
-
5  
-class Example implements ValidatorInterface
6  
-{
7  
-    public function validate($model)
8  
-    {
9  
-        return true;
10  
-    }
11  
-}
2  Validator/ValidatorInterface.php
... ...
@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3  
-namespace FreeAgent\Bundle\WorkflowBundle\Validator;
  3
+namespace FreeAgent\WorkflowBundle\Validator;
4 4
 
5 5
 interface ValidatorInterface
6 6
 {
29  phpunit.xml.dist
... ...
@@ -0,0 +1,29 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+
  3
+<phpunit backupGlobals="false"
  4
+  backupStaticAttributes="false"
  5
+  colors="true"
  6
+  convertErrorsToExceptions="true"
  7
+  convertNoticesToExceptions="true"
  8
+  convertWarningsToExceptions="true"
  9
+  processIsolation="false"
  10
+  stopOnFailure="false"
  11
+  syntaxCheck="false"
  12
+  bootstrap="./Tests/bootstrap.php">
  13
+    <testsuites>
  14
+        <testsuite name="FreeAgentWorkflowBundle test suite">
  15
+            <directory suffix="Test.php">./Tests</directory>
  16
+        </testsuite>
  17
+    </testsuites>
  18
+
  19
+    <filter>
  20
+        <whitelist>
  21
+            <directory>./</directory>
  22
+            <exclude>
  23
+                <directory>./Resources</directory>
  24
+                <directory>./Tests</directory>
  25
+                <directory>./vendor</directory>
  26
+            </exclude>
  27
+        </whitelist>
  28
+    </filter>
  29
+</phpunit>

0 notes on commit 9d30401

Please sign in to comment.
Something went wrong with that request. Please try again.