Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Beru committed Oct 17, 2020
1 parent 8557f2e commit 6e0bc70
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
37 changes: 34 additions & 3 deletions Tests/DataCollector/CommandDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,47 @@

use Doctrine\Bundle\MongoDBBundle\DataCollector\CommandDataCollector;
use Doctrine\Bundle\MongoDBBundle\Tests\TestCase;
use Doctrine\ODM\MongoDB\APM\Command;
use Doctrine\ODM\MongoDB\APM\CommandLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use function count;

class CommandDataCollectorTest extends TestCase
{
public function testCollector()
public function testCollector() : void
{
$collector = new CommandDataCollector(new CommandLogger());
$commandLogger = $this->createCommandLogger([
$this->createCommand(['first' => 'command'], 100),
$this->createCommand(['second' => 'command'], 200),
]);

$collector->collect($request = new Request(['group' => '0']), $response = new Response());
$collector = new CommandDataCollector($commandLogger);
$collector->collect(new Request(), new Response());

$this->assertSame(300, $collector->getTime());
$this->assertSame(2, $collector->getCommandCount());
$this->assertSame([
['command' => '{"first":"command"}', 'durationMicros' => 100],
['command' => '{"second":"command"}', 'durationMicros' => 200],
], $collector->getCommands());
}

private function createCommandLogger(array $commands) : CommandLogger
{
$commandLoggerMock = $this->createMock(CommandLogger::class);
$commandLoggerMock->method('count')->willReturn(count($commands));
$commandLoggerMock->method('getAll')->willReturn($commands);

return $commandLoggerMock;
}

private function createCommand(array $command, int $durationMicros) : Command
{
$commandMock = $this->createMock(Command::class);
$commandMock->method('getCommand')->willReturn((object) $command);
$commandMock->method('getDurationMicros')->willReturn($durationMicros);

return $commandMock;
}
}
17 changes: 17 additions & 0 deletions Tests/Hook/BypassFinalHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\Hook;

use DG\BypassFinals;
use PHPUnit\Runner\BeforeTestHook;

class BypassFinalHook implements BeforeTestHook
{
public function executeBeforeTest(string $test) : void
{
BypassFinals::enable();
BypassFinals::setWhitelist(['*\Doctrine\ODM\MongoDB\APM\*']);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"symfony/options-resolver": "^4.3.3|^5.0"
},
"require-dev": {
"dg/bypass-finals": "^1.3",
"doctrine/coding-standard": "^6.0",
"doctrine/data-fixtures": "^1.2.2",
"phpunit/phpunit": "^7.3",
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
stopOnFailure="false"
bootstrap="./Tests/bootstrap.php"
>

<extensions>
<extension class="Doctrine\Bundle\MongoDBBundle\Tests\Hook\BypassFinalHook"/>
</extensions>

<testsuites>
<testsuite name="DoctrineMongoDBBundle Test Suite">
<directory>./Tests/</directory>
Expand Down

0 comments on commit 6e0bc70

Please sign in to comment.