Skip to content

Commit

Permalink
[feature/dic] Fix test suite for dic-powered cron
Browse files Browse the repository at this point in the history
PHPBB3-10739
  • Loading branch information
igorw committed Apr 9, 2012
1 parent 0a5348a commit 3ebe89c
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 33 deletions.
4 changes: 2 additions & 2 deletions phpBB/includes/cron/task/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
exit;
}

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\TaggedContainerInterface;

/**
* Provides cron manager with tasks
Expand All @@ -28,7 +28,7 @@ class phpbb_cron_task_provider implements IteratorAggregate
{
private $container;

public function __construct(Container $container)
public function __construct(TaggedContainerInterface $container)
{
$this->container = $container;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/cron/ext/testext/cron/dummy_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class phpbb_ext_testext_cron_dummy_task extends phpbb_cron_task_base
{
public static $was_run = 0;

public function get_name()
{
return get_class($this);
}

public function run()
{
self::$was_run++;
Expand Down
5 changes: 5 additions & 0 deletions tests/cron/includes/cron/task/core/dummy_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class phpbb_cron_task_core_dummy_task extends phpbb_cron_task_base
{
public static $was_run = 0;

public function get_name()
{
return get_class($this);
}

public function run()
{
self::$was_run++;
Expand Down
5 changes: 5 additions & 0 deletions tests/cron/includes/cron/task/core/second_dummy_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class phpbb_cron_task_core_second_dummy_task extends phpbb_cron_task_base
{
public static $was_run = 0;

public function get_name()
{
return get_class($this);
}

public function run()
{
self::$was_run++;
Expand Down
32 changes: 16 additions & 16 deletions tests/cron/manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->manager = new phpbb_cron_manager(array(
'phpbb_cron_task_core_dummy_task',
'phpbb_cron_task_core_second_dummy_task',
'phpbb_ext_testext_cron_dummy_task',
$this->manager = $this->create_cron_manager(array(
new phpbb_cron_task_core_dummy_task(),
new phpbb_cron_task_core_second_dummy_task(),
new phpbb_ext_testext_cron_dummy_task(),
));
$this->task_name = 'phpbb_cron_task_core_dummy_task';
}
Expand All @@ -34,13 +34,6 @@ public function test_manager_finds_shipped_task_by_name()
$this->assertEquals($this->task_name, $task->get_name());
}

public function test_manager_instantiates_task_by_name()
{
$task = $this->manager->instantiate_task($this->task_name, array());
$this->assertInstanceOf('phpbb_cron_task_wrapper', $task);
$this->assertEquals($this->task_name, $task->get_name());
}

public function test_manager_finds_all_ready_tasks()
{
$tasks = $this->manager->find_all_ready_tasks();
Expand All @@ -55,10 +48,10 @@ public function test_manager_finds_one_ready_task()

public function test_manager_finds_only_ready_tasks()
{
$manager = new phpbb_cron_manager(array(
'phpbb_cron_task_core_simple_ready',
'phpbb_cron_task_core_simple_not_runnable',
'phpbb_cron_task_core_simple_should_not_run',
$manager = $this->create_cron_manager(array(
new phpbb_cron_task_core_simple_ready(),
new phpbb_cron_task_core_simple_not_runnable(),
new phpbb_cron_task_core_simple_should_not_run(),
));
$tasks = $manager->find_all_ready_tasks();
$task_names = $this->tasks_to_names($tasks);
Expand All @@ -70,8 +63,15 @@ private function tasks_to_names($tasks)
$names = array();
foreach ($tasks as $task)
{
$names[] = get_class($task->task);
$names[] = $task->get_name();
}
return $names;
}

private function create_cron_manager($tasks)
{
global $phpbb_root_path, $phpEx;

return new phpbb_cron_manager($tasks, $phpbb_root_path, $phpEx);
}
}
41 changes: 26 additions & 15 deletions tests/cron/task_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,48 @@
*
*/

require_once dirname(__FILE__) . '/../mock/extension_manager.php';
require_once dirname(__FILE__) . '/includes/cron/task/core/dummy_task.php';
require_once dirname(__FILE__) . '/includes/cron/task/core/second_dummy_task.php';
require_once dirname(__FILE__) . '/ext/testext/cron/dummy_task.php';

class phpbb_cron_task_provider_test extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->extension_manager = new phpbb_mock_extension_manager(
dirname(__FILE__) . '/',
array(
'testext' => array(
'ext_name' => 'testext',
'ext_active' => true,
'ext_path' => 'ext/testext/'
),
));
$this->provider = new phpbb_cron_task_provider($this->extension_manager);
$this->tasks = array(
'phpbb_cron_task_core_dummy_task',
'phpbb_cron_task_core_second_dummy_task',
'phpbb_ext_testext_cron_dummy_task',
);

$container = $this->getMock('Symfony\Component\DependencyInjection\TaggedContainerInterface');
$container
->expects($this->once())
->method('findTaggedServiceIds')
->will($this->returnValue(array_flip($this->tasks)));
$container
->expects($this->any())
->method('get')
->will($this->returnCallback(function ($name) {
return new $name;
}));

$this->provider = new phpbb_cron_task_provider($container);
}

public function test_manager_finds_shipped_tasks()
{
$tasks = array();
$task_names = array();
foreach ($this->provider as $task)
{
$tasks[] = $task;
$task_names[] = $task->get_name();
}
sort($tasks);
sort($task_names);

$this->assertEquals(array(
'phpbb_cron_task_core_dummy_task',
'phpbb_cron_task_core_second_dummy_task',
'phpbb_ext_testext_cron_dummy_task',
), $tasks);
), $task_names);
}
}
5 changes: 5 additions & 0 deletions tests/cron/tasks/simple_not_runnable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

class phpbb_cron_task_core_simple_not_runnable extends phpbb_cron_task_base
{
public function get_name()
{
return get_class($this);
}

public function run()
{
}
Expand Down
5 changes: 5 additions & 0 deletions tests/cron/tasks/simple_ready.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

class phpbb_cron_task_core_simple_ready extends phpbb_cron_task_base
{
public function get_name()
{
return get_class($this);
}

public function run()
{
}
Expand Down
5 changes: 5 additions & 0 deletions tests/cron/tasks/simple_should_not_run.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

class phpbb_cron_task_core_simple_should_not_run extends phpbb_cron_task_base
{
public function get_name()
{
return get_class($this);
}

public function run()
{
}
Expand Down

0 comments on commit 3ebe89c

Please sign in to comment.