1<?php declare(strict_types=1);
2
3namespace App\Tests\Unit\Utils\Versions;
4
5use PHPUnit\Framework\TestCase;
6use App\Utils\Versions\Generator;
7use App\Utils\Versions\Client;
8use App\Utils\Cache;
9
10class GeneratorTest extends TestCase
11{
12    /** @var string */
13    private $cacheDir = TEST_VAR_DIRECTORY . '/cache/test';
14
15    /** @var Cache */
16    private $cache;
17
18    /** @var Client */
19    private $client;
20
21    /** @var Generator */
22    private $generator;
23
24    public function setUp(): void
25    {
26        $this->cache = new Cache($this->cacheDir);
27        $this->cache->clear();
28
29        // The results returned by the client depend on the remote URLs so we
30        // mock the returned results.
31        $this->client = $this->getMockBuilder(Client::class)
32            ->setMethods(['fetchDevVersions', 'fetchStableVersions'])
33            ->getMock();
34
35        $this->client->expects($this->once())
36            ->method('fetchDevVersions')
37            ->will($this->returnValue(json_decode(file_get_contents(TEST_MOCKS_DIRECTORY . '/responses/dev-body.txt', true))));
38
39        $this->client->expects($this->once())
40            ->method('fetchStableVersions')
41            ->will($this->returnValue(json_decode(file_get_contents(TEST_MOCKS_DIRECTORY . '/responses/stable-body.txt'), true)));
42
43        $this->generator = $this->getMockBuilder(Generator::class)
44            ->setConstructorArgs([$this->client, $this->cache])
45            ->setMethods(['getAffixes'])
46            ->getMock();
47
48        // The extra versions are always date dependant so we mock it to include
49        // static date done on the tests day.
50        $date = '2018-12-26';
51        $this->generator->expects($this->any())
52            ->method('getAffixes')
53            ->will($this->returnValue(['Git-' . $date . ' (snap)', 'Git-' . $date . ' (Git)',]));
54    }
55
56    public function tearDown(): void
57    {
58        $this->cache->clear();
59        rmdir($this->cacheDir);
60    }
61
62    public function testVersions(): void
63    {
64        $versions = $this->generator->getVersions();
65
66        $this->assertIsArray($versions);
67        $this->assertGreaterThan(5, count($versions));
68
69        $fixture = require TEST_FIXTURES_DIRECTORY . '/versions/versions.php';
70        $cached = require $this->cacheDir . '/versions.php';
71
72        $this->assertEquals($fixture[1], $cached[1]);
73        $this->assertContains('Next Major Version', $versions);
74        $this->assertContains('Irrelevant', $versions);
75        $this->assertContains('7.2.14RC1', $versions);
76    }
77}
78