Skip to content

Commit 0e0caf1

Browse files
committed
0.0.2 redis provider
1 parent 7ed6c44 commit 0e0caf1

File tree

11 files changed

+289
-128
lines changed

11 files changed

+289
-128
lines changed

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ Provides translucent api for moving large tasks out of request response
55

66
#####Requirments:
77
- php >=5.4
8-
- [php-amqp](https://github.com/pdezwart/php-amqp)
8+
- Transports:
9+
- [php-amqp](https://github.com/pdezwart/php-amqp)
10+
- [yii2-redis](https://github.com/yiisoft/yii2-redis)
911

1012
#####Using with AMQP:
11-
######Installing:
13+
`php composer.phar require pdezwart/php-amqp:dev-master`
14+
1215
```php
1316
'components' => [
1417
'async' => [
1518
'class' => 'bazilio\async\AsyncComponent',
19+
'transportClass' => 'bazilio\async\transports\AsyncAmqpTransport',
1620
'transportConfig' => [
17-
'transportClass' => 'bazilio\async\transports\AsyncAmqpTransport'
1821
'host' => 'localhost',
1922
'login' => 'guest',
2023
'password' => 'guest',
@@ -24,9 +27,34 @@ Provides translucent api for moving large tasks out of request response
2427
]
2528
]
2629
```
27-
######Usage:
30+
31+
32+
#####Using with Redis:
33+
`php composer.phar require yiisoft/yii2-redis:*`
34+
35+
```php
36+
'components' => [
37+
'redis' => [
38+
'class' => 'yii\redis\Connection',
39+
'hostname' => 'localhost',
40+
'port' => 6379,
41+
'database' => 0,
42+
],
43+
'async' => [
44+
'class' => 'bazilio\async\AsyncComponent',
45+
'transportClass' => 'bazilio\async\transports\AsyncRedisTransport',
46+
'transportConfig' => [
47+
'connection' => 'redis',
48+
]
49+
]
50+
]
51+
```
52+
53+
54+
55+
#####Usage:
2856
For code exampless look into tests:
29-
- [AmqpTest](tests/unit/AmqpTest.php)
57+
- [BaseTestClass](tests/unit/BaseTestClass.php)
3058

3159

3260
######Runing tests:

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"minimum-stability": "alpha",
13-
"keywords": ["yii2", "async", "amqp", "rabbitmq", "tasks"],
13+
"keywords": ["yii2", "async", "amqp", "rabbitmq", "tasks", "redis"],
1414
"require": {
1515
"php": ">=5.4.0",
1616
"yiisoft/yii2": "*"
@@ -19,7 +19,8 @@
1919
"phpunit/phpunit": "3.7.*",
2020
"yiisoft/yii2-codeception": "*",
2121
"codeception/codeception": "*",
22-
"pdezwart/php-amqp": "dev-master"
22+
"pdezwart/php-amqp": "dev-master",
23+
"yiisoft/yii2-redis": "*"
2324
},
2425
"autoload": {
2526
"psr-4": {

models/AsyncExecuteTask.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public function validateClass($attribute, $params)
3030

3131
public function validateMethod($attribute, $params)
3232
{
33+
if (!class_exists($this->class)) {
34+
$this->addError($attribute, "Class {$this->$attribute} does not exist");
35+
return;
36+
}
37+
3338
if (!isset(array_flip(get_class_methods($this->class))[$this->$attribute])) {
3439
$this->addError(
3540
$attribute,
@@ -40,6 +45,11 @@ public function validateMethod($attribute, $params)
4045

4146
public function validateArguments($attribute, $params)
4247
{
48+
if (!class_exists($this->class)) {
49+
$this->addError($attribute, "Class {$this->$attribute} does not exist");
50+
return;
51+
}
52+
4353
if (!isset(array_flip(get_class_methods($this->class))[$this->method])) {
4454
$this->addError(
4555
$attribute,

tests/unit/AmqpTest.php

Lines changed: 2 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,7 @@
11
<?php
2-
use bazilio\async\AsyncComponent;
3-
use bazilio\async\models\AsyncExecuteTask;
4-
use bazilio\async\models\AsyncTask;
2+
namespace bazilio\async\tests\unit;
53

6-
class TestTask extends AsyncTask
7-
{
8-
public $id;
9-
10-
public function execute()
11-
{
12-
return $this->id;
13-
}
14-
}
15-
16-
class TestException extends \Exception
17-
{
18-
}
19-
20-
class BlackHole
21-
{
22-
public static function run($param)
23-
{
24-
throw new TestException($param);
25-
}
26-
}
27-
28-
class AmqpTest extends \yii\codeception\TestCase
4+
class AmqpTest extends BaseTestClass
295
{
306
public $appConfig = '@tests/unit/_config.amqp.php';
31-
/**
32-
* @var AsyncComponent
33-
*/
34-
protected $async;
35-
36-
public function setUp()
37-
{
38-
parent::setUp();
39-
40-
$this->async = Yii::$app->async;
41-
}
42-
43-
public function testPurge()
44-
{
45-
$task = new TestTask();
46-
$this->async->sendTask($task);
47-
48-
$this->assertInstanceOf('TestTask', $this->async->receiveTask(TestTask::$queueName));
49-
50-
$this->assertTrue($this->async->purge(TestTask::$queueName));
51-
$this->assertFalse($this->async->receiveTask(TestTask::$queueName));
52-
}
53-
54-
public function testLifeCycle()
55-
{
56-
$task = new TestTask();
57-
$task->id = uniqid();
58-
59-
$this->async->sendTask($task);
60-
61-
/** @var TestTask $rTask */
62-
$rTask = $this->async->receiveTask(TestTask::$queueName);
63-
$this->assertInstanceOf('TestTask', $rTask);
64-
65-
$this->assertEquals($task->id, $rTask->execute());
66-
67-
$this->assertTrue($this->async->acknowledgeTask($rTask));
68-
69-
$this->assertFalse($this->async->receiveTask(TestTask::$queueName));
70-
}
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-
}
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-
}
1247
}

tests/unit/BaseTestClass.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
namespace bazilio\async\tests\unit;
3+
4+
use bazilio\async\Exception;
5+
use bazilio\async\models\AsyncExecuteTask;
6+
use bazilio\async\models\AsyncTask;
7+
8+
class TestTask extends AsyncTask
9+
{
10+
public $id;
11+
12+
public function execute()
13+
{
14+
return $this->id;
15+
}
16+
}
17+
18+
class TestException extends \Exception
19+
{
20+
}
21+
22+
class BlackHole
23+
{
24+
public static function run($param)
25+
{
26+
throw new TestException($param);
27+
}
28+
}
29+
30+
class BaseTestClass extends \yii\codeception\TestCase
31+
{
32+
public $appConfig = '@tests/unit/_config.amqp.php';
33+
/**
34+
* @var \bazilio\async\AsyncComponent
35+
*/
36+
protected $async;
37+
38+
public function setUp()
39+
{
40+
parent::setUp();
41+
42+
$this->async = \Yii::$app->async;
43+
}
44+
45+
public function testPurge()
46+
{
47+
$task = new TestTask();
48+
$this->async->sendTask($task);
49+
50+
$this->assertEquals(TestTask::className(), get_class($this->async->receiveTask(TestTask::$queueName)));
51+
52+
$this->assertTrue($this->async->purge(TestTask::$queueName));
53+
$this->assertFalse($this->async->receiveTask(TestTask::$queueName));
54+
}
55+
56+
public function testLifeCycle()
57+
{
58+
$task = new TestTask();
59+
$task->id = uniqid();
60+
61+
$this->async->sendTask($task);
62+
63+
/** @var TestTask $rTask */
64+
$rTask = $this->async->receiveTask(TestTask::$queueName);
65+
$this->assertInstanceOf(TestTask::className(), $rTask);
66+
67+
$this->assertEquals($task->id, $rTask->execute());
68+
69+
$this->assertTrue($this->async->acknowledgeTask($rTask));
70+
71+
$this->assertFalse($this->async->receiveTask(TestTask::$queueName));
72+
}
73+
74+
public function testAsyncExecuteTask()
75+
{
76+
$aTask = new AsyncExecuteTask();
77+
$aTask->setAttributes(
78+
[
79+
'class' => 'bazilio\async\tests\unit\BlackHole',
80+
'method' => 'run',
81+
'arguments' => ['param' => 'through the space']
82+
]
83+
);
84+
85+
$this->async->sendTask($aTask);
86+
87+
$rTask = $this->async->receiveTask($aTask::$queueName);
88+
$this->assertInstanceOf('bazilio\async\models\AsyncExecuteTask', $rTask);
89+
90+
try {
91+
$rTask->execute();
92+
} catch (TestException $e) {
93+
$this->assertEquals($e->getMessage(), 'through the space');
94+
return true;
95+
}
96+
97+
$this->fail('BlackHole method wasn\'t called.');
98+
}
99+
100+
public function testArgumentsValidation()
101+
{
102+
$aTask = new AsyncExecuteTask();
103+
$aTask->setAttributes(
104+
[
105+
'class' => 'bazilio\async\tests\unit\BlackHole',
106+
'method' => 'run',
107+
'arguments' => ['fail' => 'through the space']
108+
]
109+
);
110+
111+
$this->assertFalse($aTask->validate());
112+
113+
$this->assertEquals(
114+
$aTask->errors['arguments'][0],
115+
"Method `run` missing required arguments: param"
116+
);
117+
118+
try {
119+
$this->async->sendTask($aTask);
120+
} catch (Exception $e) {
121+
return;
122+
}
123+
124+
$this->fail('Async doesn\'t reject invalid task.');
125+
}
126+
}

tests/unit/RedisTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace bazilio\async\tests\unit;
3+
4+
class RedisTest extends BaseTestClass
5+
{
6+
public $appConfig = '@tests/unit/_config.redis.php';
7+
}

tests/unit/UnitTester.php

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

tests/unit/_config.amqp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'async' => [
77
'class' => 'bazilio\async\AsyncComponent',
88
'transportConfig' => [
9-
'vhost' => 'test',
9+
'vhost' => '/',
1010
'exchangeName' => 'test'
1111
]
1212
]

0 commit comments

Comments
 (0)