Passed
Branch master (6a8d47)
by Brice
03:48
created

JsonRepositoryCacheTest::testAddPackage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Tests\Infrastructure;
4
5
use CompoLab\Domain\Package;
6
use CompoLab\Domain\Repository;
7
use CompoLab\Domain\ValueObject\Dir;
8
use CompoLab\Domain\ValueObject\Url;
9
use CompoLab\Infrastructure\JsonRepositoryCache;
10
use PHPUnit\Framework\TestCase;
11
12
final class JsonRepositoryCacheTest extends TestCase
13
{
14
    /** @var JsonRepositoryCache */
15
    private static $cache;
16
17
    public static function setUpBeforeClass()
18
    {
19
        self::$cache = new JsonRepositoryCache(new Repository(
20
            new Url('https://composer.my-website.com'),
21
            new Dir(__DIR__ . '/../../cache')
22
        ));
23
    }
24
25
    public function testGetRepository()
26
    {
27
        self::assertInstanceOf(Repository::class, self::$cache->getRepository());
28
    }
29
30
    public function testAddPackage()
31
    {
32
        $this->assertEquals(0, count(self::$cache));
33
34
        $json = json_decode(file_get_contents(__DIR__ . '/../../data/composer.json'), true);
35
36
        $package = Package::buildFromArray(__DIR__ . '/../../cache', array_merge($json, [
37
            'version'       => 'dev-master',
38
            'source'        => [
39
                'type'      => 'git',
40
                'url'       => '[email protected]:vendor/project.git',
41
                'reference' => '6a6e0ea9479c821d4b5728c0d3c9840e71085e82',
42
            ],
43
        ]));
44
45
        self::$cache->addPackage($package);
46
47
        $this->assertEquals(1, count(self::$cache));
48
    }
49
50
    /**
51
     *
52
     * @depends testAddPackage
53
     */
54
    public function testRefresh()
55
    {
56
        self::$cache->refresh();
57
58
        $json = json_decode(file_get_contents(__DIR__ . '/../../cache/packages.json'), true);
59
60
        $this->assertEquals(
61
            '6a6e0ea9479c821d4b5728c0d3c9840e71085e82',
62
            $json['packages']['vendor/project']['dev-master']['source']['reference']
63
        );
64
    }
65
66
    public static function tearDownAfterClass()
67
    {
68
        // Clean packages for future tests
69
        file_put_contents(__DIR__ . '/../../cache/packages.json', '{}');
70
    }
71
}
72