Skip to content

Commit 18ff004

Browse files
committed
AsyncExecuteTask
1 parent adc76b9 commit 18ff004

File tree

7 files changed

+132
-8
lines changed

7 files changed

+132
-8
lines changed

AsyncComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function sendTask(AsyncTask $task)
3434
if ($task->validate()) {
3535
return $this->transport->send(serialize($task), $task::$queueName);
3636
} else {
37-
throw new Exception(implode(',', $task->errors));
37+
throw new Exception(var_export($task->errors, true));
3838
}
3939
}
4040

Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace bazilio\async;
33

4-
class Exception extends yii\base\Exception
4+
class Exception extends \yii\base\Exception
55
{
66

77
}

composer.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,24 @@
1616
"require-dev": {
1717
"phpunit/phpunit": "3.7.*",
1818
"yiisoft/yii2-codeception": "*",
19-
"codeception/codeception": "*"
19+
"codeception/codeception": "*",
20+
"pdezwart/php-amqp": "dev-master"
2021
},
2122
"autoload": {
2223
"psr-4": {
2324
"bazilio\\async\\": ""
2425
}
25-
}
26+
},
27+
"repositories": [{
28+
"type": "package",
29+
"package": {
30+
"name": "pdezwart/php-amqp",
31+
"version": "dev-master",
32+
"source": {
33+
"url": "https://github.com/pdezwart/php-amqp",
34+
"type": "git",
35+
"reference": "origin/master"
36+
}
37+
}
38+
}]
2639
}

models/AsyncExecuteTask.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace bazilio\async\models;
3+
4+
/**
5+
* Class AsyncExecuteTask
6+
*/
7+
class AsyncExecuteTask extends AsyncTask
8+
{
9+
public $class;
10+
public $method;
11+
public $arguments = [];
12+
13+
public function rules()
14+
{
15+
return array(
16+
[['class', 'method', 'arguments'], 'required'],
17+
[['class', 'method'], 'string'],
18+
[['class'], 'validateClass'],
19+
[['method'], 'validateMethod'],
20+
[['arguments'], 'validateArguments'],
21+
);
22+
}
23+
24+
public function validateClass($attribute, $params)
25+
{
26+
if (!class_exists($this->$attribute)) {
27+
$this->addError($attribute, "Class {$this->$attribute} does not exist");
28+
}
29+
}
30+
31+
public function validateMethod($attribute, $params)
32+
{
33+
if (!isset(array_flip(get_class_methods($this->class))[$this->$attribute])) {
34+
$this->addError(
35+
$attribute,
36+
"Method {$this->$attribute} of class {$this->class} does not exist"
37+
);
38+
}
39+
}
40+
41+
public function validateArguments($attribute, $params) {
42+
if (!isset(array_flip(get_class_methods($this->class))[$this->method])) {
43+
$this->addError(
44+
$attribute,
45+
"Method {$this->method} of class {$this->class} does not exist"
46+
);
47+
}
48+
}
49+
50+
/**
51+
* Returns the list of attribute names of the model.
52+
* @return array list of attribute names.
53+
*/
54+
public function attributeNames()
55+
{
56+
return [
57+
'class',
58+
'method',
59+
'arguments'
60+
];
61+
}
62+
63+
static function nope($return)
64+
{
65+
return $return;
66+
}
67+
68+
public function execute()
69+
{
70+
return call_user_func_array(array($this->class, $this->method), $this->arguments);
71+
}
72+
}

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]
6+
enabled: [Asserts, UnitHelper, Mockery]

tests/unit/AmqpTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
<?php
2-
32
use bazilio\async\AsyncComponent;
3+
use bazilio\async\models\AsyncExecuteTask;
4+
use bazilio\async\models\AsyncTask;
45

5-
class TestTask extends \bazilio\async\models\AsyncTask
6+
class TestTask extends AsyncTask
67
{
78
public $id;
89

910
public function execute()
1011
{
1112
return $this->id;
1213
}
14+
}
1315

16+
class TestException extends \Exception
17+
{
18+
}
19+
20+
class BlackHole
21+
{
22+
public function run($param)
23+
{
24+
throw new TestException($param);
25+
}
1426
}
1527

1628
class AmqpTest extends \yii\codeception\TestCase
@@ -56,4 +68,30 @@ public function testLifeCycle()
5668

5769
$this->assertFalse($this->async->receiveTask(TestTask::$queueName));
5870
}
71+
72+
public function testAsyncExecuteTask()
73+
{
74+
$aTask = new AsyncExecuteTask();
75+
$aTask->setAttributes(
76+
[
77+
'class' => '\BlackHole',
78+
'method' => 'run',
79+
'arguments' => ['param' => 'through the space']
80+
]
81+
);
82+
83+
$this->async->sendTask($aTask);
84+
85+
$rTask = $this->async->receiveTask($aTask::$queueName);
86+
$this->assertInstanceOf('bazilio\async\models\AsyncExecuteTask', $rTask);
87+
88+
try {
89+
$rTask->execute();
90+
} catch (TestException $e) {
91+
$this->assertEquals($e->getMessage(), 'through the space');
92+
return true;
93+
}
94+
95+
$this->fail('BlackHole method wasn\'t called.');
96+
}
5997
}

tests/unit/UnitTester.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php //[STAMP] 6cd8435c53a77903754e35ca70c87c63
1+
<?php //[STAMP] 7090f4f20ecbb9880d52b8a3056e9b0b
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,6 +7,7 @@
77

88
use Codeception\Module\Asserts;
99
use Codeception\Module\UnitHelper;
10+
use Codeception\Module\Mockery;
1011

1112
/**
1213
* Inherited Methods

0 commit comments

Comments
 (0)