|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pagerfanta\Tests\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DriverManager; |
|
6
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
abstract class DoctrineDbalTestCase extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var QueryBuilder |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $qb; |
|
16
|
|
|
|
|
17
|
|
|
protected function setUp() |
|
18
|
|
|
{ |
|
19
|
|
|
if ($this->isDoctrineDbalNotAvailable()) { |
|
20
|
|
|
$this->markTestSkipped('Doctrine DBAL is not available'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$conn = $this->getConnection(); |
|
24
|
|
|
|
|
25
|
|
|
$this->createSchema($conn); |
|
26
|
|
|
$this->insertData($conn); |
|
27
|
|
|
|
|
28
|
|
|
$this->qb = new QueryBuilder($conn); |
|
29
|
|
|
$this->qb->select('p.*')->from('posts', 'p'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function isDoctrineDbalNotAvailable() |
|
33
|
|
|
{ |
|
34
|
|
|
return !class_exists('Doctrine\DBAL\DriverManager'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function getConnection() |
|
38
|
|
|
{ |
|
39
|
|
|
$params = $conn = array( |
|
|
|
|
|
|
40
|
|
|
'driver' => 'pdo_sqlite', |
|
41
|
|
|
'memory' => true, |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
return DriverManager::getConnection($params); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function createSchema($conn) |
|
48
|
|
|
{ |
|
49
|
|
|
$schema = new Schema(); |
|
50
|
|
|
$posts = $schema->createTable('posts'); |
|
51
|
|
|
$posts->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true)); |
|
52
|
|
|
$posts->addColumn('username', 'string', array('length' => 32)); |
|
53
|
|
|
$posts->addColumn('post_content', 'text'); |
|
54
|
|
|
$posts->setPrimaryKey(array('id')); |
|
55
|
|
|
|
|
56
|
|
|
$comments = $schema->createTable('comments'); |
|
57
|
|
|
$comments->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true)); |
|
58
|
|
|
$comments->addColumn('post_id', 'integer', array('unsigned' => true)); |
|
59
|
|
|
$comments->addColumn('username', 'string', array('length' => 32)); |
|
60
|
|
|
$comments->addColumn('content', 'text'); |
|
61
|
|
|
$comments->setPrimaryKey(array('id')); |
|
62
|
|
|
|
|
63
|
|
|
$queries = $schema->toSql($conn->getDatabasePlatform()); // get queries to create this schema. |
|
64
|
|
|
|
|
65
|
|
|
foreach ($queries as $sql) { |
|
66
|
|
|
$conn->executeQuery($sql); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function insertData($conn) |
|
71
|
|
|
{ |
|
72
|
|
|
for ($i = 1; $i <= 50; $i++) { |
|
73
|
|
|
$conn->insert('posts', array('username' => 'Jon Doe', 'post_content' => 'Post #'.$i)); |
|
74
|
|
|
for ($j = 1; $j <= 5; $j++) { |
|
75
|
|
|
$conn->insert('comments', array('post_id' => $i, 'username' => 'Jon Doe', 'content' => 'Comment #'.$j)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.