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

PNDataProviderSQLiteTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 28
dl 0
loc 59
rs 10
c 1
b 0
f 1
wmc 6
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Test\PNServer;
5
6
use SKien\PNServer\PNDataProviderSQLite;
7
8
/**
9
 * Fixture for the whole test.
10
 * - create temp directory for datafile if not exist 
11
 * - and ensure it is writeable
12
 * both will be done in self::getTempDataDir
13
 * - start without existing DB (delete DB file if exists so far)
14
 * 
15
 * Clean-Up 
16
 * - delete created DB-file after last test 
17
 * 
18
 * @author Stefanius <[email protected]>
19
 * @copyright MIT License - see the LICENSE file for details
20
 */
21
class PNDataProviderSQLiteTest extends PNDataProviderTest
22
{
23
    use TestHelperTrait;
24
    
25
    protected $strTmpDir;
26
    protected $strFullPath;
27
28
    public static function setUpBeforeClass() : void
29
    {
30
        // fwrite(STDOUT, __METHOD__ . "\n");
31
        self::getTempDataDir();
32
        self::deleteTempDataFile();
33
    }
34
    
35
    public static function tearDownAfterClass() : void
36
    {
37
        self::deleteTempDataFile();
38
    }
39
40
    public function setUp() : void
41
    {
42
        // each test needs dir/path and a conected DP
43
        $this->strTmpDir = self::getTempDataDir();
44
        $this->strFullPath = $this->strTmpDir . DIRECTORY_SEPARATOR . self::$strSQLiteDBFilename;
45
        $this->dp = new PNDataProviderSQLite($this->strTmpDir, self::$strSQLiteDBFilename);
46
    }
47
    
48
    public function test_constructError1() : void
49
    {
50
        // test for invalid directory
51
        $strInvalidDir = 'invaliddir';
52
        $dp = new PNDataProviderSQLite($strInvalidDir, self::$strSQLiteDBFilename);
53
        $this->assertIsObject($dp);
54
        $this->assertFalse($dp->isConnected());
55
        $this->assertNotEmpty($dp->getError());
56
    }
57
    
58
    public function test_constructError2() : void
59
    {
60
        // test for readonly directory
61
        chmod($this->strTmpDir, 0444);
62
        $dp = new PNDataProviderSQLite($this->strTmpDir, self::$strSQLiteDBFilename);
63
        $this->assertIsObject($dp);
64
        $this->assertFalse($dp->isConnected());
65
        $this->assertNotEmpty($dp->getError());
66
        chmod($this->strTmpDir, 0777);
67
    }
68
    
69
    public function test_constructError3() : void
70
    {
71
        // test for readonly db-file
72
        chmod($this->strFullPath, 0444);
73
        // for this test we need to create local instance AFTER set the DB-file to readonly ...
74
        $dp = new PNDataProviderSQLite($this->strTmpDir, self::$strSQLiteDBFilename);
75
        $this->assertIsObject($dp);
76
        $this->assertFalse($dp->isConnected());
77
        $strExpected = 'readonly database file ' . $this->strFullPath . '!';
78
        $this->assertEquals($strExpected, $dp->getError());
79
        chmod($this->strFullPath, 0777);
80
    }
81
}
82
83