DatabaseReadTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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