reload /
deployotron
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @file |
||
| 5 | * Unit tests for the Action class. |
||
| 6 | */ |
||
| 7 | |||
| 8 | require_once dirname(__DIR__) . '/deployotron.actions.inc'; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Log function for drush_log that passes messages to test class. |
||
| 12 | */ |
||
| 13 | function deployotron_log_proxy($entry = NULL) { |
||
| 14 | static $test_class = NULL; |
||
| 15 | if ($entry instanceof DeployotronActionCase) { |
||
| 16 | $test_class = $entry; |
||
| 17 | return; |
||
| 18 | } |
||
| 19 | elseif (isset($test_class)) { |
||
| 20 | $test_class->drushLog($entry); |
||
| 21 | } |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Deployotron testing class. |
||
| 26 | */ |
||
| 27 | class DeployotronActionCase extends Drush_UnitTestCase { |
||
| 28 | protected $log; |
||
| 29 | protected $prevLog; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Setup before each test. |
||
| 33 | */ |
||
| 34 | public function setup() { |
||
| 35 | $this->log = array(); |
||
| 36 | // Set ourselves as the log receiver. |
||
| 37 | deployotron_log_proxy($this); |
||
| 38 | // Save previous value. |
||
| 39 | $this->prevLog = drush_get_context('DRUSH_LOG_CALLBACK', NULL); |
||
| 40 | // Hook into drush_log. |
||
| 41 | drush_set_context('DRUSH_LOG_CALLBACK', 'deployotron_log_proxy'); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Teardown after each test. |
||
| 46 | */ |
||
| 47 | public function tearDown() { |
||
| 48 | // Reset drush_log for good meassure. |
||
| 49 | drush_set_context('DRUSH_LOG_CALLBACK', $this->prevLog); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Add a log entry from drush_log. |
||
| 54 | * |
||
| 55 | * Called by deployotron_log_proxy(). |
||
| 56 | */ |
||
| 57 | public function drushLog($entry) { |
||
| 58 | $this->log[] = $entry; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Empty the log. |
||
| 63 | */ |
||
| 64 | public function resetLog() { |
||
| 65 | $this->log[] = array(); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Assert that the log contains a message maching the regexp. |
||
| 70 | */ |
||
| 71 | public function assertLogMessageRegexp($regexp, $type = NULL) { |
||
| 72 | foreach ($this->log as $entry) { |
||
| 73 | if (preg_match($regexp, $entry['message']) && (!$type || $type == $entry['type'])) { |
||
| 74 | $this->assertTrue(TRUE); |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | $this->fail('Log message not found.'); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Assert a number of log entries. |
||
| 83 | */ |
||
| 84 | public function assertLogMessageCount($count) { |
||
| 85 | $this->assertEquals($count, count($this->log)); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Test incomplete actions. |
||
| 90 | */ |
||
| 91 | public function testIncomplete() { |
||
| 92 | $incomplete = new IncompleteAction('@none'); |
||
| 93 | $this->assertLogMessageRegexp('/Incomplete action, missing short description: /', 'warning'); |
||
| 94 | $this->assertLogMessageCount(1); |
||
| 95 | |||
| 96 | // Check the description is properly created from the default. |
||
| 97 | $description = $incomplete->getDescription(); |
||
| 98 | $this->assertEquals('Run the incompletely implemented action.', $description); |
||
| 99 | |||
| 100 | // Ensure that run() returns false. |
||
| 101 | $this->assertFalse($incomplete->run()); |
||
| 102 | |||
| 103 | // Ensure that it's disabled (should be as no short name was given). |
||
| 104 | $this->assertFalse($incomplete->enabled()); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | class IncompleteAction extends \Deployotron\Action { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 109 | } |
||
| 110 |