Passed
Pull Request — master (#78)
by Damian
01:12
created

PostgreSQLDatabaseTest::testReadOnlyTransaction()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 4
nop 0
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\PostgreSQL\Tests;
4
5
use Exception;
6
use Page;
0 ignored issues
show
Bug introduced by
The type Page 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 SilverStripe\Dev\SapphireTest;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\ORM\DB;
10
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
11
12
/**
13
 * @package postgresql
14
 * @subpackage tests
15
 */
16
class PostgreSQLDatabaseTest extends SapphireTest
17
{
18
    protected $usesDatabase = true;
19
20
    public function testReadOnlyTransaction()
21
    {
22
        if (DB::get_conn()->supportsTransactions() == true
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
23
            && DB::get_conn() instanceof PostgreSQLDatabase
24
        ) {
25
            $page = new Page();
26
            $page->Title = 'Read only success';
27
            $page->write();
28
29
            DB::get_conn()->transactionStart('READ ONLY');
30
31
            try {
32
                $page = new Page();
33
                $page->Title = 'Read only page failed';
34
                $page->write();
35
            } catch (Exception $e) {
36
                //could not write this record
37
                //We need to do a rollback or a commit otherwise we'll get error messages
38
                DB::get_conn()->transactionRollback();
39
            }
40
41
            DB::get_conn()->transactionEnd();
42
43
            DataObject::flush_and_destroy_cache();
44
45
            $success = DataObject::get('Page', "\"Title\"='Read only success'");
46
            $fail = DataObject::get('Page', "\"Title\"='Read only page failed'");
47
48
            //This page should be in the system
49
            $this->assertTrue(is_object($success) && $success->exists());
50
51
            //This page should NOT exist, we had 'read only' permissions
52
            $this->assertFalse(is_object($fail) && $fail->exists());
53
        } else {
54
            $this->markTestSkipped('Current database is not PostgreSQL');
55
        }
56
    }
57
}
58