Skip to content

Commit 5857e5d

Browse files
committed
test: add tests
1 parent 44f723e commit 5857e5d

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,21 @@
1515
"php": "^7.4 || ^8.0",
1616
"ext-json": "*",
1717
"ext-ffi": "*"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "^9.5 || ^10.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Bfabio\\PublicCodeParser\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Bfabio\\PublicCodeParser\\Tests\\": "tests/"
30+
}
31+
},
32+
"scripts": {
33+
"test": "vendor/bin/phpunit"
1834
}
1935
}

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
cacheDirectory=".phpunit.cache"
7+
executionOrder="depends,defects"
8+
requireCoverageMetadata="false"
9+
beStrictAboutOutputDuringTests="true"
10+
failOnRisky="true"
11+
failOnWarning="true">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<source>
18+
<include>
19+
<directory suffix=".php">src</directory>
20+
</include>
21+
</source>
22+
</phpunit>

tests/ParserTest.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bfabio\PublicCodeParser\Tests;
6+
7+
use Bfabio\PublicCodeParser\Exception\ParserException;
8+
use Bfabio\PublicCodeParser\Exception\ValidationException;
9+
use Bfabio\PublicCodeParser\Parser;
10+
use Bfabio\PublicCodeParser\ParserOptions;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class ParserTest extends TestCase
14+
{
15+
private Parser $parser;
16+
17+
protected function setUp(): void
18+
{
19+
$this->parser = new Parser();
20+
}
21+
22+
public function testParseValidFile(): void
23+
{
24+
$yaml = $this->getValidPublicCodeYaml();
25+
$publicCode = $this->parser->parse($yaml);
26+
27+
$this->assertEquals('Medusa', $publicCode->getName());
28+
$this->assertEquals('https://example.com/medusa', $publicCode->getUrl());
29+
$this->assertNotNull($publicCode->getDescription('en'));
30+
}
31+
32+
public function testParseInvalidFile(): void
33+
{
34+
$this->expectException(ValidationException::class);
35+
36+
$yaml = "invalid: yaml: content:";
37+
$this->parser->parse($yaml);
38+
}
39+
40+
public function testParseWithOptions(): void
41+
{
42+
$options = new ParserOptions();
43+
$options->setDisableNetwork(true);
44+
45+
$parser = new Parser($options);
46+
$yaml = $this->getValidPublicCodeYaml();
47+
48+
$publicCode = $parser->parse($yaml);
49+
$this->assertNotNull($publicCode);
50+
}
51+
52+
public function testPublicCodeAccessors(): void
53+
{
54+
$yaml = $this->getValidPublicCodeYaml();
55+
$publicCode = $this->parser->parse($yaml);
56+
57+
$this->assertEquals('Medusa', $publicCode->getName());
58+
$this->assertEquals('AGPL-3.0-or-later', $publicCode->getLicense());
59+
$this->assertEquals(['web'], $publicCode->getPlatforms());
60+
61+
$this->assertNotNull($publicCode->getDescription('en'));
62+
$this->assertNotNull($publicCode->getDescription('it'));
63+
64+
$maintenance = $publicCode->getMaintenance();
65+
$this->assertEquals('internal', $maintenance['type']);
66+
67+
$categories = $publicCode->getCategories();
68+
$this->assertContains('it-development', $categories);
69+
}
70+
71+
public function testToArray(): void
72+
{
73+
$yaml = $this->getValidPublicCodeYaml();
74+
$publicCode = $this->parser->parse($yaml);
75+
76+
$array = $publicCode->toArray();
77+
$this->assertIsArray($array);
78+
$this->assertArrayHasKey('name', $array);
79+
$this->assertArrayHasKey('url', $array);
80+
}
81+
82+
public function testToJson(): void
83+
{
84+
$yaml = $this->getValidPublicCodeYaml();
85+
$publicCode = $this->parser->parse($yaml);
86+
87+
$json = $publicCode->toJson();
88+
$this->assertJson($json);
89+
90+
$decoded = json_decode($json, true);
91+
$this->assertEquals('Medusa', $decoded['name']);
92+
}
93+
94+
public function testValidate(): void
95+
{
96+
$tempFile = tempnam(sys_get_temp_dir(), 'publiccode');
97+
file_put_contents($tempFile, $this->getValidPublicCodeYaml());
98+
99+
$this->assertTrue($this->parser->validate($tempFile));
100+
101+
unlink($tempFile);
102+
}
103+
104+
public function testValidateInvalid(): void
105+
{
106+
$tempFile = tempnam(sys_get_temp_dir(), 'publiccode');
107+
file_put_contents($tempFile, 'invalid: yaml:');
108+
109+
$this->assertFalse($this->parser->validate($tempFile));
110+
111+
unlink($tempFile);
112+
}
113+
114+
public function testGetDependencies(): void
115+
{
116+
$yaml = $this->getValidPublicCodeYaml();
117+
$publicCode = $this->parser->parse($yaml);
118+
119+
$deps = $publicCode->getDependencies();
120+
$this->assertIsArray($deps);
121+
}
122+
123+
private function getValidPublicCodeYaml(): string
124+
{
125+
return <<<YAML
126+
publiccodeYmlVersion: "0.2"
127+
name: Medusa
128+
url: "https://example.com/medusa"
129+
landingURL: "https://example.com/medusa"
130+
releaseDate: "2017-04-15"
131+
developmentStatus: stable
132+
softwareType: standalone/web
133+
platforms:
134+
- web
135+
categories:
136+
- it-development
137+
maintenance:
138+
type: internal
139+
contacts:
140+
- name: Francesco Rossi
141+
email: "francesco.rossi@comune.reggioemilia.it"
142+
legal:
143+
license: AGPL-3.0-or-later
144+
repoOwner: Comune di Reggio Emilia
145+
description:
146+
it:
147+
shortDescription: >
148+
Sistema di gestione documentale
149+
longDescription: >
150+
Medusa è un sistema di gestione documentale che permette
151+
di organizzare e gestire i documenti digitali.
152+
features:
153+
- Gestione documenti
154+
- Workflow documentale
155+
en:
156+
shortDescription: >
157+
Document management system
158+
longDescription: >
159+
Medusa is a document management system that allows
160+
organizing and managing digital documents.
161+
features:
162+
- Document management
163+
- Document workflow
164+
YAML;
165+
}
166+
}

0 commit comments

Comments
 (0)