doctrine /
DoctrineBundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
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 Doctrine\Bundle\DoctrineBundle\Tests\DataCollector; |
||
| 4 | |||
| 5 | use Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector; |
||
| 6 | use Doctrine\ORM\Mapping\ClassMetadataInfo; |
||
| 7 | use PHPUnit\Framework\TestCase; |
||
| 8 | use ReflectionClass; |
||
| 9 | use Symfony\Component\HttpFoundation\Request; |
||
| 10 | use Symfony\Component\HttpFoundation\Response; |
||
| 11 | |||
| 12 | class DoctrineDataCollectorTest extends TestCase |
||
| 13 | { |
||
| 14 | const FIRST_ENTITY = 'TestBundle\Test\Entity\Test1'; |
||
| 15 | const SECOND_ENTITY = 'TestBundle\Test\Entity\Test2'; |
||
| 16 | |||
| 17 | public function testCollectEntities() : void |
||
| 18 | { |
||
| 19 | $manager = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
||
| 20 | $config = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock(); |
||
| 21 | $factory = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory') |
||
| 22 | ->setMethods(['getLoadedMetadata'])->getMockForAbstractClass(); |
||
| 23 | $collector = $this->createCollector(['default' => $manager]); |
||
| 24 | |||
| 25 | $manager->expects($this->any()) |
||
| 26 | ->method('getMetadataFactory') |
||
| 27 | ->will($this->returnValue($factory)); |
||
| 28 | $manager->expects($this->any()) |
||
| 29 | ->method('getConfiguration') |
||
| 30 | ->will($this->returnValue($config)); |
||
| 31 | |||
| 32 | $config->expects($this->once()) |
||
| 33 | ->method('isSecondLevelCacheEnabled') |
||
| 34 | ->will($this->returnValue(false)); |
||
| 35 | |||
| 36 | $metadatas = [ |
||
| 37 | $this->createEntityMetadata(self::FIRST_ENTITY), |
||
| 38 | $this->createEntityMetadata(self::SECOND_ENTITY), |
||
| 39 | $this->createEntityMetadata(self::FIRST_ENTITY), |
||
| 40 | ]; |
||
| 41 | $factory->expects($this->once()) |
||
| 42 | ->method('getLoadedMetadata') |
||
| 43 | ->will($this->returnValue($metadatas)); |
||
| 44 | |||
| 45 | $collector->collect(new Request(), new Response()); |
||
| 46 | |||
| 47 | $entities = $collector->getEntities(); |
||
| 48 | $this->assertArrayHasKey('default', $entities); |
||
| 49 | $this->assertCount(2, $entities['default']); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testDoesNotCollectEntities() : void |
||
| 53 | { |
||
| 54 | $manager = $this->createMock('Doctrine\ORM\EntityManager'); |
||
| 55 | $config = $this->createMock('Doctrine\ORM\Configuration'); |
||
| 56 | $collector = $this->createCollector(['default' => $manager], false); |
||
| 57 | |||
| 58 | $manager->expects($this->never()) |
||
| 59 | ->method('getMetadataFactory'); |
||
| 60 | $manager->method('getConfiguration') |
||
| 61 | ->will($this->returnValue($config)); |
||
| 62 | |||
| 63 | $collector->collect(new Request(), new Response()); |
||
| 64 | |||
| 65 | $this->assertEmpty($collector->getMappingErrors()); |
||
| 66 | $this->assertEmpty($collector->getEntities()); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testGetGroupedQueries() : void |
||
| 70 | { |
||
| 71 | $logger = $this->getMockBuilder('Doctrine\DBAL\Logging\DebugStack')->getMock(); |
||
| 72 | $logger->queries = []; |
||
|
0 ignored issues
–
show
|
|||
| 73 | $logger->queries[] = [ |
||
|
0 ignored issues
–
show
Accessing
queries on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
| 74 | 'sql' => 'SELECT * FROM foo WHERE bar = :bar', |
||
| 75 | 'params' => [':bar' => 1], |
||
| 76 | 'types' => null, |
||
| 77 | 'executionMS' => 32, |
||
| 78 | ]; |
||
| 79 | $logger->queries[] = [ |
||
|
0 ignored issues
–
show
Accessing
queries on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
| 80 | 'sql' => 'SELECT * FROM foo WHERE bar = :bar', |
||
| 81 | 'params' => [':bar' => 2], |
||
| 82 | 'types' => null, |
||
| 83 | 'executionMS' => 25, |
||
| 84 | ]; |
||
| 85 | $collector = $this->createCollector([]); |
||
| 86 | $collector->addLogger('default', $logger); |
||
|
0 ignored issues
–
show
$logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Logging\DebugStack>.
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...
|
|||
| 87 | $collector->collect(new Request(), new Response()); |
||
| 88 | $groupedQueries = $collector->getGroupedQueries(); |
||
| 89 | $this->assertCount(1, $groupedQueries['default']); |
||
| 90 | $this->assertSame('SELECT * FROM foo WHERE bar = :bar', $groupedQueries['default'][0]['sql']); |
||
| 91 | $this->assertSame(2, $groupedQueries['default'][0]['count']); |
||
| 92 | |||
| 93 | $logger->queries[] = [ |
||
|
0 ignored issues
–
show
Accessing
queries on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
| 94 | 'sql' => 'SELECT * FROM bar', |
||
| 95 | 'params' => [], |
||
| 96 | 'types' => null, |
||
| 97 | 'executionMS' => 25, |
||
| 98 | ]; |
||
| 99 | $collector->collect(new Request(), new Response()); |
||
| 100 | $groupedQueries = $collector->getGroupedQueries(); |
||
| 101 | $this->assertCount(2, $groupedQueries['default']); |
||
| 102 | $this->assertSame('SELECT * FROM bar', $groupedQueries['default'][1]['sql']); |
||
| 103 | $this->assertSame(1, $groupedQueries['default'][1]['count']); |
||
| 104 | } |
||
| 105 | |||
| 106 | private function createEntityMetadata(string $entityFQCN) : ClassMetadataInfo |
||
| 107 | { |
||
| 108 | $metadata = new ClassMetadataInfo($entityFQCN); |
||
| 109 | $metadata->name = $entityFQCN; |
||
| 110 | $metadata->reflClass = new ReflectionClass('stdClass'); |
||
| 111 | |||
| 112 | return $metadata; |
||
| 113 | } |
||
| 114 | |||
| 115 | private function createCollector(array $managers, bool $shouldValidateSchema = true) : DoctrineDataCollector |
||
| 116 | { |
||
| 117 | $registry = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry')->getMock(); |
||
| 118 | $registry |
||
| 119 | ->expects($this->any()) |
||
| 120 | ->method('getConnectionNames') |
||
| 121 | ->will($this->returnValue(['default' => 'doctrine.dbal.default_connection'])); |
||
| 122 | $registry |
||
| 123 | ->expects($this->any()) |
||
| 124 | ->method('getManagerNames') |
||
| 125 | ->will($this->returnValue(['default' => 'doctrine.orm.default_entity_manager'])); |
||
| 126 | $registry |
||
| 127 | ->expects($this->any()) |
||
| 128 | ->method('getManagers') |
||
| 129 | ->will($this->returnValue($managers)); |
||
| 130 | |||
| 131 | return new DoctrineDataCollector($registry, $shouldValidateSchema); |
||
|
0 ignored issues
–
show
$registry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Persistence\ManagerRegistry>.
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...
|
|||
| 132 | } |
||
| 133 | } |
||
| 134 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: