Issues (19)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/WorkflowTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Ddeboer\DataImport\Tests;
4
5
use Ddeboer\DataImport\Exception\WriterException;
6
use Ddeboer\DataImport\Reader\ArrayReader;
7
use Ddeboer\DataImport\Step\ConverterStep;
8
use Ddeboer\DataImport\Step\FilterStep;
9
use Ddeboer\DataImport\Step\MappingStep;
10
use Ddeboer\DataImport\Step\ValueConverterStep;
11
use Ddeboer\DataImport\Writer\ArrayWriter;
12
use Ddeboer\DataImport\Workflow\StepAggregator;
13
use Ddeboer\DataImport\Filter\CallbackFilter;
14
use Ddeboer\DataImport\ValueConverter\CallbackValueConverter;
15
use Ddeboer\DataImport\ItemConverter\CallbackItemConverter;
16
use Ddeboer\DataImport\Writer\CallbackWriter;
17
use Ddeboer\DataImport\Exception\SourceNotFoundException;
18
19
class WorkflowTest extends \PHPUnit_Framework_TestCase
20
{
21
    public function testAddStep()
22
    {
23
        $step = $this->getMock('Ddeboer\DataImport\Step');
24
25
        $this->getWorkflow()->addStep($step);
26
    }
27
28
    public function testAddCallbackWriter()
29
    {
30
        $this->getWorkflow()->addWriter(new CallbackWriter(function($item) {
0 ignored issues
show
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
//            var_dump($item);
32
        }));
33
    }
34
35
    public function testWriterIsPreparedAndFinished()
36
    {
37
        $writer = $this->getMockBuilder('\Ddeboer\DataImport\Writer\CallbackWriter')
38
            ->disableOriginalConstructor()
39
            ->getMock();
40
41
        $writer->expects($this->once())
42
            ->method('prepare');
43
44
        $writer->expects($this->once())
45
            ->method('finish');
46
47
        $this->getWorkflow()->addWriter($writer)
48
            ->process();
49
    }
50
51
    public function testWorkflowWithObjects()
52
    {
53
        $reader = new ArrayReader(array(
54
            new Dummy('foo'),
55
            new Dummy('bar'),
56
            new Dummy('foobar'),
57
        ));
58
59
        $data = array();
60
        $writer = new ArrayWriter($data);
61
62
        $workflow = new StepAggregator($reader);
63
        $workflow->addWriter($writer);
64
65
        $converterStep = new ConverterStep([
66
            function($item) { return array('name' => $item->name); }
67
        ]);
68
69
        $valueStep = new ValueConverterStep();
70
        $valueStep->add('[name]', function($name) { return strrev($name); });
71
72
        $workflow->addStep($converterStep)->addStep($valueStep);
73
        $workflow->process();
74
75
        $this->assertEquals(array(
76
            array('name' => 'oof'),
77
            array('name' => 'rab'),
78
            array('name' => 'raboof')
79
        ), $data);
80
    }
81
82
    /**
83
     * @expectedException \Ddeboer\DataImport\Exception\UnexpectedTypeException
84
     */
85
    public function testItemConverterWhichReturnObjects()
86
    {
87
        $reader = new ArrayReader(array(
88
            new Dummy('foo'),
89
            new Dummy('bar'),
90
            new Dummy('foobar'),
91
        ));
92
93
        $data = array();
94
        $writer = new ArrayWriter($data);
95
96
        $workflow = new StepAggregator($reader);
97
        $workflow->addWriter($writer);
98
99
        $converterStep = new ConverterStep();
100
        $converterStep->add(function($item) { return $item; });
101
102
        $workflow->addStep($converterStep)->process();
103
    }
104
105
    /**
106
     * @expectedException \Ddeboer\DataImport\Exception\UnexpectedTypeException
107
     */
108
    public function testItemConverterWithObjectsAndNoItemConverters()
109
    {
110
        $reader = new ArrayReader(array(
111
            new Dummy('foo'),
112
            new Dummy('bar'),
113
            new Dummy('foobar'),
114
        ));
115
116
        $data = array();
117
        $writer = new ArrayWriter($data);
118
119
        $workflow = new StepAggregator($reader);
120
        $workflow->addWriter($writer);
121
122
        $workflow->process();
123
    }
124
125
    public function testFilterPriority()
126
    {
127
        $offsetFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\OffsetFilter')
128
            ->disableOriginalConstructor()
129
            ->setMethods(array('__invoke'))
130
            ->getMock();
131
        $offsetFilter->expects($this->never())->method('filter');
132
133
        $validatorFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\ValidatorFilter')
134
            ->disableOriginalConstructor()
135
            ->setMethods(array('__invoke'))
136
            ->getMock();
137
        $validatorFilter->expects($this->exactly(3))
138
            ->method('__invoke')
139
            ->will($this->returnValue(false));
140
141
        $filterStep = (new FilterStep())
142
            ->add($offsetFilter)
143
            ->add($validatorFilter);
144
145
        $this->getWorkflow()
146
            ->addStep($filterStep)
147
            ->process();
148
    }
149
150
    public function testFilterPriorityOverride()
151
    {
152
        $offsetFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\OffsetFilter')
153
            ->disableOriginalConstructor()
154
            ->setMethods(array('__invoke'))
155
            ->getMock();
156
        $offsetFilter->expects($this->exactly(3))
157
            ->method('__invoke')
158
            ->will($this->returnValue(false));
159
160
        $validatorFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\ValidatorFilter')
161
            ->disableOriginalConstructor()
162
            ->setMethods(array('__invoke'))
163
            ->getMock();
164
        $validatorFilter->expects($this->never())->method('filter');
165
166
        $filterStep = (new FilterStep())
167
            ->add($offsetFilter, 257)
168
            ->add($validatorFilter);
169
170
        $this->getWorkflow()
171
            ->addStep($filterStep)
172
            ->process();
173
    }
174
175
    public function testExceptionInterfaceThrownFromWriterIsCaught()
176
    {
177
        $originalData = array(array('foo' => 'bar'));
178
        $reader = new ArrayReader($originalData);
179
180
        $array = array();
181
        $writer = $this->getMock('Ddeboer\DataImport\Writer\ArrayWriter', array(), array(&$array));
182
183
        $exception = new SourceNotFoundException("Log me!");
184
185
        $writer->expects($this->once())
186
            ->method('writeItem')
187
            ->with($originalData[0])
188
            ->will($this->throwException($exception));
189
190
        $logger = $this->getMock('Psr\Log\LoggerInterface');
191
        $logger->expects($this->once())
192
            ->method('error')
193
            ->with($exception->getMessage());
194
195
196
        $workflow = new StepAggregator($reader);
197
        $workflow->setLogger($logger);
198
        $workflow->setSkipItemOnFailure(true);
199
        $workflow->addWriter($writer);
200
        $workflow->process();
201
    }
202
203
    public function testWorkflowResultWhenAllSuccessful()
204
    {
205
        $workflow   = $this->getWorkflow();
206
        $result     = $workflow->process();
207
208
        $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
209
        $this->assertInstanceOf('DateTime', $result->getStartTime());
210
        $this->assertInstanceOf('DateTime', $result->getEndTime());
211
        $this->assertInstanceOf('DateInterval', $result->getElapsed());
212
        $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
213
        $this->assertSame(3, $result->getTotalProcessedCount());
214
        $this->assertSame(3, $result->getSuccessCount());
215
        $this->assertSame(0, $result->getErrorCount());
216
        $this->assertFalse($result->hasErrors());
217
        $this->assertEmpty($result->getExceptions());
218
        $this->assertSame(null, $result->getName());
219
    }
220
221
    public function testMultipleMappingsForAnItemAfterAnotherItemConverterwasAdded()
222
    {
223
        $originalData = array(array('foo' => 'bar', 'baz' => 'value'));
224
225
        $outputTestData = array();
226
227
        $writer = new ArrayWriter($outputTestData);
228
        $reader = new ArrayReader($originalData);
229
230
        $workflow = new StepAggregator($reader);
231
232
        $converterStep = new ConverterStep();
233
234
        // add a dummy item converter
235
        $converterStep->add(function($item) { return $item; });
236
237
        $mappingStep = (new MappingStep())
238
            ->map('[foo]', '[bar]')
239
            ->map('[baz]', '[bazzoo]');
240
241
        // add multiple mappings
242
        $workflow
243
            ->addStep($converterStep)
244
            ->addStep($mappingStep)
245
            ->addWriter($writer)
246
            ->process()
247
        ;
248
249
        $this->assertArrayHasKey('bar', $outputTestData[0]);
250
        $this->assertArrayHasKey('bazzoo', $outputTestData[0]);
251
    }
252
253
    public function _testWorkflowResultWithExceptionThrowFromWriter()
254
    {
255
        $workflow   = $this->getWorkflow();
256
        $workflow->setSkipItemOnFailure(true);
257
        $writer     = $this->getMock('Ddeboer\DataImport\Writer\WriterInterface');
258
259
        $e = new WriterException();
260
261
        $writer
262
            ->expects($this->at(1))
263
            ->method('writeItem')
264
            ->with(array('first' => 'James', 'last'  => 'Bond'))
265
            ->will($this->throwException($e));
266
267
        $workflow->addWriter($writer);
268
        $result = $workflow->process();
269
270
        $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
271
        $this->assertInstanceOf('DateTime', $result->getStartTime());
272
        $this->assertInstanceOf('DateTime', $result->getEndTime());
273
        $this->assertInstanceOf('DateInterval', $result->getElapsed());
274
        $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
275
        $this->assertSame(3, $result->getTotalProcessedCount());
276
        $this->assertSame(2, $result->getSuccessCount());
277
        $this->assertSame(1, $result->getErrorCount());
278
        $this->assertTrue($result->hasErrors());
279
        $this->assertSame(array($e), iterator_to_array($result->getExceptions()));
280
        $this->assertSame(null, $result->getName());
281
    }
282
283
    protected function getWorkflow()
284
    {
285
        $reader = new ArrayReader(array(
286
            array(
287
                'first' => 'James',
288
                'last'  => 'Bond'
289
            ),
290
            array(
291
                'first' => 'Miss',
292
                'last'  => 'Moneypenny'
293
            ),
294
            array(
295
                'first' => null,
296
                'last'  => 'Doe'
297
            )
298
        ));
299
300
        return new StepAggregator($reader);
301
    }
302
}
303
304
class Dummy
305
{
306
    public $name;
307
308
    public function __construct($name)
309
    {
310
        $this->name = $name;
311
    }
312
}
313