ConnectionFactoryTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 1
cbo 4
dl 8
loc 76
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createDataProvider() 0 8 1
A testCreateWorkingDriver() 0 8 1
A testCreateEmptyDriverConnector() 0 6 1
A testCreateUnsupportedDriverConnector() 0 6 1
A getConnectionFactory() 0 4 1
A testMake() 8 8 1
A getMockedFactory() 0 16 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 Laravel SMSFactor.
5
 *
6
 * (c) Filippo Galante <[email protected]>
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 IlGala\Tests\SMSFactor\Adapters;
13
14
use IlGala\SMSFactor\Adapters\AdapterInterface;
15
use IlGala\SMSFactor\Adapters\BuzzAdapter;
16
use IlGala\SMSFactor\Connectors\BuzzConnector;
17
use IlGala\SMSFactor\Connectors\ConnectionFactory;
18
use IlGala\SMSFactor\Connectors\GuzzleConnector;
19
use IlGala\SMSFactor\Connectors\GuzzleHttpConnector;
20
use IlGala\SMSFactor\Connectors\LocalConnector;
21
use GrahamCampbell\TestBench\AbstractTestCase;
22
use Mockery;
23
24
/**
25
 * This is the adapter connection factory test class.
26
 *
27
 * @author Filippo Galante <[email protected]>
28
 */
29
class ConnectionFactoryTest extends AbstractTestCase
30
{
31 View Code Duplication
    public function testMake()
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...
32
    {
33
        $factory = $this->getMockedFactory();
34
35
        $return = $factory->make(['driver' => 'guzzlehttp', 'username' => 'your-username', 'password' => 'your-password', 'accept' => 'application/json']);
0 ignored issues
show
Bug introduced by
The method make() does not seem to exist on object<Mockery\MockInterface>.

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...
36
37
        $this->assertInstanceOf(AdapterInterface::class, $return);
38
    }
39
40
    public function createDataProvider()
41
    {
42
        return [
43
            ['buzz', BuzzConnector::class],
44
            ['guzzle', GuzzleConnector::class],
45
            ['guzzlehttp', GuzzleHttpConnector::class],
46
        ];
47
    }
48
49
    /**
50
     * @dataProvider createDataProvider
51
     */
52
    public function testCreateWorkingDriver($driver, $class)
53
    {
54
        $factory = $this->getConnectionFactory();
55
56
        $return = $factory->createConnector(['driver' => $driver]);
57
58
        $this->assertInstanceOf($class, $return);
59
    }
60
61
    /**
62
     * @expectedException \InvalidArgumentException
63
     * @expectedExceptionMessage A driver must be specified.
64
     */
65
    public function testCreateEmptyDriverConnector()
66
    {
67
        $factory = $this->getConnectionFactory();
68
69
        $factory->createConnector([]);
70
    }
71
72
    /**
73
     * @expectedException \InvalidArgumentException
74
     * @expectedExceptionMessage Unsupported driver [unsupported].
75
     */
76
    public function testCreateUnsupportedDriverConnector()
77
    {
78
        $factory = $this->getConnectionFactory();
79
80
        $factory->createConnector(['driver' => 'unsupported']);
81
    }
82
83
    protected function getConnectionFactory()
84
    {
85
        return new ConnectionFactory();
86
    }
87
88
    protected function getMockedFactory()
89
    {
90
        $mock = Mockery::mock(ConnectionFactory::class . '[createConnector]');
91
92
        $connector = Mockery::mock(LocalConnector::class);
93
94
        $connector->shouldReceive('connect')->once()
95
                ->with(['driver' => 'guzzlehttp', 'username' => 'your-username', 'password' => 'your-password', 'accept' => 'application/json'])
96
                ->andReturn(Mockery::mock(BuzzAdapter::class));
97
98
        $mock->shouldReceive('createConnector')->once()
99
                ->with(['driver' => 'guzzlehttp', 'username' => 'your-username', 'password' => 'your-password', 'accept' => 'application/json'])
100
                ->andReturn($connector);
101
102
        return $mock;
103
    }
104
}
105