Passed
Push — master ( 800a4a...a7adec )
by WEBEWEB
03:19
created

DataTablesManagerTest::testContainsProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\JQuery\DataTablesBundle\Tests\Manager;
13
14
use InvalidArgumentException;
15
use Throwable;
16
use WBW\Bundle\JQuery\DataTablesBundle\Exception\AlreadyRegisteredDataTablesProviderException;
17
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException;
18
use WBW\Bundle\JQuery\DataTablesBundle\Manager\DataTablesManager;
19
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
20
use WBW\Bundle\JQuery\DataTablesBundle\Tests\AbstractTestCase;
21
use WBW\Library\Symfony\Color\MaterialDesignColorPalette\RedColorProvider;
22
23
/**
24
 * DataTables manager test.
25
 *
26
 * @author webeweb <https://github.com/webeweb>
27
 * @package WBW\Bundle\JQuery\DataTablesBundle\Tests\Manager
28
 */
29
class DataTablesManagerTest extends AbstractTestCase {
30
31
    /**
32
     * DataTables provider.
33
     *
34
     * @var DataTablesProviderInterface
35
     */
36
    private $dataTablesProvider;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function setUp(): void {
42
        parent::setUp();
43
44
        // Set a DataTables provider mock.
45
        $this->dataTablesProvider = $this->getMockBuilder(DataTablesProviderInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(WB...face::class)->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type WBW\Bundle\JQuery\DataTa...TablesProviderInterface of property $dataTablesProvider.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        $this->dataTablesProvider->expects($this->any())->method("getName")->willReturn("name");
47
    }
48
49
    /**
50
     * Tests addProvider()
51
     *
52
     * @return void
53
     * @throws Throwable Throws an exception if an error occurs.
54
     */
55
    public function testAddProvider(): void {
56
57
        $obj = new DataTablesManager();
58
59
        $obj->addProvider($this->dataTablesProvider);
60
        $this->assertCount(1, $obj->getProviders());
61
        $this->assertSame($this->dataTablesProvider, $obj->getProviders()[0]);
62
    }
63
64
    /**
65
     * Tests addProvider()
66
     *
67
     * @return void
68
     * @throws Throwable Throws an exception if an error occurs.
69
     */
70
    public function testAddProviderWithAlreadyRegisteredDataTablesProviderException(): void {
71
72
        $obj = new DataTablesManager();
73
        $obj->addProvider($this->dataTablesProvider);
74
75
        try {
76
77
            $obj->addProvider($this->dataTablesProvider);
78
        } catch (Throwable $ex) {
79
80
            $this->assertInstanceOf(AlreadyRegisteredDataTablesProviderException::class, $ex);
81
            $this->assertEquals('A DataTables provider with name "name" is already registered', $ex->getMessage());
82
        }
83
    }
84
85
    /**
86
     * Tests addProvider()
87
     *
88
     * @return void
89
     */
90
    public function testAddProviderWithInvalidArgumentException(): void {
91
92
        $obj = new DataTablesManager();
93
94
        try {
95
96
            $obj->addProvider(new RedColorProvider());
97
        } catch (Throwable $ex) {
98
99
            $this->assertInstanceOf(InvalidArgumentException::class, $ex);
100
            $this->assertEquals("The provider must implements " . DataTablesProviderInterface::class, $ex->getMessage());
101
        }
102
    }
103
104
    /**
105
     * Tests getProvider()
106
     *
107
     * @return void
108
     * @throws Throwable Throws an exception if an error occurs.
109
     */
110
    public function testGetProvider(): void {
111
112
        $obj = new DataTablesManager();
113
114
        $obj->addProvider($this->dataTablesProvider);
115
        $this->assertSame($this->dataTablesProvider, $obj->getProvider($this->dataTablesProvider->getName()));
116
    }
117
118
    /**
119
     * Tests getProvider()
120
     *
121
     * @return void
122
     */
123
    public function testGetProviderWithUnregisteredDataTablesProviderException(): void {
124
125
        $obj = new DataTablesManager();
126
127
        try {
128
129
            $obj->getProvider("exception");
130
        } catch (Throwable $ex) {
131
132
            $this->assertInstanceOf(UnregisteredDataTablesProviderException::class, $ex);
133
            $this->assertEquals('None DataTables provider registered with name "exception"', $ex->getMessage());
134
        }
135
    }
136
137
    /**
138
     * Tests __construct()
139
     *
140
     * @return void
141
     */
142
    public function test__construct(): void {
143
144
        $this->assertEquals("wbw.jquery.datatables.manager", DataTablesManager::SERVICE_NAME);
145
146
        $obj = new DataTablesManager();
147
148
        $this->assertEquals([], $obj->getProviders());
149
    }
150
}
151