forked from PHPNuts/yii2-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests and fixed some major bugs
- Loading branch information
Kaupo Juhkam
committed
Dec 30, 2015
1 parent
e2871a6
commit cb378cd
Showing
5 changed files
with
193 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace yii\queue\tests; | ||
|
||
use yii\db\Connection; | ||
use yii\db\Query; | ||
|
||
class SqlQueueTest extends TestCase | ||
{ | ||
|
||
public function setUp() | ||
{ | ||
$this->mockApplication([ | ||
'components' => [ | ||
'queue' => [ | ||
'class'=> 'yii\queue\SqlQueue' | ||
], | ||
'db' => [ | ||
'class' => 'yii\db\Connection', | ||
'dsn' => 'sqlite::memory:', | ||
'charset' => 'utf8', | ||
] | ||
] | ||
]); | ||
} | ||
|
||
protected function pushJobToQueue($data = 'test', $delay = 0) | ||
{ | ||
$job=new TestJob([ | ||
'data'=>$data | ||
]); | ||
return $job->push($delay); | ||
} | ||
|
||
public function testJobCreation() | ||
{ | ||
$job=new TestJob([ | ||
'data'=>'test' | ||
]); | ||
$this->assertEquals('test', $job->data); | ||
} | ||
|
||
public function testJobInsertion() | ||
{ | ||
$result=$this->pushJobToQueue(); | ||
$resolvedJob=\Yii::$app->queue->pop(); | ||
$this->assertEquals($result, $resolvedJob['id']); | ||
} | ||
|
||
|
||
public function testJobRun() | ||
{ | ||
$this->pushJobToQueue(__FUNCTION__); | ||
$job=\Yii::$app->queue->pop(); | ||
$jobObject = call_user_func($job['serializer'][1], $job['object']); | ||
$this->assertEquals(__FUNCTION__, $jobObject->data); | ||
$this->assertEquals(__FUNCTION__, $jobObject->run()); | ||
} | ||
|
||
public function testJobDelete() | ||
{ | ||
$this->pushJobToQueue(__FUNCTION__); | ||
$job=\Yii::$app->queue->pop(); | ||
\Yii::$app->queue->delete($job); | ||
|
||
$this->assertFalse(\Yii::$app->queue->pop()); | ||
} | ||
|
||
public function testDifferentQueueWontPopJob() | ||
{ | ||
$this->pushJobToQueue(); | ||
$this->assertFalse(\Yii::$app->queue->pop('hurrdurrImasheep')); | ||
} | ||
|
||
public function testJobPurge() | ||
{ | ||
for ($i = 0; $i <= 10; $i++) { | ||
$this->pushJobToQueue(time()); | ||
} | ||
\Yii::$app->queue->purge('test'); | ||
|
||
$this->assertFalse(\Yii::$app->queue->pop()); | ||
} | ||
|
||
public function testJobRelease() | ||
{ | ||
$this->pushJobToQueue(__FUNCTION__); | ||
$job = \Yii::$app->queue->pop(); | ||
|
||
$this->assertEquals(time(), $job['run_at']); | ||
|
||
\Yii::$app->queue->release($job, 200); | ||
$this->assertFalse(\Yii::$app->queue->pop()); | ||
} | ||
|
||
public function testJobInFutureDoesNotPopNow() | ||
{ | ||
$this->pushJobToQueue(__FUNCTION__, 200); | ||
$this->assertFalse(\Yii::$app->queue->pop()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
namespace yii\queue\tests; | ||
|
||
use yii\helpers\ArrayHelper; | ||
|
||
abstract class TestCase extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Clean up after test. | ||
* By default the application created with [[mockApplication]] will be destroyed. | ||
*/ | ||
protected function tearDown() | ||
{ | ||
parent::tearDown(); | ||
$this->destroyApplication(); | ||
} | ||
/** | ||
* Populates Yii::$app with a new application | ||
* The application will be destroyed on tearDown() automatically. | ||
* @param array $config The application configuration, if needed | ||
* @param string $appClass name of the application class to create | ||
*/ | ||
protected function mockApplication($config = [], $appClass = '\yii\console\Application') | ||
{ | ||
new $appClass(ArrayHelper::merge([ | ||
'id' => 'testapp', | ||
'basePath' => __DIR__, | ||
'vendorPath' => $this->getVendorPath(), | ||
], $config)); | ||
} | ||
protected function getVendorPath() | ||
{ | ||
$vendor = dirname(dirname(__DIR__)) . '/vendor'; | ||
if (!is_dir($vendor)) { | ||
$vendor = dirname(dirname(dirname(dirname(__DIR__)))); | ||
} | ||
return $vendor; | ||
} | ||
/** | ||
* Destroys application in Yii::$app by setting it to null. | ||
*/ | ||
protected function destroyApplication() | ||
{ | ||
\Yii::$app = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace yii\queue\tests; | ||
|
||
use yii\queue\ActiveJob; | ||
|
||
class TestJob extends ActiveJob | ||
{ | ||
public $data; | ||
|
||
public function queueName() | ||
{ | ||
return 'test'; | ||
} | ||
|
||
public function run() | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
// ensure we get report on all possible php errors | ||
error_reporting(-1); | ||
define('YII_ENABLE_ERROR_HANDLER', false); | ||
define('YII_DEBUG', true); | ||
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__; | ||
$_SERVER['SCRIPT_FILENAME'] = __FILE__; | ||
require_once(__DIR__ . '/../vendor/autoload.php'); | ||
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); | ||
Yii::setAlias('@yii/queue', dirname(__DIR__)); |