Passed
Push — master ( c73d10...c1344b )
by Stefan
06:00
created

PNDataProviderTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 43
dl 0
loc 92
rs 10
c 1
b 0
f 1
wmc 7
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Test\PNServer;
5
6
use PHPUnit\Framework\TestCase;
7
use SKien\PNServer\PNDataProvider;
8
use SKien\PNServer\PNSubscription;
9
10
/**
11
 * Abstract class providing comprehensive tests for all classes that implement the
12
 * PNDataProvider Interface.
13
 * Extending classes have to 
14
 * - create database fixture for whole testclass in the static setUpBeforeClass() method
15
 * - create an instance of the class in the setUp() method and assign it to the $dp property
16
 * - implement tests to verify the creation
17
 * - implement tests for extended functions not supported by PNDataProvider 
18
 * - Clean-Up Database after last test in the static tearDownAfterClass() method
19
 * 
20
 * @author Stefanius <[email protected]>
21
 * @copyright MIT License - see the LICENSE file for details
22
 */
23
abstract class PNDataProviderTest extends TestCase
24
{
25
    use TestHelperTrait;
26
    
27
    protected ?PNDataProvider $dp = null;
28
    
29
    public function test_construct() : void
30
    {
31
        $this->assertIsObject($this->dp);
32
        $this->assertEmpty($this->dp->getError());
33
        $this->assertTrue($this->dp->isConnected());
34
    }
35
    
36
    public function test_saveSubscription() : string
37
    {
38
        $this->assertIsObject($this->dp);
39
        
40
        $this->assertEquals(0, $this->dp->count());
41
        $strSub = $this->loadSubscription('valid_subscription.json');
42
        $this->assertTrue($this->dp->saveSubscription($strSub));
43
        $this->assertEmpty($this->dp->getError());
44
        $this->assertEquals(1, $this->dp->count());
45
        $sub = PNSubscription::fromJSON($strSub);
46
        
47
        // pass to test_remove...
48
        return $sub->getEndpoint();
49
    }
50
    
51
    /**
52
     * @depends test_saveSubscription
53
     */
54
    public function test_removeSubscription(string $strEndpoint) : void
55
    {
56
        $this->assertIsObject($this->dp);
57
        
58
        $this->assertEquals(1, $this->dp->count());
59
        // remove previous inserted subscription by endpoint
60
        $this->assertTrue($this->dp->removeSubscription($strEndpoint));
61
        $this->assertEmpty($this->dp->getError());
62
        $this->assertEquals(0, $this->dp->count());
63
    }
64
    
65
    public function test_fetch() : void
66
    {
67
        $this->assertIsObject($this->dp);
68
        
69
        // create with valid and expired subscription
70
        $this->assertTrue($this->dp->isConnected());
71
        $this->dp->saveSubscription($this->loadSubscription('valid_subscription.json'));
72
        $this->dp->saveSubscription($this->loadSubscription('expired_subscription.json'));
73
        $this->assertEquals(2, $this->dp->count());
74
        
75
        // init list with autoremove=false - expired record have to be ignored but not deleted
76
        $this->assertTrue($this->dp->init(false));
77
        
78
        $this->assertNotEmpty($this->dp->fetch());
79
        $this->assertGreaterThan(0, intval($this->dp->getColumn(PNDataProvider::COL_ID)));
80
        // no more record to fetch
81
        $this->assertFalse($this->dp->fetch());
82
    }
83
    
84
    public function test_autoremove() : void
85
    {
86
        $this->assertIsObject($this->dp);
87
        
88
        // create with valid and expired subscription
89
        $this->assertTrue($this->dp->isConnected());
90
        $this->dp->saveSubscription($this->loadSubscription('valid_subscription.json'));
91
        $this->dp->saveSubscription($this->loadSubscription('expired_subscription.json'));
92
        $this->assertTrue($this->dp->init(true));
93
        // ... 1 record left
94
        $this->assertEquals(1, $this->dp->count());
95
    }
96
    
97
    public function test_truncate() : void
98
    {
99
        $this->assertIsObject($this->dp);
100
        
101
        $this->dp->saveSubscription($this->loadSubscription('valid_subscription.json'));
102
        $this->assertGreaterThan(0, $this->dp->count());
103
        $this->assertTrue($this->dp->truncate());
104
        $this->assertEquals(0, $this->dp->count());
105
    }
106
    
107
    public function test_saveSubscriptionError() : void
108
    {
109
        $this->assertIsObject($this->dp);
110
        
111
        // causing error by passing invalid JSON string
112
        $result = $this->dp->saveSubscription('{,');
113
        $this->assertFalse($result);
114
        $this->assertNotEmpty($this->dp->getError());
115
    }
116
}
117
118