Skip to content

Commit fba432f

Browse files
author
Mateusz Sojda
committed
Added some tests
1 parent ed0e48e commit fba432f

3 files changed

Lines changed: 389 additions & 3 deletions

File tree

src/Cache/CacheFileConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public function toJson($cacheFile, array $options = array())
5151

5252
$jsonData = new stdClass();
5353
$jsonData->modules = array();
54-
$jsonData->installInfo = array();
54+
$jsonData->installInfos = array();
5555

5656
foreach ($cacheFile->getModuleFiles() as $moduleFile) {
5757
$jsonData->modules[] = $this->moduleFileConverter->toJson($moduleFile, array(
5858
'targetVersion' => $moduleFile->getVersion(),
5959
));
6060
}
6161
foreach ($cacheFile->getInstallInfos() as $installInfo) {
62-
$jsonData->installInfo[$installInfo->getModuleName()] = $installInfo->getInstallPath();
62+
$jsonData->installInfos[$installInfo->getModuleName()] = $installInfo->getInstallPath();
6363
}
6464

6565
return $jsonData;
@@ -79,7 +79,7 @@ public function fromJson($jsonData, array $options = array())
7979
$cacheFile->addModuleFile($moduleFile);
8080
}
8181

82-
foreach ($jsonData->installInfo as $packageName => $installPath) {
82+
foreach ($jsonData->installInfos as $packageName => $installPath) {
8383
$cacheFile->addInstallInfo(new InstallInfo($packageName, $installPath));
8484
}
8585

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the puli/manager package.
5+
*
6+
* (c) Bernhard Schussek <bschussek@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Puli\Manager\Tests\Cache;
13+
14+
use PHPUnit_Framework_MockObject_MockObject;
15+
use PHPUnit_Framework_TestCase;
16+
use Puli\Manager\Api\Cache\CacheFile;
17+
use Puli\Manager\Api\Module\InstallInfo;
18+
use Puli\Manager\Api\Module\ModuleFile;
19+
use Puli\Manager\Cache\CacheFileConverter;
20+
use stdClass;
21+
use Webmozart\Json\Conversion\JsonConverter;
22+
23+
/**
24+
* @since 1.0
25+
*
26+
* @author Mateusz Sojda <mateusz@sojda.pl>
27+
*/
28+
class CacheFileConverterTest extends PHPUnit_Framework_TestCase
29+
{
30+
/**
31+
* @var ModuleFile
32+
*/
33+
private $moduleFile1;
34+
35+
/**
36+
* @var ModuleFile
37+
*/
38+
private $moduleFile2;
39+
40+
/**
41+
* @var stdClass
42+
*/
43+
private $jsonDataModule1;
44+
45+
/**
46+
* @var stdClass
47+
*/
48+
private $jsonDataModule2;
49+
50+
/**
51+
* @var InstallInfo
52+
*/
53+
private $installInfo1;
54+
55+
/**
56+
* @var InstallInfo
57+
*/
58+
private $installInfo2;
59+
60+
/**
61+
* @var CacheFileConverter
62+
*/
63+
private $converter;
64+
65+
/**
66+
* @var PHPUnit_Framework_MockObject_MockObject|JsonConverter
67+
*/
68+
private $moduleFileConverter;
69+
70+
protected function setUp()
71+
{
72+
$this->moduleFile1 = new ModuleFile('vendor/module1');
73+
$this->moduleFile2 = new ModuleFile('vendor/module2');
74+
75+
$this->installInfo1 = new InstallInfo('vendor/module1', '../module1');
76+
$this->installInfo2 = new InstallInfo('vendor/module2', '../module2');
77+
78+
$this->jsonDataModule1 = (object) array(
79+
'$schema' => 'http://puli.io/schema/2.0/manager/module',
80+
'name' => 'vendor/module1',
81+
);
82+
$this->jsonDataModule2 = (object) array(
83+
'$schema' => 'http://puli.io/schema/2.0/manager/module',
84+
'name' => 'vendor/module2',
85+
);
86+
87+
$this->moduleFileConverter = $this->getMock('Webmozart\Json\Conversion\JsonConverter');
88+
89+
$this->converter = new CacheFileConverter($this->moduleFileConverter);
90+
}
91+
92+
public function testToJson()
93+
{
94+
$this->moduleFileConverter
95+
->expects($this->at(0))
96+
->method('toJson')->with($this->moduleFile1, array(
97+
'targetVersion' => $this->moduleFile1->getVersion(),
98+
))
99+
->willReturn($this->jsonDataModule1);
100+
101+
$this->moduleFileConverter
102+
->expects($this->at(1))
103+
->method('toJson')->with($this->moduleFile2, array(
104+
'targetVersion' => $this->moduleFile2->getVersion(),
105+
))
106+
->willReturn($this->jsonDataModule2);
107+
108+
$cacheFile = new CacheFile('/path');
109+
110+
$cacheFile->addModuleFile($this->moduleFile1);
111+
$cacheFile->addModuleFile($this->moduleFile2);
112+
113+
$cacheFile->addInstallInfo($this->installInfo1);
114+
$cacheFile->addInstallInfo($this->installInfo2);
115+
116+
$jsonData = (object) array(
117+
'modules' => array(
118+
$this->jsonDataModule1,
119+
$this->jsonDataModule2,
120+
),
121+
'installInfos' => array(
122+
'vendor/module1' => '../module1',
123+
'vendor/module2' => '../module2',
124+
),
125+
);
126+
127+
$this->assertEquals($jsonData, $this->converter->toJson($cacheFile));
128+
}
129+
130+
public function testToJsonWithEmptyModules()
131+
{
132+
$this->moduleFileConverter
133+
->expects($this->never())
134+
->method('toJson');
135+
136+
$cacheFile = new CacheFile('/path');
137+
138+
$jsonData = (object) array(
139+
'modules' => array(),
140+
'installInfos' => array(),
141+
);
142+
143+
$this->assertEquals($jsonData, $this->converter->toJson($cacheFile));
144+
}
145+
146+
public function testFromJson()
147+
{
148+
$this->moduleFileConverter
149+
->expects($this->at(0))
150+
->method('fromJson')
151+
->with($this->jsonDataModule1)
152+
->willReturn($this->moduleFile1);
153+
154+
$this->moduleFileConverter
155+
->expects($this->at(1))
156+
->method('fromJson')
157+
->with($this->jsonDataModule2)
158+
->willReturn($this->moduleFile2);
159+
160+
$jsonData = (object) array(
161+
'modules' => array(
162+
$this->jsonDataModule1,
163+
$this->jsonDataModule2,
164+
),
165+
'installInfos' => array(
166+
'vendor/module1' => '../module1',
167+
'vendor/module2' => '../module2',
168+
),
169+
);
170+
171+
$cacheFile = $this->converter->fromJson($jsonData, array(
172+
'path' => '/path',
173+
));
174+
175+
$this->assertInstanceOf('Puli\Manager\Api\Cache\CacheFile', $cacheFile);
176+
$this->assertEquals('/path', $cacheFile->getPath());
177+
$this->assertEquals(2, count($cacheFile->getModuleFiles()));
178+
$this->assertTrue($cacheFile->hasModuleFile('vendor/module1'));
179+
$this->assertTrue($cacheFile->hasModuleFile('vendor/module2'));
180+
$this->assertTrue($cacheFile->hasInstallInfo('vendor/module1'));
181+
$this->assertTrue($cacheFile->hasInstallInfo('vendor/module2'));
182+
}
183+
184+
public function testFromJsonWithEmptyModules()
185+
{
186+
$this->moduleFileConverter
187+
->expects($this->never())
188+
->method('fromJson');
189+
190+
$jsonData = (object) array(
191+
'modules' => array(),
192+
'installInfos' => array(),
193+
);
194+
195+
$cacheFile = $this->converter->fromJson($jsonData, array(
196+
'path' => '/path',
197+
));
198+
199+
$this->assertInstanceOf('Puli\Manager\Api\Cache\CacheFile', $cacheFile);
200+
$this->assertEquals('/path', $cacheFile->getPath());
201+
$this->assertEquals(0, count($cacheFile->getModuleFiles()));
202+
$this->assertFalse($cacheFile->hasModuleFiles());
203+
$this->assertFalse($cacheFile->hasInstallInfos());
204+
}
205+
206+
public function testFromJsonWithEmptyPath()
207+
{
208+
$jsonData = (object) array(
209+
'modules' => array(),
210+
'installInfos' => array(),
211+
);
212+
213+
$cacheFile = $this->converter->fromJson($jsonData);
214+
215+
$this->assertInstanceOf('Puli\Manager\Api\Cache\CacheFile', $cacheFile);
216+
$this->assertNull($cacheFile->getPath());
217+
}
218+
}

0 commit comments

Comments
 (0)