QueryBusTests   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 36.51 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 8
dl 23
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateWithoutQueryHandlerMiddleware() 12 12 1
B testDispatchChainedMiddlewares() 0 26 1
A testDispatchWithInvalidQuery() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Cqrs\Tests\Units\Query;
13
14
use Cubiche\Core\Bus\Exception\NotFoundException;
15
use Cubiche\Core\Bus\Middlewares\Locking\LockingMiddleware;
16
use Cubiche\Core\Bus\Tests\Units\BusTests;
17
use Cubiche\Core\Cqrs\Query\QueryBus;
18
use Cubiche\Core\Cqrs\Tests\Fixtures\FooMessage;
19
use Cubiche\Core\Cqrs\Tests\Fixtures\JsonEncodeMiddleware;
20
use Cubiche\Core\Cqrs\Tests\Fixtures\Query\NearbyVenuesQuery;
21
use Cubiche\Core\Cqrs\Tests\Fixtures\Query\PublishedPostsQuery;
22
use Cubiche\Core\Cqrs\Tests\Fixtures\Query\VenuesQueryHandler;
23
24
/**
25
 * QueryBusTests class.
26
 *
27
 * @author Ivannis Suárez Jerez <[email protected]>
28
 */
29
class QueryBusTests extends BusTests
30
{
31
    /**
32
     * Test create without query handler middleware.
33
     */
34 View Code Duplication
    public function testCreateWithoutQueryHandlerMiddleware()
0 ignored issues
show
Duplication introduced by
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...
35
    {
36
        $this
37
            ->given($middleware = new LockingMiddleware())
38
            ->and($queryBus = new QueryBus([$middleware]))
39
            ->then()
40
                ->exception(function () use ($queryBus) {
41
                    $queryBus->dispatch(new PublishedPostsQuery(new \DateTime()));
42
                })
43
                ->isInstanceOf(NotFoundException::class)
44
        ;
45
    }
46
47
    /**
48
     * Test dispatch chained middlewares.
49
     */
50
    public function testDispatchChainedMiddlewares()
51
    {
52
        $this
53
            ->given($queryBus = QueryBus::create())
54
            ->and($query = new NearbyVenuesQuery($this->faker->latitude(), $this->faker->longitude()))
55
            ->and($queryHandler = new VenuesQueryHandler())
56
            ->and($queryBus->addHandler($query->named(), $queryHandler))
57
            ->and($result = $queryBus->dispatch($query))
58
            ->then()
59
                ->array($result)
60
                    ->hasSize(2)
61
        ;
62
63
        $this
64
            ->given($queryBus = QueryBus::create())
65
            ->and($queryBus->addMiddlewareAfter(new JsonEncodeMiddleware(), $queryBus->handlerMiddleware()))
66
            ->and($query = new NearbyVenuesQuery($this->faker->latitude(), $this->faker->longitude()))
67
            ->and($queryHandler = new VenuesQueryHandler())
68
            ->and($queryBus->addHandler($query->named(), $queryHandler))
69
            ->and($result = $queryBus->dispatch($query))
70
            ->then()
71
                ->string($result)
72
                    ->isNotEmpty()
73
                    ->isEqualTo(json_encode($queryHandler->aroundVenues($query)))
74
        ;
75
    }
76
77
    /**
78
     * Test dispatch with invalid query.
79
     */
80 View Code Duplication
    public function testDispatchWithInvalidQuery()
0 ignored issues
show
Duplication introduced by
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...
81
    {
82
        $this
83
            ->given($queryBus = QueryBus::create())
84
            ->then()
85
                ->exception(function () use ($queryBus) {
86
                    $queryBus->dispatch(new FooMessage());
87
                })
88
                ->isInstanceOf(\InvalidArgumentException::class)
89
        ;
90
    }
91
}
92