Completed
Push — master ( d461d1...b123ad )
by Joachim
05:50
created

SiteSettingsProviderTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testOnlyDefaultSettings() 0 16 1
A testOneSetting() 0 20 1
A getContainer() 0 9 1
A getRepository() 0 7 1
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Provider;
4
5
use Loevgaard\DandomainAltapayBundle\Entity\SiteSetting;
6
use Loevgaard\DandomainAltapayBundle\Entity\SiteSettingRepository;
7
use Loevgaard\DandomainAltapayBundle\Provider\SiteSettingsProvider;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
final class SiteSettingsProviderTest extends TestCase
12
{
13
    public function testOnlyDefaultSettings()
14
    {
15
        $container = $this->getContainer();
16
        $repository = $this->getRepository();
17
18
        $repository->method('findBySiteIdIndexedBySetting')->willReturn(null);
19
20
        $expected = array_map(function () {
21
            return 'default_setting';
22
        }, SiteSetting::getSettings());
23
24
        $siteSettingsProvider = new SiteSettingsProvider($container, $repository);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->getContainer() on line 15 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...Provider::__construct() does only seem to accept object<Symfony\Component...ion\ContainerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $repository defined by $this->getRepository() on line 16 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...Provider::__construct() does only seem to accept object<Loevgaard\Dandoma...\SiteSettingRepository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
25
        $settings = $siteSettingsProvider->findBySiteIdIndexedBySetting(26);
26
27
        $this->assertSame($expected, $settings);
28
    }
29
30
    public function testOneSetting()
31
    {
32
        $container = $this->getContainer();
33
        $repository = $this->getRepository();
34
35
        $siteSetting = new SiteSetting();
36
        $siteSetting->setVal('11223344')->setSetting(SiteSetting::SETTING_PHONE)->setSiteId(26);
37
38
        $repository->method('findBySiteIdIndexedBySetting')->willReturn([SiteSetting::SETTING_PHONE => $siteSetting]);
39
40
        $expected = array_map(function ($elm) {
0 ignored issues
show
Unused Code introduced by
The parameter $elm is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
            return 'default_setting';
42
        }, SiteSetting::getSettings());
43
        $expected['phone'] = $siteSetting;
44
45
        $siteSettingsProvider = new SiteSettingsProvider($container, $repository);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->getContainer() on line 32 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...Provider::__construct() does only seem to accept object<Symfony\Component...ion\ContainerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $repository defined by $this->getRepository() on line 33 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...Provider::__construct() does only seem to accept object<Loevgaard\Dandoma...\SiteSettingRepository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
        $settings = $siteSettingsProvider->findBySiteIdIndexedBySetting(26);
47
48
        $this->assertEquals($expected, $settings);
49
    }
50
51
    /**
52
     * @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
53
     */
54
    private function getContainer()
55
    {
56
        /** @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject $container */
57
        $container = $this->getMockForAbstractClass(ContainerInterface::class);
58
59
        $container->method('getParameter')->willReturn('default_setting');
60
61
        return $container;
62
    }
63
64
    /**
65
     * @return SiteSettingRepository|\PHPUnit_Framework_MockObject_MockObject
66
     */
67
    private function getRepository()
68
    {
69
        /** @var SiteSettingRepository|\PHPUnit_Framework_MockObject_MockObject $repository */
70
        $repository = $this->getMockBuilder(SiteSettingRepository::class)->disableOriginalConstructor()->getMock();
71
72
        return $repository;
73
    }
74
}
75