forked from lox/pheasant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncludesTest.php
83 lines (67 loc) · 2.5 KB
/
IncludesTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Pheasant\Tests\Relationships;
use \Pheasant\Tests\Examples\Hero;
use \Pheasant\Tests\Examples\Power;
use \Pheasant\Tests\Examples\SecretIdentity;
class IncludesTest extends \Pheasant\Tests\MysqlTestCase
{
public function setUp()
{
parent::setUp();
$migrator = new \Pheasant\Migrate\Migrator();
$migrator
->create('hero', Hero::schema())
->create('power', Power::schema())
->create('secretidentity', SecretIdentity::schema())
;
$this->pheasant
->connection()
->execute(
'INSERT INTO sequences (name, id) VALUES (?, ?)',
array('SECRETIDENTITY_ID_SEQ', 100)
);
$spiderman = Hero::createHelper('Spider Man', 'Peter Parker', array(
'Super-human Strength', 'Spider Senses'
));
$superman = Hero::createHelper('Super Man', 'Clark Kent', array(
'Super-human Strength', 'Invulnerability'
));
$batman = Hero::createHelper('Batman', 'Bruce Wayne', array(
'Richness', 'Super-human Intellect'
));
}
public function testIncludesHitsCache()
{
$queries = 0;
$this->connection()->filterChain()->onQuery(function ($sql) use (&$queries) {
++$queries;
return $sql;
});
// the first lookup of SecretIdentity should cache all the rest
$heros = Hero::all()->includes(array('SecretIdentity'))->toArray();
$this->assertNotNull($heros[0]->SecretIdentity);
// these should be from cache
$queries = 0;
$this->assertNotNull($heros[1]->SecretIdentity);
$this->assertNotNull($heros[2]->SecretIdentity);
$this->assertEquals(0, $queries, "this should have hit the cache");
}
public function testNestedIncludesHitsCache()
{
$queries = 0;
$this->connection()->filterChain()->onQuery(function ($sql) use (&$queries) {
++$queries;
return $sql;
});
// the first lookup of SecretIdentity should cache all the rest
$powers = Power::all()->includes(
array('Hero' => array('SecretIdentity')))->toArray();
$this->assertNotNull($powers[0]->Hero->SecretIdentity);
// these should be from cache
$queries = 0;
foreach ($powers as $power) {
$this->assertNotNull($power->Hero->SecretIdentity);
}
$this->assertEquals(0, $queries, "this should have hit the cache");
}
}