Skip to content

Commit

Permalink
Fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Apr 26, 2020
1 parent d28ed81 commit 6184680
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 94 deletions.
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ sonar.projectKey=SMillerDev_phpdraft
sonar.projectName=PHPDraft
sonar.organization=smillerdev-github
sonar.sources=src/PHPDraft
sonar.php.coverage.reportPaths=coverage/coverage.xml
sonar.php.coverage.reportPaths=coverage.xml
sonar.exclusions=src/PHPDraft/**/Tests/**, tests/**
sonar.coverage.exclusions=src/PHPDraft/Out/HTML/**
110 changes: 110 additions & 0 deletions src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace PHPDraft\Model\Elements\Tests;

use Lunr\Halo\LunrBaseTest;
use PHPDraft\Model\Elements\ElementStructureElement;
use ReflectionClass;

/**
* Class ElementStructureElementTest
* @covers \PHPDraft\Model\Elements\ElementStructureElement
*/
class ElementStructureElementTest extends LunrBaseTest
{

/**
* Set up
*/
public function setUp(): void
{
$this->class = new ElementStructureElement();
$this->reflection = new ReflectionClass('PHPDraft\Model\Elements\ElementStructureElement');
}

/**
* Tear down
*/
public function tearDown(): void
{
unset($this->class);
unset($this->reflection);
}

/**
* @covers \PHPDraft\Model\Elements\ElementStructureElement::parse
*/
public function testParse()
{
$json = '{"element": "Cow", "content": "stuff", "meta": {"description": {"content": "desc"}}}';
$dep = [];
$this->class->parse(json_decode($json), $dep);

$this->assertPropertySame('type', 'Cow');
$this->assertPropertySame('value', 'stuff');
$this->assertPropertySame('description', 'desc');
$this->assertSame(['Cow'], $dep);
}

/**
* @covers \PHPDraft\Model\Elements\ElementStructureElement::string_value
*/
public function testStringValue()
{
$this->assertSame('<li class="list-group-item mdl-list__item"><a class="code" title="" href="#object-"></a></li>', $this->class->string_value());
}

/**
* @covers \PHPDraft\Model\Elements\ElementStructureElement::__toString
*/
public function testToString()
{
$this->set_reflection_property_value('type', 'string');

$this->assertSame('<li class="list-group-item mdl-list__item"><code>string</code></li>', $this->class->__toString());
}

/**
* @covers \PHPDraft\Model\Elements\ElementStructureElement::__toString
*/
public function testToStringCustomType()
{
$this->set_reflection_property_value('type', 'Cow');

$this->assertSame('<li class="list-group-item mdl-list__item"><a class="code" title="Cow" href="#object-cow">Cow</a></li>', $this->class->__toString());
}

/**
* @covers \PHPDraft\Model\Elements\ElementStructureElement::__toString
*/
public function testToStringDescription()
{
$this->set_reflection_property_value('type', 'Cow');
$this->set_reflection_property_value('description', 'Something');

$this->assertSame('<li class="list-group-item mdl-list__item"><a class="code" title="Cow" href="#object-cow">Cow</a> - <span class="description">Something</span></li>', $this->class->__toString());
}

/**
* @covers \PHPDraft\Model\Elements\ElementStructureElement::__toString
*/
public function testToStringValue()
{
$this->set_reflection_property_value('type', 'Cow');
$this->set_reflection_property_value('value', 'stuff');

$this->assertSame('<li class="list-group-item mdl-list__item"><a class="code" title="Cow" href="#object-cow">Cow</a> - <span class="example-value pull-right">stuff</span></li>', $this->class->__toString());
}

/**
* @covers \PHPDraft\Model\Elements\ElementStructureElement::__toString
*/
public function testToStringDescriptionAndValue()
{
$this->set_reflection_property_value('type', 'Cow');
$this->set_reflection_property_value('value', 'stuff');
$this->set_reflection_property_value('description', 'Something');

$this->assertSame('<li class="list-group-item mdl-list__item"><a class="code" title="Cow" href="#object-cow">Cow</a> - <span class="description">Something</span> - <span class="example-value pull-right">stuff</span></li>', $this->class->__toString());
}
}
12 changes: 6 additions & 6 deletions src/PHPDraft/Out/Sorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ class Sorting
*
* @var int
*/
public static $PHPD_SORT_ALL = 3;
public const PHPD_SORT_ALL = 3;

/**
* Sets sorting to all webservices.
*
* @var int
*/
public static $PHPD_SORT_WEBSERVICES = 2;
public const PHPD_SORT_WEBSERVICES = 2;

/**
* Sets sorting to all data structures.
*
* @var int
*/
public static $PHPD_SORT_STRUCTURES = 1;
public const PHPD_SORT_STRUCTURES = 1;

/**
* Sets sorting to no data structures.
*
* @var int
*/
public static $PHPD_SORT_NONE = -1;
public const PHPD_SORT_NONE = -1;

/**
* Check if structures should be sorted.
Expand All @@ -54,7 +54,7 @@ class Sorting
*/
public static function sortStructures(int $sort): bool
{
return $sort === self::$PHPD_SORT_ALL || $sort === self::$PHPD_SORT_STRUCTURES;
return $sort === self::PHPD_SORT_ALL || $sort === self::PHPD_SORT_STRUCTURES;
}

/**
Expand All @@ -66,6 +66,6 @@ public static function sortStructures(int $sort): bool
*/
public static function sortServices(int $sort): bool
{
return $sort === self::$PHPD_SORT_ALL || $sort === self::$PHPD_SORT_WEBSERVICES;
return $sort === self::PHPD_SORT_ALL || $sort === self::PHPD_SORT_WEBSERVICES;
}
}
2 changes: 1 addition & 1 deletion src/PHPDraft/Out/TemplateGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __construct(string $template, ?string $image)
$this->base_data['COLOR_2'] = $template_parts[2] ?? 'light_green';
$this->image = $image;
$this->http_status = new Httpstatus();
$this->sorting = Sorting::$PHPD_SORT_NONE;
$this->sorting = Sorting::PHPD_SORT_NONE;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/PHPDraft/Out/Tests/SortingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* This file contains the SortingTest.php
*
* @package PHPDraft\Out
* @author Sean Molenaar<[email protected]>
*/

namespace PHPDraft\Out\Tests;

use Lunr\Halo\LunrBaseTest;
use PHPDraft\Out\Sorting;
use PHPDraft\Out\Version;
use ReflectionClass;

/**
* Class SortingTest
*
* @covers \PHPDraft\Out\Sorting
*/
class SortingTest extends LunrBaseTest
{

/**
* Test if service sorting is determined correctly.
*
* @covers \PHPDraft\Out\Sorting::sortServices
*/
public function testSortsServicesIfNeeded(): void
{
$this->assertTrue(Sorting::sortServices(3));
$this->assertTrue(Sorting::sortServices(2));
$this->assertFalse(Sorting::sortServices(-1));
$this->assertFalse(Sorting::sortServices(1));
$this->assertFalse(Sorting::sortServices(0));
}

/**
* Test if structure sorting is determined correctly.
*
* @covers \PHPDraft\Out\Sorting::sortStructures
*/
public function testSortsStructureIfNeeded(): void
{
$this->assertTrue(Sorting::sortStructures(3));
$this->assertTrue(Sorting::sortStructures(1));
$this->assertFalse(Sorting::sortStructures(-1));
$this->assertFalse(Sorting::sortStructures(2));
$this->assertFalse(Sorting::sortStructures(0));
}
}
2 changes: 2 additions & 0 deletions src/PHPDraft/Parse/BaseHtmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ abstract class BaseHtmlGenerator
* JsonToHTML constructor.
*
* @param object $json JSON representation of an API Blueprint
*
* @return self
*/
public function init(object $json): self
{
Expand Down
77 changes: 0 additions & 77 deletions src/PHPDraft/Parse/LegacyDrafter.php

This file was deleted.

12 changes: 3 additions & 9 deletions src/PHPDraft/Parse/ParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ParserFactory
/**
* Get the applicable Drafter parser.
*
* @return \PHPDraft\Parse\BaseParser The parser that can be used
* @return BaseParser The parser that can be used
*/
public static function getDrafter(): BaseParser
{
Expand All @@ -22,26 +22,20 @@ public static function getDrafter(): BaseParser
if (DrafterAPI::available()) {
return new DrafterAPI();
}
if (LegacyDrafter::available()) {
throw new ResourceException('Drafter 3.x is no longer supported', 100);
}

throw new ResourceException("Couldn't get an apib parser", 255);
throw new ResourceException("Couldn't get an APIB parser", 255);
}

/**
* Get the applicable JSON parser.
*
* @return \PHPDraft\Parse\BaseHtmlGenerator The parser that can be used
* @return BaseHtmlGenerator The parser that can be used
*/
public static function getJson(): BaseHtmlGenerator
{
if (Drafter::available() || DrafterAPI::available()) {
return new HtmlGenerator();
}
if (LegacyDrafter::available()) {
throw new ResourceException('Drafter 3.x is no longer supported', 100);
}

throw new ResourceException("Couldn't get a JSON parser", 255);
}
Expand Down
Loading

0 comments on commit 6184680

Please sign in to comment.