Issues (144)

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/unit/IO/FileReaderTest.php (4 issues)

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
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\IO;
15
16
use ArrayIterator;
17
use Graze\DataFile\Format\CsvFormat;
18
use Graze\DataFile\Format\FormatAwareInterface;
19
use Graze\DataFile\Format\JsonFormat;
20
use Graze\DataFile\Format\Parser\ParserFactoryInterface;
21
use Graze\DataFile\Format\Parser\ParserInterface;
22
use Graze\DataFile\IO\FileReader;
23
use Graze\DataFile\IO\ReaderInterface;
24
use Graze\DataFile\Node\FileNodeInterface;
25
use Graze\DataFile\Node\NodeStreamInterface;
26
use Graze\DataFile\Test\Helper\CreateStreamTrait;
27
use Graze\DataFile\Test\TestCase;
28
use InvalidArgumentException;
29
use Iterator;
30
use Mockery as m;
31
use Psr\Http\Message\StreamInterface;
32
33
class FileReaderTest extends TestCase
34
{
35
    use CreateStreamTrait;
36
37
    /**
38
     * @param resource        $stream
39
     * @param ParserInterface $parser
40
     *
41
     * @return FileReader
42
     */
43 View Code Duplication
    private function buildFactoryReader($stream, ParserInterface $parser)
0 ignored issues
show
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...
44
    {
45
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
46
        $file->shouldReceive('getStream')
47
             ->with('r')
48
             ->andReturn($stream);
49
50
        $format = m::mock(JsonFormat::class)->makePartial();
51
52
        $factory = m::mock(ParserFactoryInterface::class);
53
        $factory->shouldReceive('getParser')
54
                ->with($format)
55
                ->andReturn($parser);
56
57
        return new FileReader($file, $format, $factory);
0 ignored issues
show
$format is of type object<Mockery\Mock>, but the function expects a null|object<Graze\DataFi...Format\FormatInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
    }
59
60
    public function testNodeStreamFileWillGetAStream()
61
    {
62
        $stream = m::mock(StreamInterface::class);
63
64
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
65
        $file->shouldReceive('getStream')
66
             ->with('r')
67
             ->andReturn($stream);
68
69
        $format = new CsvFormat();
70
        $reader = new FileReader($file, $format);
71
72
        static::assertInstanceOf(ReaderInterface::class, $reader);
73
    }
74
75
    public function testFileNodeWillGetAReadStream()
76
    {
77
        $stream = fopen('php://memory', 'r');
78
79
        try {
80
            $file = m::mock(FileNodeInterface::class);
81
            $file->shouldReceive('readStream')
82
                 ->andReturn($stream);
83
84
            $format = new CsvFormat();
85
86
            $reader = new FileReader($file, $format);
87
88
            static::assertInstanceOf(ReaderInterface::class, $reader);
89
        } finally {
90
            fclose($stream);
91
        }
92
    }
93
94 View Code Duplication
    public function testNodeWithNoFormatAndNoFormatSpecifiedWillThrowAnException()
0 ignored issues
show
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...
95
    {
96
        $stream = fopen('php://memory', 'r');
97
98
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
99
        $file->shouldReceive('getStream')
100
             ->with('r')
101
             ->andReturn($stream);
102
103
        static::expectException(InvalidArgumentException::class);
104
        new FileReader($file);
105
    }
106
107
    public function testNodeWithFormatWillUseThatFormat()
108
    {
109
        $stream = fopen('php://memory', 'r');
110
111
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class, FormatAwareInterface::class);
112
        $file->shouldReceive('getStream')
113
             ->with('r')
114
             ->andReturn($stream);
115
116
        $format = new JsonFormat(['fileType' => JsonFormat::JSON_FILE_TYPE_EACH_LINE]);
117
118
        $file->shouldReceive('getFormat')
119
             ->andReturn($format);
120
121
        $reader = new FileReader($file);
122
123
        static::assertInstanceOf(ReaderInterface::class, $reader);
124
    }
125
126 View Code Duplication
    public function testProvidingAParserFactoryWillUseTheFactory()
0 ignored issues
show
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...
127
    {
128
        $stream = fopen('php://memory', 'r');
129
        $iterator = m::mock(Iterator::class);
130
        $parser = m::mock(ParserInterface::class);
131
        $parser->shouldReceive('parse')
132
               ->andReturn($iterator);
133
134
        $reader = $this->buildFactoryReader($stream, $parser);
135
136
        static::assertInstanceOf(ReaderInterface::class, $reader);
137
    }
138
139
    public function testFetch()
140
    {
141
        $iterator = new ArrayIterator(['some', 'text']);
142
        $stream = $this->createStream('some text in a stream');
143
144
        $parser = m::mock(ParserInterface::class);
145
        $parser->shouldReceive('parse')
146
               ->with($stream)
147
               ->andReturn($iterator);
148
149
        $reader = $this->buildFactoryReader($stream, $parser);
150
151
        $actual = $reader->fetch();
152
        static::assertSame($reader, $actual);
153
        static::assertEquals(['some', 'text'], iterator_to_array($actual));
154
    }
155
156
    public function testFetchAll()
157
    {
158
        $iterator = new ArrayIterator(['some', 'text']);
159
        $stream = $this->createStream('some text in a stream');
160
161
        $parser = m::mock(ParserInterface::class);
162
        $parser->shouldReceive('parse')
163
               ->with($stream)
164
               ->andReturn($iterator);
165
166
        $reader = $this->buildFactoryReader($stream, $parser);
167
168
        $actual = $reader->fetchAll();
169
        static::assertEquals(['some', 'text'], $actual);
170
    }
171
}
172