Skip to content

Commit 5dc5ffa

Browse files
committed
Add passing arguments to cli
1 parent 2dc4641 commit 5dc5ffa

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

Unit.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Unit
1414
private Command $command;
1515
private bool $quite;
1616
private ?string $title;
17+
private array $args = [];
1718
private array $units;
1819
private int $index = 0;
1920
private array $error;
@@ -24,10 +25,35 @@ public function __construct(bool $quite = false)
2425
$this->quite = $quite;
2526
}
2627

28+
/**
29+
* Add title to the test (optional)
30+
* @param string $title
31+
* @return void
32+
*/
2733
public function addTitle(string $title): void
2834
{
2935
$this->title = $title;
3036
}
37+
38+
/**
39+
* Pass arguments to the testing script (optional)
40+
* @param array $args
41+
* @return void
42+
*/
43+
public function setArgs(array $args): void
44+
{
45+
$this->args = $args;
46+
}
47+
48+
/**
49+
* Get passed arguments in the testing script
50+
* @return array
51+
*/
52+
public function getArgs(): array
53+
{
54+
return $this->args;
55+
}
56+
3157
/**
3258
* Add a test unit/group
3359
* @param string $message
@@ -94,6 +120,7 @@ public function executeAll(string $directory): void
94120
throw new Exception("No files found matching the pattern \"" . static::PATTERN . "\" in directory \"$directory\" ");
95121
} else {
96122
foreach ($files as $file) {
123+
extract($this->args, EXTR_PREFIX_SAME, "wddx");
97124
require_once ($file);
98125
}
99126
}

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
"maplephp/validate": "^1.0",
2323
"maplephp/prompts": "^1.0"
2424
},
25+
"replace": {
26+
"maplephp/dto": "self.version",
27+
"maplephp/http": "self.version",
28+
"maplephp/validate": "self.version",
29+
"maplephp/prompts": "self.version"
30+
},
2531
"autoload": {
2632
"psr-4": {
2733
"MaplePHP\\Unitary\\": ""

examples/unitary-test.php renamed to tests/unitary-test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// If you add true to Unit it will run in quite mode
1919
// and only report if it finds any errors!
20-
$unit = new Unit(true);
20+
$unit = new Unit();
2121

2222
// Add a title to your tests (not required)
2323
$unit->addTitle("Testing MaplePHP Unitary library!");

tools/unitary

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env php
2+
<?php
3+
use MaplePHP\Http\Environment;
4+
use MaplePHP\Http\ServerRequest;
5+
use MaplePHP\Http\Uri;
6+
use MaplePHP\Prompts\Command;
7+
use MaplePHP\Unitary\Unit;
8+
9+
if (!class_exists(Unit::class)) {
10+
$dir = realpath(__DIR__ . "/../../../");
11+
$alFile = (defined("UNITARY_AUTOLOAD_FILE") ? UNITARY_AUTOLOAD_FILE : "$dir/vendor/autoload.php");
12+
if (is_file($alFile)) {
13+
require_once($alFile);
14+
} else {
15+
die("Please run 'composer install' before running the example test suite");
16+
}
17+
}
18+
19+
$unit = new Unit();
20+
$command = new Command();
21+
$env = new Environment();
22+
$request = new ServerRequest(new Uri($env->getUriParts([
23+
"argv" => $argv
24+
])), $env);
25+
26+
$data = $request->getCliArgs();
27+
$defaultPath = (defined("UNITARY_PATH") ? UNITARY_PATH : null);
28+
29+
try {
30+
$path = ($data['path'] ?? $defaultPath);
31+
if(!isset($path)) {
32+
throw new Exception("Path not specified: --path=path/to/dir");
33+
}
34+
35+
$testDir = realpath($data['path']);
36+
if(!is_dir($testDir)) {
37+
throw new Exception("Test directory '$testDir' does not exist");
38+
}
39+
$unit->setArgs($data);
40+
$unit->executeAll($testDir);
41+
42+
} catch (Exception $e) {
43+
$command->error($e->getMessage());
44+
}

0 commit comments

Comments
 (0)