Passed
Push — master ( 8a0955...800a4a )
by WEBEWEB
03:03
created

testContainsWithInvalidArgumentException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 11
c 1
b 0
f 0
rs 10
cc 2
nc 2
nop 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 containsProvider()
87
     *
88
     * @return void
89
     * @throws Throwable Throws an exception if an error occurs.
90
     */
91
    public function testContainsProvider(): void {
92
93
        $obj = new DataTablesManager();
94
95
        $this->assertFalse($obj->containsProvider($this->dataTablesProvider));
96
97
        $obj->addProvider($this->dataTablesProvider);
98
        $this->assertTrue($obj->containsProvider($this->dataTablesProvider));
99
    }
100
101
    /**
102
     * Tests containsProvider()
103
     *
104
     * @return void
105
     */
106
    public function testContainsProviderWithInvalidArgumentException(): void {
107
108
        $obj = new DataTablesManager();
109
110
        try {
111
112
            $obj->containsProvider(new RedColorProvider());
113
        } catch (Throwable $ex) {
114
115
            $this->assertInstanceOf(InvalidArgumentException::class, $ex);
116
            $this->assertEquals("The provider must implements " . DataTablesProviderInterface::class, $ex->getMessage());
117
        }
118
    }
119
120
    /**
121
     * Tests getProvider()
122
     *
123
     * @return void
124
     * @throws Throwable Throws an exception if an error occurs.
125
     */
126
    public function testGetProvider(): void {
127
128
        $obj = new DataTablesManager();
129
130
        $obj->addProvider($this->dataTablesProvider);
131
        $this->assertSame($this->dataTablesProvider, $obj->getProvider($this->dataTablesProvider->getName()));
132
    }
133
134
    /**
135
     * Tests getProvider()
136
     *
137
     * @return void
138
     */
139
    public function testGetProviderWithUnregisteredDataTablesProviderException(): void {
140
141
        $obj = new DataTablesManager();
142
143
        try {
144
145
            $obj->getProvider("exception");
146
        } catch (Throwable $ex) {
147
148
            $this->assertInstanceOf(UnregisteredDataTablesProviderException::class, $ex);
149
            $this->assertEquals('None DataTables provider registered with name "exception"', $ex->getMessage());
150
        }
151
    }
152
153
    /**
154
     * Tests __construct()
155
     *
156
     * @return void
157
     */
158
    public function test__construct(): void {
159
160
        $this->assertEquals("wbw.jquery.datatables.manager", DataTablesManager::SERVICE_NAME);
161
162
        $obj = new DataTablesManager();
163
164
        $this->assertEquals([], $obj->getProviders());
165
    }
166
}
167