Skip to content

Commit 21ab8ea

Browse files
committed
Fixing unittests
1 parent f98cb14 commit 21ab8ea

File tree

13 files changed

+221
-140
lines changed

13 files changed

+221
-140
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/build/logs
66
/build/phar
77
/build/*.phar
8+
/tests/statics/index.*
89

910
# JIRA plugin
1011
atlassian-ide-plugin.xml

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
language: php
22
php:
3-
- '5.5'
43
- '5.6'
5-
- '7.0'
6-
- '7.1'
4+
5+
before_script:
6+
- pecl install runkit
7+
- echo "runkit.internal_override=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
78
script: phpunit --configuration tests/phpunit.xml

index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
require_once 'PHPDraft/Core/Autoloader.php';
1212
use PHPDraft\In\ApibFileParser;
1313
use PHPDraft\Out\UI;
14-
use PHPDraft\Parse\ApibToJson;
14+
use PHPDraft\Parse\Drafter;
1515
use PHPDraft\Parse\JsonToHTML;
1616

1717
define('VERSION', '0');
1818

1919
$values = UI::main($argv);
2020

2121
$apib = new ApibFileParser($values['file']);
22-
$json = new ApibToJson($apib);
22+
$json = new Drafter($apib);
2323
$html = new JsonToHTML($json->parseToJson());
2424
$html->get_html($values['template'], $values['image']);
2525

src/PHPDraft/Core/TestBase.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This file contains the TestBase.php
4+
*
5+
* @package php-drafter\SOMETHING
6+
* @author Sean Molenaar<[email protected]>
7+
*/
8+
9+
namespace PHPDraft\Core;
10+
11+
12+
use PHPUnit_Framework_TestCase;
13+
use ReflectionClass;
14+
15+
class TestBase extends PHPUnit_Framework_TestCase
16+
{
17+
const FUNCTION_ID = '_phpdraftbu';
18+
/**
19+
* Test Class
20+
* @var mixed
21+
*/
22+
protected $class;
23+
24+
/**
25+
* Test reflection
26+
* @var ReflectionClass
27+
*/
28+
protected $reflection;
29+
30+
protected function mock_function($name, $mock)
31+
{
32+
if (function_exists($name . self::FUNCTION_ID) === FALSE)
33+
{
34+
runkit_function_copy($name, $name . self::FUNCTION_ID);
35+
}
36+
runkit_function_redefine($name, '', $mock);
37+
}
38+
/**
39+
* Unmock a PHP function.
40+
*
41+
* @param String $name Function name
42+
*
43+
* @return void
44+
*/
45+
protected function unmock_function($name)
46+
{
47+
runkit_function_remove($name);
48+
runkit_function_rename($name . self::FUNCTION_ID, $name);
49+
}
50+
}

src/PHPDraft/Model/Tests/DataStructureElementTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@
99
namespace PHPDraft\Model\Tests;
1010

1111

12+
use PHPDraft\Core\TestBase;
1213
use PHPDraft\Model\DataStructureElement;
13-
use PHPUnit_Framework_TestCase;
1414
use ReflectionClass;
1515

16-
class DataStructureElementTest extends PHPUnit_Framework_TestCase
16+
class DataStructureElementTest extends TestBase
1717
{
18-
/**
19-
* Test Class
20-
* @var DataStructureElement
21-
*/
22-
protected $class;
23-
24-
/**
25-
* Test reflection
26-
* @var ReflectionClass
27-
*/
28-
protected $reflection;
2918

3019
public function setUp()
3120
{
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
<?php
22
/**
3-
* This file contains the ApibToJson.php
3+
* This file contains the Drafter.php
44
*
55
* @package PHPDraft\Parse
66
* @author Sean Molenaar<[email protected]>
77
*/
88

99
namespace PHPDraft\Parse;
1010

11-
class ApibToJson
11+
class Drafter
1212
{
13+
/**
14+
* The API Blueprint output (JSON)
15+
*
16+
* @var string
17+
*/
18+
public $json;
1319
/**
1420
* Configuration
1521
*
1622
* @var array
1723
*/
1824
protected $config;
19-
2025
/**
2126
* The API Blueprint input
2227
*
@@ -25,11 +30,11 @@ class ApibToJson
2530
protected $apib;
2631

2732
/**
28-
* The API Blueprint output (JSON)
33+
* The location of the drafter executable
2934
*
3035
* @var string
3136
*/
32-
public $json;
37+
protected $drafter;
3338

3439
/**
3540
* ApibToJson constructor.
@@ -39,8 +44,27 @@ class ApibToJson
3944
public function __construct($apib)
4045
{
4146
global $config;
42-
$this->config = $config;
47+
$this->config = &$config;
4348
$this->apib = $apib;
49+
50+
if (!$this->location())
51+
{
52+
throw new \RuntimeException("Drafter was not installed!", 1);
53+
}
54+
$this->drafter = $this->location();
55+
}
56+
57+
/**
58+
* Return drafter location if found
59+
*
60+
* @return bool|string
61+
*/
62+
function location()
63+
{
64+
$returnVal = shell_exec('which drafter 2> /dev/null');
65+
$returnVal = preg_replace('/^\s+|\n|\r|\s+$/m', '', $returnVal);
66+
67+
return (empty($returnVal) ? FALSE : $returnVal);
4468
}
4569

4670
/**
@@ -58,20 +82,16 @@ public function parseToJson()
5882
}
5983

6084
file_put_contents($tmp_dir . '/index.apib', $this->apib);
61-
if (!$this->drafter_location())
62-
{
63-
file_put_contents('php://stderr', "Drafter was not installed!\n");
64-
exit(1);
65-
}
6685

67-
shell_exec($this->drafter_location() . ' ' . $tmp_dir . '/index.apib -f json -o ' . $tmp_dir . '/index.json 2> /dev/null');
86+
shell_exec($this->drafter . ' ' . $tmp_dir . '/index.apib -f json -o ' . $tmp_dir . '/index.json 2> /dev/null');
6887
$this->json = json_decode(file_get_contents($tmp_dir . '/index.json'));
88+
6989
if (json_last_error() !== JSON_ERROR_NONE)
7090
{
71-
file_put_contents('php://stderr', "Drafter generated invalid JSON!\n" . json_last_error_msg() . "\n");
72-
file_put_contents('php://stdout', file_get_contents($tmp_dir . '/index.json') . "\n");
73-
exit(2);
91+
file_put_contents('php://stdout', "ERROR: invalid json in " . $tmp_dir . '/index.json');
92+
throw new \RuntimeException("Drafter generated invalid JSON (" . json_last_error_msg() . ")", 2);
7493
}
94+
7595
$warnings = FALSE;
7696
foreach ($this->json->content as $item)
7797
{
@@ -85,24 +105,10 @@ public function parseToJson()
85105
}
86106
if ($warnings)
87107
{
88-
file_put_contents('php://stderr', "Parsing encountered errors and stopped\n");
89-
exit(2);
108+
throw new \RuntimeException("Parsing encountered errors and stopped", 2);
90109
}
91110

92111
return $this->json;
93112
}
94113

95-
/**
96-
* Return drafter location if found
97-
*
98-
* @return bool|string
99-
*/
100-
function drafter_location()
101-
{
102-
$returnVal = shell_exec('which drafter 2> /dev/null');
103-
$returnVal = preg_replace('/^\s+|\n|\r|\s+$/m', '', $returnVal);
104-
105-
return (empty($returnVal) ? FALSE : $returnVal);
106-
}
107-
108114
}

src/PHPDraft/Parse/Tests/ApibToJsonTest.php

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)