-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStoredProcedureTest.php
73 lines (64 loc) · 2.32 KB
/
StoredProcedureTest.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
<?php
/**
* This is a kind of integration test: we mock out the database from every
* other test, but still we have to hit it in one place to ensure our Gateway
* (in this case a class containing queries) work.
*/
class StoredProcedureTest extends PHPUnit_Framework_TestCase
{
private $connection;
private $repository;
/**
* I'm using my local instance of MySQL: the only requirement is the
* presence of a database named 'sandbox'. You may want to parametrize
* everything here from database name to user and password.
*/
public function setUp()
{
$this->connection = new PDO("mysql:host=localhost;dbname=sandbox", 'root', '');
$this->connection->exec("CREATE TABLE users (name VARCHAR(255) NOT NULL PRIMARY KEY, year YEAR)");
$this->repository = new UserRepository($this->connection, 2011);
}
public function testAverageAgeIsCalculated()
{
$this->insertUser('Giorgio', 1942);
$this->insertUser('Isaac', 1920);
$this->assertEquals(80, $this->repository->getAverageAge());
}
private function insertUser($name, $year)
{
$stmt = $this->connection->prepare("INSERT INTO users (name, year) VALUES (:name, :year)");
$stmt->bindValue('name', $name, PDO::PARAM_STR);
$stmt->bindValue('year', $year, PDO::PARAM_INT);
return $stmt->execute();
}
public function tearDown()
{
$this->connection->exec('DROP TABLE users');
}
}
class UserRepository
{
private $connection;
private $currentYear;
public function __construct(PDO $connection, $currentYear)
{
$this->connection = $connection;
$this->currentYear = $currentYear;
}
/**
* We suppose AVG() cannot be correctly implemented by Sqlite or
* another surrogate database (substitute another vendor feature
* for the same effect).
* We also suppose reconstituting millions of User objects to calculate
* their average age isn't feasible: that's why we used SQL directly.
*/
public function getAverageAge()
{
$stmt = $this->connection->prepare('SELECT AVG(:year - year) AS average_age FROM users');
$stmt->bindValue('year', $this->currentYear, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch();
return $row['average_age'];
}
}