Completed
Pull Request — master (#230)
by Helpful
03:29
created

WorkflowImportExportTest::testFormatWithActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4286
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * Tests for workflow import/export logic.
4
 *
5
 * @author     [email protected]
6
 * @license    BSD License (http://silverstripe.org/bsd-license/)
7
 * @package    advancedworkflow
8
 * @subpackage tests
9
 */
10
class WorkflowImportExportTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
    
13
    public static $fixture_file = 'advancedworkflow/tests/workflowtemplateimport.yml';
14
    
15
    /**
16
     * Utility method, used in tests
17
     * @return \WorkflowDefinition
18
     */
19 View Code Duplication
    protected function createDefinition()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        $definition = new WorkflowDefinition();
22
        $definition->Title = "Dummy Workflow Definition";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<WorkflowDefinition>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
23
        $definition->write();
24
25
        $stepOne = new WorkflowAction();
26
        $stepOne->Title = "Step One";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<WorkflowAction>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
        $stepOne->WorkflowDefID = $definition->ID;
0 ignored issues
show
Documentation introduced by
The property WorkflowDefID does not exist on object<WorkflowAction>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
28
        $stepOne->write();
29
30
        $stepTwo = new WorkflowAction();
31
        $stepTwo->Title = "Step Two";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<WorkflowAction>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
32
        $stepTwo->WorkflowDefID = $definition->ID;
0 ignored issues
show
Documentation introduced by
The property WorkflowDefID does not exist on object<WorkflowAction>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
33
        $stepTwo->write();
34
35
        $transitionOne = new WorkflowTransition();
36
        $transitionOne->Title = 'Step One T1';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<WorkflowTransition>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
        $transitionOne->ActionID = $stepOne->ID;
0 ignored issues
show
Documentation introduced by
The property ActionID does not exist on object<WorkflowTransition>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
38
        $transitionOne->NextActionID = $stepTwo->ID;
0 ignored issues
show
Documentation introduced by
The property NextActionID does not exist on object<WorkflowTransition>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
        $transitionOne->write();
40
41
        return $definition;
42
    }
43
44
    /**
45
     * Create a WorkflowDefinition with some actions. Ensure an expected length of formatted template.
46
     */
47
    public function testFormatWithActions()
48
    {
49
        $definition = $this->createDefinition();
50
        $exporter = Injector::inst()->createWithArgs('WorkflowDefinitionExporter', array($definition->ID));
51
        $member = new Member();
52
        $member->FirstName = 'joe';
53
        $member->Surname = 'bloggs';
54
        $exporter->setMember($member);
55
        $templateData = new ArrayData(array(
56
            'ExportMetaData' => $exporter->ExportMetaData(),
57
            'ExportActions' => $exporter->getDefinition()->Actions()
58
        ));
59
        
60
        $formatted = $exporter->format($templateData);
61
        $numActions = count(preg_split("#\R#", $formatted));
62
        
63
        $this->assertNotEmpty($formatted);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
        // Seems arbitrary, but if no actions, then the resulting YAML file is exactly 18 lines long
65
        $this->assertGreaterThan(18, $numActions);
0 ignored issues
show
Bug introduced by
The method assertGreaterThan() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
    }
67
    
68
    /**
69
     * Create a WorkflowDefinition with NO actions. Ensure an expected length of formatted template.
70
     */
71
    public function testFormatWithoutActions()
72
    {
73
        $definition = $this->createDefinition();
74
        $exporter = Injector::inst()->createWithArgs('WorkflowDefinitionExporter', array($definition->ID));
75
        $member = new Member();
76
        $member->FirstName = 'joe';
77
        $member->Surname = 'bloggs';
78
        $exporter->setMember($member);
79
        $templateData = new ArrayData(array());
80
        
81
        $formatted = $exporter->format($templateData);
82
        $numActions = count(preg_split("#\R#", $formatted));
83
        
84
        // Seems arbitrary, but if no actions, then the resulting YAML file is exactly 18 lines long
85
        $this->assertEquals(18, $numActions);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        
87
        // Ensure outputted YAML has no blank lines, where SS's control structures would normally be
88
        $numBlanks = preg_match("#^\s*$#m", $formatted);
89
        $this->assertEquals(0, $numBlanks);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
    }
91
    
92
    /**
93
     * Tests a badly formatted YAML import for parsing (no headers)
94
     * Note: The available test-cases we can expect to get out of sfYamlParser is limited..
95
     */
96
    public function testParseBadYAMLNoHeaderImport()
97
    {
98
        $importer = new WorkflowDefinitionImporter();
99
        $this->setExpectedException('Exception', 'Invalid YAML format.');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        $source = <<<'EOD'
101
Injector:
102
  ExportedWorkflow:
103
    class: WorkflowTemplate
104
    constructor:
105
      - 'My Workflow 4 20/02/2014 03-12-55'
106
      - 'Exported from localhost on 20/02/2014 03:12:55 by joe bloggs using SilverStripe versions Framework 3.1.2, CMS 3.1.2'
107
      - 0.2
108
      - 0
109
      - 3
110
    properties:
111
      structure:
112
        'Step One':
113
          type: WorkflowAction
114
          transitions:
115
            - Step One T1: 'Step Two'
116
        'Step Two':
117
          type: WorkflowAction
118
  WorkflowService:
119
    properties:
120
      templates:
121
        - %$ExportedWorkflow
122
EOD;
123
        
124
        $importer->parseYAMLImport($source);
125
    }
126
    
127
    /**
128
     * Tests a badly formatted YAML import for parsing (missing YML colon)
129
     * Note: The available test-cases we can expect to get out of sfYamlParser is limited..
130
     */
131
    public function testParseBadYAMLMalformedImport()
132
    {
133
        $importer = new WorkflowDefinitionImporter();
134
        $this->setExpectedException('ValidationException', 'Invalid YAML format. Unable to parse.');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
        $source = <<<'EOD'
136
---
137
Name: exportedworkflow
138
---
139
Injector:
140
  ExportedWorkflow:
141
    class: WorkflowTemplate
142
    constructor:
143
      - 'My Workflow 4 20/02/2014 03-12-55'
144
      - 'Exported from localhost on 20/02/2014 03-12-55 by joe bloggs using SilverStripe versions Framework 3.1.2, CMS 3.1.2'
145
      - 0.2
146
      - 0
147
      - 3
148
    properties:
149
      structure:
150
        'Step One'
151
          type: WorkflowAction
152
          transitions:
153
            - Step One T1: 'Step Two'
154
        'Step Two':
155
          type: WorkflowAction
156
  WorkflowService:
157
    properties:
158
      templates:
159
        - %$ExportedWorkflow
160
EOD;
161
        
162
        $importer->parseYAMLImport($source);
163
    }
164
    
165
    /**
166
     * Tests a well-formatted YAML import for parsing
167
     * Note: The available test-cases we can expect to get out of sfYamlParser is limited..
168
     */
169
    public function testParseGoodYAMLImport()
170
    {
171
        $importer = new WorkflowDefinitionImporter();
172
        $source = <<<'EOD'
173
---
174
Name: exportedworkflow
175
---
176
Injector:
177
  ExportedWorkflow:
178
    class: WorkflowTemplate
179
    constructor:
180
      - 'My Workflow 4 20/02/2014 03-12-55'
181
      - 'Exported from localhost on 20/02/2014 03-12-55 by joe bloggs using SilverStripe versions Framework 3.1.2, CMS 3.1.2'
182
      - 0.2
183
      - 0
184
      - 3
185
    properties:
186
      structure:
187
        'Step One':
188
          type: WorkflowAction
189
          transitions:
190
            - Step One T1: 'Step Two'
191
        'Step Two':
192
          type: WorkflowAction
193
  WorkflowService:
194
    properties:
195
      templates:
196
        - %$ExportedWorkflow
197
EOD;
198
        
199
        $this->assertNotEmpty($importer->parseYAMLImport($source));
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200
    }
201
    
202
    /**
203
     * Given no ImportedWorkflowTemplate fixture/input data, tests an empty array is returned 
204
     * by WorkflowDefinitionImporter#getImportedWorkflows()
205
     */
206
    public function testGetImportedWorkflowsNone()
207
    {
208
        $this->clearFixtures();
209
        $importer = new WorkflowDefinitionImporter();
210
        $imports = $importer->getImportedWorkflows();
211
        $this->assertEmpty($imports);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
    }
213
    
214
    /**
215
     * Given a single ImportedWorkflowTemplate fixture/input data, tests an non-empty array is returned 
216
     * by WorkflowDefinitionImporter#getImportedWorkflows()
217
     */
218
    public function testGetImportedWorkflowsOne()
219
    {
220
        $name = 'My Workflow 21/02/2014 09-01-29';
221
        // Pretend a ImportedWorkflowTemplate object has been created by WorkflowBulkLoader
222
        $this->objFromFixture('ImportedWorkflowTemplate', 'Import01');
223
        
224
        $importer = singleton('WorkflowDefinitionImporter');
225
        $import = $importer->getImportedWorkflows($name);
226
227
        $this->assertNotEmpty($import);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
228
        $this->assertInstanceOf('WorkflowTemplate', $import);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
229
        $this->assertEquals(1, count($import));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
230
        $this->assertEquals($name, $import->getName());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
231
    }
232
    
233
    /**
234
     * Given many ImportedWorkflowTemplate fixture/input data, tests an non-empty array is returned 
235
     * by WorkflowDefinitionImporter#getImportedWorkflows()
236
     */
237
    public function testGetImportedWorkflowsMany()
238
    {
239
        // Pretend some ImportedWorkflowTemplate objects have been created by WorkflowBulkLoader
240
        $this->objFromFixture('ImportedWorkflowTemplate', 'Import02');
241
        $this->objFromFixture('ImportedWorkflowTemplate', 'Import03');
242
        
243
        $importer = singleton('WorkflowDefinitionImporter');
244
        $imports = $importer->getImportedWorkflows();
245
246
        $this->assertNotEmpty($imports);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
        $this->assertInternalType('array', $imports);
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
248
        $this->assertGreaterThan(1, count($imports));
0 ignored issues
show
Bug introduced by
The method assertGreaterThan() does not seem to exist on object<WorkflowImportExportTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
249
    }
250
}
251