Skip to content

Commit dd29e16

Browse files
authored
Merge pull request #37 from SMillerDev/fix/unittests
General: Improve test coverage
2 parents ea1bdc9 + 9dfc714 commit dd29e16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+6438
-352
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/build/coverage
55
/build/logs
66
/build/phar
7+
/build/tmp
78
/build/*.phar
89
/tests/statics/index.*
910
src/Michelf/*

index.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@
1919
$values = UI::main($argv);
2020

2121
$apib = new ApibFileParser($values['file']);
22+
$apib = $apib->parse();
23+
2224
$json = new DrafterAPI($apib);
23-
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1)) {
24-
try {
25+
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1))
26+
{
27+
try
28+
{
2529
$json = new Drafter($apib);
26-
} catch (RuntimeException $exception) {
27-
file_put_contents('php://stderr', $exception->getMessage()."\n");
30+
}
31+
catch (RuntimeException $exception)
32+
{
33+
file_put_contents('php://stderr', $exception->getMessage() . "\n");
2834
$options = [
2935
'y' => 'Yes',
3036
'n' => 'No',
3137
];
32-
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
33-
if (!$answer) {
38+
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
39+
if (!$answer)
40+
{
3441
file_put_contents('php://stderr', 'Could not find a suitable drafter version');
3542
exit(1);
3643
}
@@ -39,19 +46,19 @@
3946

4047
$html = new JsonToHTML($json->parseToJson());
4148
$html->sorting = $values['sorting'];
42-
$html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
49+
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
4350

4451

4552
function phpdraft_var_dump(...$vars)
4653
{
47-
if (defined('__PHPDRAFT_PHAR__')) {
54+
if (defined('__PHPDRAFT_PHAR__'))
55+
{
4856
return;
4957
}
5058
echo '<pre>';
51-
foreach ($vars as $var) {
59+
foreach ($vars as $var)
60+
{
5261
var_dump($var);
5362
}
5463
echo '</pre>';
5564
}
56-
57-
?>

src/PHPDraft/Core/BaseTest.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ private function __runkit_mock_function($name, $return)
7979
runkit_function_copy($name, $name . self::FUNCTION_ID);
8080
}
8181

82-
runkit_function_redefine($name, '', 'return ' . (is_string($return) ? ('"' . $return . '"') : $return) . ';');
82+
runkit_function_redefine($name, function () use ($return) {
83+
return $return;
84+
});
8385
}
8486

8587
/**
@@ -94,9 +96,7 @@ private function __uopz_mock_function($name, $return)
9496
{
9597
if (PHP_MAJOR_VERSION < 7) {
9698
\uopz_backup($name);
97-
\uopz_function($name, function () {
98-
global $return;
99-
99+
\uopz_function($name, function () use ($return){
100100
return $return;
101101
});
102102

@@ -158,4 +158,24 @@ private function __uopz_unmock_function($name)
158158

159159
uopz_unset_return($name);
160160
}
161+
162+
/**
163+
* Redefine a constant
164+
*
165+
* @param string $name Constant to redefine
166+
* @param mixed $value Value to define to
167+
*/
168+
public function redefine($name, $value)
169+
{
170+
if (extension_loaded('runkit') === true) {
171+
\runkit_constant_redefine($name, $value);
172+
return;
173+
}
174+
175+
if (extension_loaded('uopz') === true) {
176+
\uopz_redefine($name, $value);
177+
return;
178+
}
179+
180+
}
161181
}

src/PHPDraft/In/ApibFileParser.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,31 @@ class ApibFileParser
2727
*/
2828
protected $location;
2929

30+
/**
31+
* Filename to parse
32+
*
33+
* @var
34+
*/
35+
private $filename;
36+
3037
/**
3138
* FileParser constructor.
3239
*
3340
* @param string $filename File to parse
3441
*/
3542
public function __construct($filename = 'index.apib')
3643
{
37-
$this->location = pathinfo($filename, PATHINFO_DIRNAME) . '/';
44+
$this->filename = $filename;
45+
$this->location = pathinfo($this->filename, PATHINFO_DIRNAME) . '/';
3846

3947
set_include_path(get_include_path() . ':' . $this->location);
48+
}
49+
50+
public function parse()
51+
{
52+
$this->full_apib = $this->get_apib($this->filename);
4053

41-
$this->full_apib = $this->get_apib($filename);
54+
return $this;
4255
}
4356

4457
/**
@@ -49,7 +62,7 @@ public function __construct($filename = 'index.apib')
4962
*
5063
* @return string The full API blueprint file
5164
*/
52-
function get_apib($filename)
65+
private function get_apib($filename)
5366
{
5467
$this->file_check($filename);
5568
$file = file_get_contents($filename);
@@ -90,7 +103,7 @@ private function file_check($filename)
90103
*
91104
* @return string The schema as a string
92105
*/
93-
function get_schema($url)
106+
private function get_schema($url)
94107
{
95108
$ch = curl_init();
96109
curl_setopt($ch, CURLOPT_URL, $url);

src/PHPDraft/In/Tests/ApibFileParserTest.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* Class ApibFileParserTest
17-
* @covers PHPDraft\In\ApibFileParser
17+
* @covers \PHPDraft\In\ApibFileParser
1818
*/
1919
class ApibFileParserTest extends BaseTest
2020
{
@@ -33,11 +33,49 @@ public function setUp()
3333
* Test if setup is successful
3434
* @return void
3535
*/
36-
public function testSetup()
36+
public function testLocationSetup()
3737
{
3838
$property = $this->reflection->getProperty('location');
39-
$property->setAccessible(TRUE);
39+
$property->setAccessible(true);
4040
$this->assertSame(__DIR__ . '/', $property->getValue($this->class));
4141
}
4242

43+
/**
44+
* Test if setup is successful
45+
* @return void
46+
*/
47+
public function testFilenameSetup()
48+
{
49+
$property = $this->reflection->getProperty('filename');
50+
$property->setAccessible(true);
51+
$this->assertSame(__DIR__ . '/ApibFileParserTest.php', $property->getValue($this->class));
52+
}
53+
54+
/**
55+
* Test if setup is successful
56+
* @return void
57+
*/
58+
public function testParseBasic()
59+
{
60+
$property = $this->reflection->getProperty('filename');
61+
$property->setAccessible(true);
62+
$property->setValue($this->class, TEST_STATICS . '/drafter/including_apib');
63+
$loc_property = $this->reflection->getProperty('location');
64+
$loc_property->setAccessible(true);
65+
$loc_property->setValue($this->class, TEST_STATICS . '/drafter/');
66+
67+
$this->mock_function('curl_exec', 'hello');
68+
$this->class->parse();
69+
$this->unmock_function('curl_exec');
70+
71+
$full_property = $this->reflection->getProperty('full_apib');
72+
$full_property->setAccessible(true);
73+
74+
$text = "FORMAT: 1A\nHOST: https://owner-api.teslamotors.com\nEXTRA_HOSTS: https://test.owner-api.teslamotors.com\nSOMETHING: INFO\n\n";
75+
$text .="# Tesla Model S JSON API\nThis is unofficial documentation of the Tesla Model S JSON API used by the iOS and Android apps. It features functionality to monitor and control the Model S remotely.\n\nTEST\nhello";
76+
77+
$this->assertSame($text, $full_property->getValue($this->class));
78+
$this->assertSame($text, $this->class->__toString());
79+
}
80+
4381
}

src/PHPDraft/Model/Category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function parse($object)
4444
$struct->deps = $deps;
4545
$struct->parse($item, $deps);
4646

47-
if (isset($item->content[0]->meta->id))
47+
if (is_array($item->content) && isset($item->content[0]->meta->id))
4848
{
4949
$this->structures[$item->content[0]->meta->id] = $struct;
5050
} else {

src/PHPDraft/Model/Elements/ArrayStructureElement.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
namespace PHPDraft\Model\Elements;
1010

11-
use PHPDraft\Model\StructureElement;
12-
1311
/**
1412
* Class ArrayStructureElement
1513
*/
16-
class ArrayStructureElement extends ObjectStructureElement implements StructureElement
14+
class ArrayStructureElement extends BasicStructureElement
1715
{
1816

1917
/**
@@ -22,7 +20,7 @@ class ArrayStructureElement extends ObjectStructureElement implements StructureE
2220
* @param \stdClass $object APIb Item to parse
2321
* @param array $dependencies List of dependencies build
2422
*
25-
* @return $this
23+
* @return self Self reference
2624
*/
2725
public function parse($object, &$dependencies)
2826
{
@@ -66,7 +64,7 @@ function __toString()
6664
}
6765

6866
foreach ($this->value as $item) {
69-
$type = (in_array($this->value, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-',
67+
$type = (in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-',
7068
strtolower($item)) . '">' . $item . '</a>';
7169

7270
$return .= '<li class="list-group-item">' . $type . '</li>';
@@ -77,4 +75,13 @@ function __toString()
7775
return $return;
7876
}
7977

78+
/**
79+
* Get a new instance of a class
80+
*
81+
* @return self
82+
*/
83+
protected function new_instance()
84+
{
85+
return new ArrayStructureElement();
86+
}
8087
}

0 commit comments

Comments
 (0)