Skip to content

Commit 6d14b85

Browse files
committed
arguments validation
1 parent 18ff004 commit 6d14b85

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

models/AsyncExecuteTask.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,32 @@ public function validateMethod($attribute, $params)
3838
}
3939
}
4040

41-
public function validateArguments($attribute, $params) {
41+
public function validateArguments($attribute, $params)
42+
{
4243
if (!isset(array_flip(get_class_methods($this->class))[$this->method])) {
4344
$this->addError(
4445
$attribute,
45-
"Method {$this->method} of class {$this->class} does not exist"
46+
"Can't validate attributes for not existing method {$this->method} of class {$this->class}"
47+
);
48+
return;
49+
}
50+
51+
$refFunc = new \ReflectionMethod($this->class, $this->method);
52+
$userArguments = array_keys($this->{$attribute});
53+
$missingArguments = [];
54+
foreach ($refFunc->getParameters() as $param) {
55+
if (!$param->isOptional() && !in_array($param->getName(), $userArguments)) {
56+
$missingArguments[] = $param->getName();
57+
}
58+
}
59+
60+
if (sizeof($missingArguments)) {
61+
$this->addError(
62+
$attribute,
63+
"Method `{$this->method}` missing required arguments: " . implode(
64+
', ',
65+
$missingArguments
66+
)
4667
);
4768
}
4869
}

tests/unit.suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# suite for unit (internal) tests.
44
class_name: UnitTester
55
modules:
6-
enabled: [Asserts, UnitHelper, Mockery]
6+
enabled: [Asserts, UnitHelper]

tests/unit/AmqpTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestException extends \Exception
1919

2020
class BlackHole
2121
{
22-
public function run($param)
22+
public static function run($param)
2323
{
2424
throw new TestException($param);
2525
}
@@ -94,4 +94,31 @@ public function testAsyncExecuteTask()
9494

9595
$this->fail('BlackHole method wasn\'t called.');
9696
}
97+
98+
public function testArgumentsValidation()
99+
{
100+
$aTask = new AsyncExecuteTask();
101+
$aTask->setAttributes(
102+
[
103+
'class' => '\BlackHole',
104+
'method' => 'run',
105+
'arguments' => ['fail' => 'through the space']
106+
]
107+
);
108+
109+
$this->assertFalse($aTask->validate());
110+
111+
$this->assertEquals(
112+
$aTask->errors['arguments'][0],
113+
"Method `run` missing required arguments: param"
114+
);
115+
116+
try {
117+
$this->async->sendTask($aTask);
118+
} catch (bazilio\async\Exception $e) {
119+
return;
120+
}
121+
122+
$this->fail('Async doesn\'t reject invalid task.');
123+
}
97124
}

tests/unit/UnitTester.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php //[STAMP] 7090f4f20ecbb9880d52b8a3056e9b0b
1+
<?php //[STAMP] 6cd8435c53a77903754e35ca70c87c63
22

33
// This class was automatically generated by build task
44
// You should not change it manually as it will be overwritten on next build
@@ -7,7 +7,6 @@
77

88
use Codeception\Module\Asserts;
99
use Codeception\Module\UnitHelper;
10-
use Codeception\Module\Mockery;
1110

1211
/**
1312
* Inherited Methods

0 commit comments

Comments
 (0)