1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhpTest\Exercise; |
4
|
|
|
|
5
|
|
|
use Faker\Factory; |
6
|
|
|
use Faker\Generator; |
7
|
|
|
use PDO; |
8
|
|
|
use PhpSchool\LearnYouPhp\Exercise\DatabaseRead; |
9
|
|
|
use PhpSchool\PhpWorkshop\Check\DatabaseCheck; |
10
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
11
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
12
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
13
|
|
|
use PHPUnit_Framework_TestCase; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class DatabaseReadTest |
17
|
|
|
* @package PhpSchool\LearnYouPhpTest\Exercise |
18
|
|
|
* @author Aydin Hassan <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class DatabaseReadTest extends PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Generator |
24
|
|
|
*/ |
25
|
|
|
private $faker; |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->faker = Factory::create(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testDatabaseExercise() |
33
|
|
|
{ |
34
|
|
|
$e = new DatabaseRead($this->faker); |
35
|
|
|
$this->assertEquals('Database Read', $e->getName()); |
36
|
|
|
$this->assertEquals('Read an SQL databases contents', $e->getDescription()); |
37
|
|
|
$this->assertEquals(ExerciseType::CLI, $e->getType()); |
38
|
|
|
|
39
|
|
|
$this->assertInstanceOf(SolutionInterface::class, $e->getSolution()); |
40
|
|
|
$this->assertFileExists(realpath($e->getProblem())); |
41
|
|
|
$this->assertNull($e->tearDown()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testSeedAddsRandomUsersToDatabaseAndStoresRandomIdAndName() |
45
|
|
|
{ |
46
|
|
|
$db = new PDO('sqlite::memory:'); |
47
|
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
48
|
|
|
$e = new DatabaseRead($this->faker); |
49
|
|
|
|
50
|
|
|
$e->seed($db); |
51
|
|
|
|
52
|
|
|
$args = $e->getArgs(); |
53
|
|
|
$stmt = $db->query('SELECT * FROM users;'); |
54
|
|
|
|
55
|
|
|
$users = $stmt->fetchAll(PDO::FETCH_ASSOC); |
56
|
|
|
$this->assertTrue(count($users) >= 5); |
57
|
|
|
$this->assertInternalType('array', $users); |
58
|
|
|
$this->assertTrue(in_array($args[0], array_column($users, 'name'))); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testVerifyReturnsTrueIfRecordExistsWithNameUsingStoredId() |
62
|
|
|
{ |
63
|
|
|
$db = new PDO('sqlite::memory:'); |
64
|
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
65
|
|
|
$e = new DatabaseRead($this->faker); |
66
|
|
|
|
67
|
|
|
$rp = new \ReflectionProperty(DatabaseRead::class, 'randomRecord'); |
68
|
|
|
$rp->setAccessible(true); |
69
|
|
|
$rp->setValue($e, ['id' => 5]); |
70
|
|
|
|
71
|
|
|
$db |
72
|
|
|
->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT)'); |
73
|
|
|
$stmt = $db->prepare('INSERT INTO users (id, name, age, gender) VALUES (:id, :name, :age, :gender)'); |
74
|
|
|
$stmt->execute([':id' => 5, ':name' => 'David Attenborough', ':age' => 50, ':gender' => 'Male']); |
75
|
|
|
|
76
|
|
|
$this->assertTrue($e->verify($db)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testConfigure() |
80
|
|
|
{ |
81
|
|
|
$dispatcher = $this->getMockBuilder(ExerciseDispatcher::class) |
82
|
|
|
->disableOriginalConstructor() |
83
|
|
|
->getMock(); |
84
|
|
|
|
85
|
|
|
$dispatcher |
86
|
|
|
->expects($this->once()) |
87
|
|
|
->method('requireListenableCheck') |
88
|
|
|
->with(DatabaseCheck::class); |
89
|
|
|
|
90
|
|
|
$e = new DatabaseRead($this->faker); |
91
|
|
|
$e->configure($dispatcher); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|