Commit 394d6542 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Teste SPDXLicenseTemplateProvider.

No related merge requests found
Showing with 97 additions and 3 deletions
+97 -3
......@@ -30,8 +30,10 @@ final class SPDXLicenseTemplateProvider implements TemplateProviderInterface
/**
* SPDXLicenseTemplateProvider constructor.
*/
public function __construct(ComposerPackageInterface $composerPackage, string $urlTemplate = 'https://spdx.org/licenses/%s.json')
{
public function __construct(
ComposerPackageInterface $composerPackage,
string $urlTemplate = 'https://spdx.org/licenses/%s.json'
) {
$this->composerPackage = $composerPackage;
$this->urlTemplate = $urlTemplate;
}
......@@ -52,7 +54,11 @@ final class SPDXLicenseTemplateProvider implements TemplateProviderInterface
private function fetchTemplate(string $spdxId): ?string
{
$licenseJson = @file_get_contents(sprintf($this->urlTemplate, $spdxId));
try {
$licenseJson = @file_get_contents(sprintf($this->urlTemplate, $spdxId));
} catch (\ErrorException $ex) {
return null;
}
if ($licenseJson === false) {
return null;
}
......
<?php declare(strict_types=1);
/**
* Copyright (C) 2020 IRSTEA
* All rights reserved.
*
* @copyright 2020 IRSTEA
* @author guillaume.perreal
*/
namespace Irstea\CS\Tests\HeaderComment;
use Irstea\CS\Composer\ComposerPackageInterface;
use Irstea\CS\HeaderComment\SPDXLicenseTemplateProvider;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
/**
* Class SPDXLicenseTemplateProviderTest
*/
final class SPDXLicenseTemplateProviderTest extends TestCase
{
/**
* @var vfsStreamDirectory
*/
private $root;
/**
* @var ComposerPackageInterface|ObjectProphecy
*/
private $composerPackage;
/**
* @var SPDXLicenseTemplateProviderTest
*/
private $templateProvider;
protected function setUp()
{
$this->root = vfsStream::setup('licenses');
$this->composerPackage = $this->prophesize(ComposerPackageInterface::class);
$this->templateProvider = new SPDXLicenseTemplateProvider(
$this->composerPackage->reveal()
$this->root->url(). '/%s.json'
);
}
public function testKnownLicense() {
$this->composerPackage->getLicenses()
->shouldBeCalled()
->willReturn(['foo']);
$this->root->addChild(vfsStream::newFile('foo.json')->setContent('{"standardLicenseHeader": "bar"}'));
$result = $this->templateProvider->getTemplate();
self::assertEquals('bar', $result);
}
public function testUnknownLicense() {
$this->composerPackage->getLicenses()
->shouldBeCalled()
->willReturn(['foo']);
$result = $this->templateProvider->getTemplate();
self::assertNull($result);
}
public function testInvalidJSON() {
$this->composerPackage->getLicenses()
->shouldBeCalled()
->willReturn(['foo']);
$this->root->addChild(vfsStream::newFile('foo.json')->setContent('bar'));
$result = $this->templateProvider->getTemplate();
self::assertNull($result);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment