Issues (457)

Security Analysis    not enabled

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.

src/TestHelpers/UnitTestCase.php (5 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
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 1/6/18
7
 * Time: 7:08 PM
8
 */
9
10
namespace Tfboe\FmLib\TestHelpers;
11
12
use Doctrine\ORM\AbstractQuery;
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\QueryBuilder;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * Class UnitTestCase
20
 * @package Tfboe\FmLib\TestHelpers
21
 */
22
abstract class UnitTestCase extends TestCase
23
{
24
  use ReflectionMethods;
25
  use OnlyTestLogging;
26
27
//<editor-fold desc="Protected Methods">
28
29
  /**
30
   * Creates a stub with a given set of stubbed methods, which will return the given results
31
   * @param string $class the class name
32
   * @param array $methodResults a dictionary mapping method names to results of this methods
33
   * @return MockObject the configured stub
34
   */
35
  protected function createStub(string $class, array $methodResults = []): MockObject
36
  {
37
    $entity = $this->createMock($class);
38
    foreach ($methodResults as $method => $result) {
39
      $entity->method($method)->willReturn($result);
0 ignored issues
show
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
40
    }
41
    return $entity;
42
  }
43
44
  /**
45
   * Creates an empty mock with a getId method
46
   * @param string $class the class to mock
47
   * @param string $entityId the id to assign
48
   * @param string $getterMethod the name of the getter method
49
   * @return \PHPUnit\Framework\MockObject\MockObject the mocked instance
50
   */
51
  protected function createStubWithId(string $class, $entityId = "entity-id", $getterMethod = 'getId')
52
  {
53
    return $this->createStub($class, [$getterMethod => $entityId]);
54
  }
55
56
  /**
57
   * Gets a mock for an entity manager which creates a query builder which will return a query which will return the
58
   * given result.
59
   * @param array $result the result array the query should return
60
   * @param string|null $expectedQuery the expected query if set
61
   * @param string[] $otherMockedMethods list of other methods to mock
62
   * @return MockObject the mocked entity manager
63
   */
64
  protected function getEntityManagerMockForQuery(array $result, ?string $expectedQuery = null,
65
                                                  array $otherMockedMethods = [], $amount = 1)
66
  {
67
    return $this->getEntityManagerMockForQueries(array_fill(0, $amount, $result),
68
      $expectedQuery === null ? [] : array_fill(0, $amount, $expectedQuery), $otherMockedMethods);
69
  }
70
71
  /**
72
   * Gets a mock for an entity manager which creates a query builder which will return a query which will return the
73
   * given result.
74
   * @param array $results the result arrays the queries should return
75
   * @param string[] $expectedQueries the expected queries if set
76
   * @param string[] $otherMockedMethods list of other methods to mock
77
   * @return MockObject the mocked entity manager
78
   */
79
  protected function getEntityManagerMockForQueries(array $results, array $expectedQueries = [],
80
                                                    array $otherMockedMethods = [])
81
  {
82
    $entityManager = $this->getMockForAbstractClass(EntityManager::class, [], '',
83
      false, true, true, array_merge($otherMockedMethods, ['createQueryBuilder']));
84
    assert($expectedQueries == [] || count($results) === count($expectedQueries));
85
    $entityManager->expects(static::exactly(count($results)))->method('createQueryBuilder')->willReturnCallback(
86
      function () use ($entityManager, &$results, &$expectedQueries) {
87
        $queryBuilder = $this->getMockForAbstractClass(QueryBuilder::class, [$entityManager],
88
          '', true, true, true, ['getQuery']);
89
        $query = $this->getMockBuilder(AbstractQuery::class)
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
          ->disableOriginalConstructor()
91
          ->disableOriginalClone()
92
          ->disableArgumentCloning()
93
          ->disallowMockingUnknownTypes()
94
          ->setMethods(['setLockMode', 'getSQL', '_doExecute', 'getResult'])
95
          ->getMock();
96
        $query->expects(static::once())->method('getResult')->willReturn(array_shift($results));
97
        $query->method('setLockMode')->willReturn($query);
0 ignored issues
show
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
98
        if ($expectedQueries !== []) {
99
          $queryBuilder->expects(static::once())->method('getQuery')->willReturnCallback(
100
            function () use ($queryBuilder, $query, &$expectedQueries) {
101
              /** @var QueryBuilder $queryBuilder */
102
              self::assertEquals(array_shift($expectedQueries), $queryBuilder->getDQL());
103
              return $query;
104
            });
105
        } else {
106
          $queryBuilder->expects(static::once())->method('getQuery')->willReturn($query);
107
        }
108
        return $queryBuilder;
109
      }
110
    );
111
    return $entityManager;
112
  }
113
114
  /** @noinspection PhpDocMissingThrowsInspection */
115
  /**
116
   * Gets a mock class (with full implementation). The given arguments are used for the arguments for the constructor.
117
   * If too less arguments are given mocks are created for the rest of the constructor arguments.
118
   * @param string $className the class to mock
119
   * @param array $arguments the arguments to use for the constructor
120
   * @param string[] $mockedMethods the methods to mock in the class
121
   * @return MockObject the mocked object
122
   */
123
  protected final function getMockWithMockedArguments(string $className, array $arguments = [],
124
                                                      array $mockedMethods = []): MockObject
125
  {
126
    /** @noinspection PhpUnhandledExceptionInspection */
127
    $reflection = new \ReflectionClass($className);
128
    $params = $reflection->getConstructor()->getParameters();
129
    $allArguments = $arguments;
130
    for ($i = count($arguments); $i < count($params); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
131
      $allArguments[] = $this->createMock($params[$i]->getClass()->name);
132
    }
133
    return $this->getMockForAbstractClass($className, $allArguments, '', true, true, true, $mockedMethods);
134
  }
135
136
  /** @noinspection PhpDocMissingThrowsInspection */
137
  /**
138
   * Gets a new instance of the given class. The given arguments are used for the arguments for the constructor.
139
   * If too less arguments are given mocks are created for the rest of the constructor arguments.
140
   * @param string $className the class for which to create an instance
141
   * @param array $arguments the arguments to use for the constructor
142
   * @return mixed an instance of the given class
143
   */
144
  protected final function getObjectWithMockedArguments($className, array $arguments = [])
145
  {
146
    /** @noinspection PhpUnhandledExceptionInspection */
147
    $reflection = new \ReflectionClass($className);
148
    $params = $reflection->getConstructor()->getParameters();
149
    $allArguments = $arguments;
150
    for ($i = count($arguments); $i < count($params); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
151
      $allArguments[$i] = $this->createMock($params[$i]->getClass()->name);
152
    }
153
    return new $className(...$allArguments);
154
  }
155
156
  public function tearDown()
157
  {
158
    parent::tearDown();
159
  }
160
//</editor-fold desc="Protected Methods">
161
}