Passed
Branch master (c73d10)
by Stefan
02:51 queued 55s
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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A test_autoremove() 0 11 1
A test_removeSubscription() 0 9 1
A test_saveSubscriptionError() 0 8 1
A test_construct() 0 5 1
A test_fetch() 0 17 1
A test_saveSubscription() 0 13 1
A test_truncate() 0 8 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SKien\Test\PNServer;
5
6
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getError() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $this->assertEmpty($this->dp->/** @scrutinizer ignore-call */ getError());

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...
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