Skip to content

Commit d5304dd

Browse files
committed
Initial.
1 parent 08794a3 commit d5304dd

File tree

7 files changed

+238
-0
lines changed

7 files changed

+238
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020

2121
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2222
hs_err_pid*
23+
24+
25+
.idea/

package.php.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ sources:
1313
- src
1414

1515
plugin:
16+
list: [tester\TesterPlugin]
1617
sources:
1718
- src-plugin

src-plugin/tester/TesterPlugin.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
namespace tester;
3+
4+
use packager\Event;
5+
use packager\JavaExec;
6+
use packager\Vendor;
7+
use php\lib\arr;
8+
use php\lib\fs;
9+
use phpx\parser\ClassRecord;
10+
use phpx\parser\SourceFile;
11+
use phpx\parser\SourceManager;
12+
13+
/**
14+
*
15+
* @jppm-task-prefix tester
16+
*
17+
* @jppm-task run
18+
*/
19+
class TesterPlugin
20+
{
21+
/**
22+
* @jppm-need-package
23+
*
24+
* @jppm-denendency-of test
25+
*
26+
* @jppm-description Run all tests.
27+
* @param $event
28+
*/
29+
public function run(Event $event)
30+
{
31+
$vendor = new Vendor($event->package()->getConfigVendorPath());
32+
33+
$exec = new JavaExec();
34+
$exec->setSystemProperties([
35+
'bootstrap.file' => 'tester/.bootstrap.php'
36+
]);
37+
38+
$exec->addPackageClassPath($event->package());
39+
$exec->addVendorClassPath($vendor);
40+
$exec->addClassPath("./tests");
41+
42+
$files = fs::scan("./tests/", ['extensions' => ['php']]);
43+
44+
$manager = new SourceManager();
45+
46+
$testCases = [];
47+
48+
foreach ($files as $file) {
49+
$source = new SourceFile($file, fs::relativize($file, "./tests/"));
50+
$source->update($manager);
51+
52+
foreach ($source->moduleRecord->getClasses() as $class) {
53+
if ($this->isTestCase($class, $testCases)) {
54+
$testCases[$class->name] = $class->name;
55+
}
56+
}
57+
}
58+
59+
foreach ($files as $file) {
60+
$source = new SourceFile($file, fs::relativize($file, "./tests/"));
61+
$source->update($manager);
62+
63+
foreach ($source->moduleRecord->getClasses() as $class) {
64+
if ($testCases[$class->name]) continue;
65+
66+
if ($this->isTestCase($class, $testCases)) {
67+
$testCases[$class->name] = $class->name;
68+
}
69+
}
70+
}
71+
72+
fs::format($vendor->getDir() . "/tester.json", [
73+
'testCases' => arr::values($testCases)
74+
]);
75+
}
76+
77+
protected function isTestCase(ClassRecord $record, array $otherTestCases = [])
78+
{
79+
if ($record->parent && !$record->abstract && $record->type === 'CLASS') {
80+
if ($record->parent->name === 'tester\TestCase' || $otherTestCases[$record->parent->name]) {
81+
return true;
82+
}
83+
}
84+
85+
return false;
86+
}
87+
}

src/tester/.bootstrap.php

Whitespace-only changes.

src/tester/Constraint.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace tester;
4+
use php\lib\str;
5+
6+
/**
7+
* Class Constraint
8+
* @package tester
9+
*/
10+
class Constraint
11+
{
12+
/**
13+
* @var array
14+
*/
15+
private $args;
16+
private $value;
17+
18+
/**
19+
* @var callable
20+
*/
21+
private $evalute;
22+
/**
23+
* @var callable
24+
*/
25+
private $getMessage;
26+
27+
/**
28+
* Constraint constructor.
29+
* @param $value
30+
* @param array $args
31+
* @param callable $evalute
32+
* @param callable $getMessage
33+
*/
34+
public function __construct($value, array $args, callable $evalute, callable $getMessage)
35+
{
36+
$this->args = $args;
37+
$this->value = $value;
38+
$this->evalute = $evalute;
39+
$this->getMessage = $getMessage;
40+
}
41+
42+
public function getMessage(): string
43+
{
44+
return call_user_func($this->getMessage, $this->value, $this->args);
45+
}
46+
47+
/**
48+
* @param $other
49+
* @return bool
50+
*/
51+
public function evalute($other): bool
52+
{
53+
return call_user_func($this->evalute, $other, $this->value, $this->args);
54+
}
55+
56+
/**
57+
* @param Constraint $constraint
58+
* @return Constraint
59+
*/
60+
static function isNot(Constraint $constraint): Constraint
61+
{
62+
return new Constraint($constraint->value, [], function ($other, $expected) use ($constraint) {
63+
return !$constraint->evalute($other);
64+
}, function ($value, $args) use ($constraint) {
65+
return "is NOT ({$constraint->getMessage()})";
66+
});
67+
}
68+
69+
/**
70+
* @param $value
71+
* @return Constraint
72+
*/
73+
static function isEqual($value): Constraint
74+
{
75+
return new Constraint(
76+
$value, [],
77+
function ($other, $expected) {
78+
return $other == $expected;
79+
},
80+
function ($value, $args) {
81+
if (is_string($value)) {
82+
$lines = str::lines($value);
83+
84+
if (sizeof($lines) < 2) {
85+
return "is equal to \"$value\"";
86+
} else {
87+
return "is equal to <text> \"$lines[0] ...\"";
88+
}
89+
} else {
90+
return "is equal to " . var_export($value);
91+
}
92+
}
93+
);
94+
}
95+
}

src/tester/TestCase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace tester;
3+
4+
/**
5+
*
6+
*/
7+
abstract class TestCase
8+
{
9+
/**
10+
* @param $expected
11+
* @param $actual
12+
* @param string $message
13+
*/
14+
public function assertEquals($expected, $actual, string $message = '')
15+
{
16+
$this->assertThat($expected, Constraint::isEqual($actual), $message);
17+
}
18+
19+
/**
20+
* @param $expected
21+
* @param $actual
22+
* @param string $message
23+
*/
24+
public function assertNotEquals($expected, $actual, string $message = '')
25+
{
26+
$this->assertThat($expected, Constraint::isNot(Constraint::isEqual($actual)), $message);
27+
}
28+
29+
/**
30+
* @param mixed $value
31+
* @param Constraint $constraint
32+
* @param string $message
33+
*/
34+
public function assertThat($value, Constraint $constraint, string $message = "")
35+
{
36+
if (!$constraint->evalute($value)) {
37+
$value = var_export($value, true);
38+
39+
echo "{$value} {$constraint->getMessage()}", "\n";
40+
}
41+
}
42+
}

src/tester/TestEnvironment.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace tester;
3+
4+
/**
5+
* Class TestEnvironment
6+
* @package tester
7+
*/
8+
class TestEnvironment
9+
{
10+
}

0 commit comments

Comments
 (0)