Completed
Pull Request — master (#4)
by Pavel
10:02
created

StopwatchTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 10
dl 0
loc 98
ccs 63
cts 84
cp 0.75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C testClientClass() 0 74 7
A getCacheDir() 0 4 1
A getSuccessResponseMock() 0 9 1
1
<?php
2
3
namespace Bankiru\Api\Tests;
4
5
use Bankiru\Api\BankiruDoctrineApiBundle;
6
use Bankiru\Api\Client\TraceableClient;
7
use Bankiru\Api\Doctrine\ClientRegistryInterface;
8
use Bankiru\Api\Doctrine\Test\RpcRequestMock;
9
use PHPUnit\Framework\TestCase;
10
use ScayTrase\Api\Rpc\RpcResponseInterface;
11
use Symfony\Bundle\MonologBundle\MonologBundle;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
14
15
final class StopwatchTest extends TestCase
16
{
17
    use ContainerTestTrait;
18
19
    /**
20
     * @group time-sensitive
21
     */
22 1
    public function testClientClass()
23
    {
24 1
        $container = $this->buildContainer(
25
            [
26 1
                new MonologBundle(),
27 1
                new BankiruDoctrineApiBundle(),
28 1
            ],
29
            [
30
                'api_client' => [
31 1
                    'logger'    => ['id' => false],
32 1
                    'profiling' => true,
33 1
                ],
34
            ]
35 1
        );
36
37 1
        self::assertTrue($container->has('rpc.test_client'));
38 1
        self::assertInstanceOf(TraceableClient::class, $container->get('rpc.test_client'));
39
40
        /** @var ClientRegistryInterface $registry */
41 1
        $registry = $container->get('bankiru_api.entity_manager')->getConfiguration()->getClientRegistry();
42 1
        foreach ($registry->all() as $client) {
43 1
            self::assertInstanceOf(TraceableClient::class, $client);
44 1
        }
45
46 1
        $mock = $container->get('rpc.client_mock');
47 1
        $mock->push(
48 1
            $this->getSuccessResponseMock(
49
                (object)[
50 1
                    'id'          => 2,
51 1
                    'payload'     => 'test-payload',
52 1
                    'sub-payload' => 'sub-payload',
53
                ]
54 1
            ),
55 1
            function () {
56 1
                usleep(1000);
57
58 1
                return true;
59
            }
60 1
        );
61
62 1
        $client     = $container->get('rpc.test_client');
63 1
        $request    = new RpcRequestMock('test', []);
64 1
        $collection = $client->invoke($request);
65
66 1
        foreach ($collection as $response) {
67 1
            self::assertTrue($response->isSuccessful());
68 1
        }
69
70 1
        self::assertTrue($collection->getResponse($request)->isSuccessful());
71
72 1
        $stopwatch = $container->get('debug.stopwatch');
73 1
        foreach ($stopwatch->getSections() as $section) {
74 1
            foreach ($section->getEvents() as $event) {
75 1
                self::assertEquals('rpc_call', $event->getCategory());
76 1
                self::assertGreaterThan(0, $event->getMemory());
77 1
                self::assertGreaterThan(0, $event->getDuration());
78 1
                foreach ($event->getPeriods() as $period) {
79 1
                    self::assertGreaterThan(0, $period->getMemory());
80 1
                    self::assertNotNull(0, $period->getDuration());
81 1
                }
82 1
            }
83 1
        }
84
85 1
        $collector = $container->get('bankiru_api.profiler.collector');
86 1
        self::assertCount(1, $collector->getData());
87 1
        foreach ($collector->getData() as $profiler) {
88 1
            self::assertEquals('test_client', $profiler->getClientName());
89 1
            self::assertCount(1, $profiler->getResponses());
90 1
            self::assertCount(1, $profiler->getCalls());
91 1
        }
92
93 1
        self::assertEquals('api_client', $collector->getName());
94 1
        $collector->collect(new Request(), new SymfonyResponse());
95 1
    }
96
97
    /** {@inheritdoc} */
98 1
    protected function getCacheDir()
99
    {
100 1
        return CACHE_DIR;
101
    }
102
103 1
    private function getSuccessResponseMock($result)
104
    {
105 1
        $mock = $this->prophesize(RpcResponseInterface::class);
106 1
        $mock->isSuccessful()->willReturn(true);
107 1
        $mock->getBody()->willReturn($result);
108 1
        $mock->getError()->willReturn(null);
109
110 1
        return $mock->reveal();
111
    }
112
}
113