Completed
Push — master ( ef5eaa...6d70d2 )
by Lars
03:21 queued 30s
created

CacheTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 27.5 %

Coupling/Cohesion

Components 0
Dependencies 4
Metric Value
wmc 4
lcom 0
cbo 4
dl 11
loc 40
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use idiorm\orm\ORM;
4
5
class CacheTest extends PHPUnit_Framework_TestCase {
6
7
    const ALTERNATE = 'alternate'; // Used as name of alternate connection
8
9
    public function setUp() {
10
        // Set up the dummy database connections
11
        ORM::set_db(new \MockPDO('sqlite::memory:'));
12
        ORM::set_db(new \MockDifferentPDO('sqlite::memory:'), self::ALTERNATE);
13
14
        // Enable logging
15
        ORM::configure('logging', true);
16
        ORM::configure('logging', true, self::ALTERNATE);
17
        ORM::configure('caching', true);
18
        ORM::configure('caching', true, self::ALTERNATE);
19
    }
20
21
    public function tearDown() {
22
        ORM::reset_config();
23
        ORM::reset_db();
24
    }
25
26
    // Test caching. This is a bit of a hack.
27
    public function testQueryGenerationOnlyOccursOnce() {
28
        ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one();
29
        ORM::for_table('widget')->where('name', 'Bob')->where('age', 42)->find_one();
30
        $expected = ORM::get_last_query();
31
        ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one(); // this shouldn't run a query!
32
        $this->assertEquals($expected, ORM::get_last_query());
33
    }
34
35
    public function testQueryGenerationOnlyOccursOnceWithMultipleConnections() {
36
        // Test caching with multiple connections (also a bit of a hack)
37
        ORM::for_table('widget', self::ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one();
38
        ORM::for_table('widget', self::ALTERNATE)->where('name', 'Tom')->where('age', 120)->find_one();
39
        $expected = ORM::get_last_query();
40
        ORM::for_table('widget', self::ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one(); // this shouldn't run a query!
41
        $this->assertEquals($expected, ORM::get_last_query(self::ALTERNATE));
42
    }
43
44
}