Passed
Pull Request — master (#78)
by Damian
02:06 queued 52s
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 SilverStripe\Dev\SapphireTest;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
9
10
/**
11
 * @package postgresql
12
 * @subpackage tests
13
 */
14
class PostgreSQLDatabaseTest extends SapphireTest
15
{
16
    protected $usesDatabase = true;
17
18
    public function testReadOnlyTransaction()
19
    {
20
        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...
21
            && DB::get_conn() instanceof PostgreSQLDatabase
22
        ) {
23
            $page=new Page();
0 ignored issues
show
Bug introduced by
The type SilverStripe\PostgreSQL\Tests\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...
24
            $page->Title='Read only success';
25
            $page->write();
26
27
            DB::get_conn()->transactionStart('READ ONLY');
28
29
            try {
30
                $page=new Page();
31
                $page->Title='Read only page failed';
32
                $page->write();
33
            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type SilverStripe\PostgreSQL\Tests\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
34
                //could not write this record
35
                //We need to do a rollback or a commit otherwise we'll get error messages
36
                DB::get_conn()->transactionRollback();
37
            }
38
39
            DB::get_conn()->transactionEnd();
40
41
            DataObject::flush_and_destroy_cache();
42
43
            $success=DataObject::get('Page', "\"Title\"='Read only success'");
44
            $fail=DataObject::get('Page', "\"Title\"='Read only page failed'");
45
46
            //This page should be in the system
47
            $this->assertTrue(is_object($success) && $success->exists());
48
49
            //This page should NOT exist, we had 'read only' permissions
50
            $this->assertFalse(is_object($fail) && $fail->exists());
51
        } else {
52
            $this->markTestSkipped('Current database is not PostgreSQL');
53
        }
54
    }
55
}
56