JsonRepositoryCacheTest::testRemovePackage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 10
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
        self::$cache->addPackage(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::buildFromArray(__DIR__ . '/../../cache', array_merge($json, [
46
            'version'       => 'dev-feature',
47
            'source'        => [
48
                'type'      => 'git',
49
                'url'       => '[email protected]:vendor/project.git',
50
                'reference' => '8c7g1iu6249c789d4b6365c0d4c1205d36498i64',
51
            ],
52
        ])));
53
54
        self::$cache->addPackage(Package::buildFromArray(__DIR__ . '/../../cache', array_merge($json, [
55
            'version'       => 'dev-other-feature',
56
            'source'        => [
57
                'type'      => 'git',
58
                'url'       => '[email protected]:vendor/project.git',
59
                'reference' => '1x9f6xo4297r146w9c5469c0d2w8796x65398i10',
60
            ],
61
        ])));
62
63
        $this->assertEquals(3, count(self::$cache));
64
    }
65
66
    /**
67
     *
68
     * @depends testAddPackage
69
     */
70
    public function testRemovePackage()
71
    {
72
        $json = json_decode(file_get_contents(__DIR__ . '/../../data/composer.json'), true);
73
74
        self::$cache->removePackage(Package::buildFromArray(__DIR__ . '/../../cache', array_merge($json, [
75
            'version'       => 'dev-feature',
76
            'source'        => [
77
                'type'      => 'git',
78
                'url'       => '[email protected]:vendor/project.git',
79
                'reference' => '8c7g1iu6249c789d4b6365c0d4c1205d36498i64',
80
            ],
81
        ])));
82
83
        $this->assertEquals(2, count(self::$cache));
84
    }
85
86
    /**
87
     *
88
     * @depends testRemovePackage
89
     */
90
    public function testRefresh()
91
    {
92
        self::$cache->refresh();
93
94
        $json = file_get_contents(__DIR__ . '/../../cache/packages.json');
95
96
        $this->assertEquals(2, count(Repository::buildFromJson(
97
            new Url('https://composer.my-website.com'),
98
            new Dir(__DIR__ . '/../../cache'),
99
            $json
100
        )));
101
102
        $this->assertEquals(
103
            '6a6e0ea9479c821d4b5728c0d3c9840e71085e82',
104
            json_decode($json, true)['packages']['vendor/project']['dev-master']['source']['reference']
105
        );
106
    }
107
108
    public static function tearDownAfterClass()
109
    {
110
        // Clean packages for future tests
111
        file_put_contents(__DIR__ . '/../../cache/packages.json', '{}');
112
    }
113
}
114