Skip to content

Commit

Permalink
Merge pull request #168 from devenbansod/add_tests_shell
Browse files Browse the repository at this point in the history
Add tests for shells
  • Loading branch information
nijel committed Jul 5, 2017
2 parents 83780eb + 1b43390 commit b75e3a6
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/Controller/GithubController.php
Expand Up @@ -431,7 +431,6 @@ protected function _getReportStatusFromIssueState($issueState)
*/
public function sync_issue_status()
{

if (!defined('CRON_DISPATCHER')) {
$flash_class = 'alert alert-error';
$this->Flash->default(
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/IncidentsController.php
Expand Up @@ -74,7 +74,7 @@ public function create()

public function json($id)
{
if (!$id) {
if (!isset($id) || !$id) {
throw new NotFoundException(__('Invalid Incident'));
}

Expand All @@ -97,7 +97,7 @@ public function json($id)

public function view($incidentId)
{
if (!$incidentId) {
if (!isset($incidentId) || !$incidentId) {
throw new NotFoundException(__('Invalid Incident'));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Controller/ReportsController.php
Expand Up @@ -65,7 +65,7 @@ public function index()

public function view($reportId)
{
if (!$reportId) {
if (!isset($reportId) || !$reportId) {
throw new NotFoundException(__('Invalid Report'));
}
$report = $this->Reports->findById($reportId)->toArray();
Expand All @@ -88,7 +88,7 @@ public function view($reportId)
// if there is an unread notification for this report, then mark it as read
$current_developer = TableRegistry::get('Developers')->
findById($this->request->session()->read('Developer.id'))->all()->first();
//$current_developer = Sanitize::clean($current_developer);

if ($current_developer) {
TableRegistry::get('Notifications')->deleteAll(
array('developer_id' => $current_developer['Developer']['id'],
Expand Down
1 change: 0 additions & 1 deletion src/Shell/StatsShell.php
Expand Up @@ -36,7 +36,6 @@ public function initialize()

public function main()
{
//Cache::clear(false);
foreach ($this->Incidents->filterTimes as $filter_string => $filter) {
foreach ($this->Incidents->summarizableFields as $field) {
$this->out('processing ' . $filter_string . ':' . $field);
Expand Down
13 changes: 12 additions & 1 deletion tests/Fixture/DevelopersFixture.php
Expand Up @@ -30,7 +30,7 @@ class DevelopersFixture extends TestFixture
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);*/
public $import = array('table' => 'developers', 'connection' => 'test');
public $import = array('table' => 'developers');
/**
* Records.
*
Expand All @@ -48,5 +48,16 @@ class DevelopersFixture extends TestFixture
'modified' => '2013-08-29 22:11:02',
'has_commit_access' => 1,
),
array(
'id' => 2,
'github_id' => 2,
'full_name' => 'Lorem ipsum dolor sit amet',
'email' => 'Lorem ipsum dolor sit amet',
'gravatar_id' => 'Lorem ipsum dolor sit amet',
'access_token' => 'Lorem ipsum dolor sit amet',
'created' => '2013-08-29 22:11:02',
'modified' => '2013-08-29 22:11:02',
'has_commit_access' => 0,
),
);
}
21 changes: 21 additions & 0 deletions tests/Fixture/IncidentsFixture.php
Expand Up @@ -101,5 +101,26 @@ class IncidentsFixture extends TestFixture
'created' => '2013-08-29 18:10:00',
'modified' => '2013-08-29 18:10:00',
),

array(
'id' => 5,
'error_name' => 'Lorem ipsum dolor sit amet',
'error_message' => 'Lorem ipsum dolor sit amet',
'pma_version' => 'Lorem ipsum dolor sit amet',
'php_version' => '5.3',
'browser' => 'Lorem ipsum dolor sit amet',
'user_os' => 'Lorem ipsum dolor sit amet',
'locale' => 'Lorem ipsum dolor sit amet',
'server_software' => 'Lorem ipsum dolor sit amet',
'stackhash' => 'hash3',
'configuration_storage' => 'Lorem ipsum dolor sit amet',
'script_name' => 'Lorem ipsum dolor sit amet',
'steps' => null,
'stacktrace' => '[{"context": ["test"]}]',
'full_report' => '{"pma_version": "1.2"}',
'report_id' => 5,
'created' => '2013-08-29 18:10:00',
'modified' => '2013-08-29 18:10:00',
),
);
}
4 changes: 2 additions & 2 deletions tests/Fixture/NotificationsFixture.php
Expand Up @@ -25,7 +25,7 @@ class NotificationsFixture extends TestFixture
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);*/
public $import = array('table' => 'notifications', 'connection' => 'test');
public $import = array('table' => 'notifications');
/**
* Records.
*
Expand All @@ -48,7 +48,7 @@ class NotificationsFixture extends TestFixture
),
array(
'id' => 3,
'developer_id' => 1,
'developer_id' => 2,
'report_id' => 4,
'created' => '2014-07-02 07:05:09',
'modified' => '2014-07-03 07:05:09',
Expand Down
21 changes: 17 additions & 4 deletions tests/Fixture/ReportsFixture.php
Expand Up @@ -7,15 +7,15 @@
class ReportsFixture extends TestFixture
{
public $connection = 'test';
public $import = array('table' => 'reports', 'connection' => 'test');
public $import = array('table' => 'reports');

public $records = array(
array(
'id' => 1,
'error_message' => 'Lorem ipsum dolor sit amet',
'error_name' => 'error2',
'pma_version' => '4.0',
'status' => 'new',
'status' => 'forwarded',
'location' => 'filename_1.php',
'linenumber' => 1,
'sourceforge_bug_id' => 1,
Expand All @@ -28,7 +28,7 @@ class ReportsFixture extends TestFixture
'error_message' => 'Lorem ipsum dolor sit amet',
'error_name' => 'error2',
'pma_version' => '4.0',
'status' => 'new',
'status' => 'forwarded',
'location' => 'filename_2.php',
'linenumber' => 2,
'sourceforge_bug_id' => 2,
Expand All @@ -41,13 +41,26 @@ class ReportsFixture extends TestFixture
'error_message' => 'Lorem ipsum dolor sit amet',
'error_name' => 'error1',
'pma_version' => '3.8',
'status' => 'new',
'status' => 'forwarded',
'location' => 'filename_3.js',
'linenumber' => 4,
'sourceforge_bug_id' => 4,
'related_to' => null,
'created' => '2013-08-28 21:47:17',
'modified' => '2013-08-28 21:47:17',
),
array(
'id' => 5,
'error_message' => 'Lorem ipsum dolor sit amet',
'error_name' => 'error1',
'pma_version' => '3.8',
'status' => 'new',
'location' => 'filename_4.js',
'linenumber' => 3,
'sourceforge_bug_id' => null,
'related_to' => null,
'created' => '2013-08-28 21:47:17',
'modified' => '2013-08-28 21:47:17',
),
);
}
30 changes: 15 additions & 15 deletions tests/TestCase/Controller/ReportsControllerTest.php
Expand Up @@ -4,6 +4,7 @@

use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestCase;
use Cake\Network\Exception\NotFoundException;

class ReportsControllerTest extends IntegrationTestCase
{
Expand All @@ -24,7 +25,7 @@ public function testIndex()
{
$this->get('/reports');
$this->assertEquals(array('3.8', '4.0'), $this->viewVariable('distinct_versions'));
$this->assertEquals(array('new'), $this->viewVariable('distinct_statuses'));
$this->assertEquals(array('forwarded', 'new'), $this->viewVariable('distinct_statuses'));
$this->assertEquals(array('error1', 'error2'),
$this->viewVariable('distinct_error_names'));
}
Expand Down Expand Up @@ -61,54 +62,53 @@ public function testView()

$this->get('/reports/view/3');
$this->assertResponseContains('Invalid Report');
// $this->assertResponseOk();
//$this->setExpectedException('NotFoundException');
// $this->get('/reports/view');
$this->assertResponseContains('/reports/view/3');
}

public function testDataTables()
{
$this->get('/reports/data_tables?sEcho=1&iDisplayLength=25');
$expected = array(
'iTotalRecords' => 3,
'iTotalDisplayRecords' => 3,
'iTotalRecords' => 4,
'iTotalDisplayRecords' => 4,
'sEcho' => 1,
'aaData' => array(
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'New', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'New', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='4'/>", 4, 'error1', 'Lorem ipsum dolor sit amet', 'filename_3.js', '3.8', 'New', 'js', '2'),
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'Forwarded', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='4'/>", 4, 'error1', 'Lorem ipsum dolor sit amet', 'filename_3.js', '3.8', 'Forwarded', 'js', '2'),
array("<input type='checkbox' name='reports[]' value='5'/>", 5, 'error1', 'Lorem ipsum dolor sit amet', 'filename_4.js', '3.8', 'New', 'js', '1')
),
);
$this->assertEquals($expected, json_decode($this->_response->body(), true));

$this->get('/reports/data_tables?sEcho=1&sSearch=error2&bSearchable_2=true&iSortCol_0=0&sSortDir_0=desc&bSortable_0=true&iSortingCols=2&iDisplayLength=25');
$expected = array(
'iTotalRecords' => 3,
'iTotalRecords' => 4,
'iTotalDisplayRecords' => 2,
'sEcho' => 1,
'aaData' => array(
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'New', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'New', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'Forwarded', 'js', '1'),
),
);
$result = json_decode($this->_response->body(), true);
$this->assertEquals($expected, $result);

$this->get('/reports/data_tables?sEcho=1&sSearch_1=1&iDisplayLength=25');
$expected = array(
'iTotalRecords' => 3,
'iTotalRecords' => 4,
'iTotalDisplayRecords' => 1,
'sEcho' => 1,
'aaData' => array(
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'New', 'js', '1'),
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'),
),
);
$result = json_decode($this->_response->body(), true);
$this->assertEquals($expected, $result);

$this->get('/reports/data_tables?sEcho=1&sSearch_1=error&iDisplayLength=25');
$expected = array(
'iTotalRecords' => 3,
'iTotalRecords' => 4,
'iTotalDisplayRecords' => 0,
'sEcho' => 1,
'aaData' => array(
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Model/Table/ReportsTableTest.php
Expand Up @@ -140,7 +140,7 @@ public function testGetRelatedByField()
),
array(
'php_version' => '5.3',
'count' => '3',
'count' => '4',
),
);
$this->assertEquals($expected, $result);
Expand Down
101 changes: 101 additions & 0 deletions tests/TestCase/Shell/CleanOldNotifsShellTest.php
@@ -0,0 +1,101 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */

/**
* Tests for Clean Old Notifications Shell.
*
* phpMyAdmin Error reporting server
* Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @see https://www.phpmyadmin.net/
*/

namespace App\Test\TestCase\Shell;

use App\Shell\CleanOldNotifsShell;
use Cake\TestSuite\TestCase;

/**
* App\Shell\CleanOldNotifsShell Test Case
*/
class CleanOldNotifsShellTest extends TestCase
{

/**
* ConsoleIo mock
*
* @var \Cake\Console\ConsoleIo|\PHPUnit_Framework_MockObject_MockObject
*/
public $io;

/**
* Test subject
*
* @var \App\Shell\CleanOldNotifsShell
*/
public $CleanOldNotifs;

public $fixtures = array(
'app.notifications',
'app.developers'
);

/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
$this->CleanOldNotifs = new CleanOldNotifsShell($this->io);
}

/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->CleanOldNotifs);

parent::tearDown();
}

/**
* Test main method
*
* @return void
*/
public function testMain()
{
// Call intialize method to load the models
$this->CleanOldNotifs->initialize();

$conditions = array(
'developer_id' => 1
);
$currentNotificationCount
= $this->CleanOldNotifs->Notifications->find('all')->where($conditions)->count();
$this->assertEquals(2, $currentNotificationCount);

// Run shell command
$this->CleanOldNotifs->main();

$newNotificationCount
= $this->CleanOldNotifs->Notifications->find('all')->where($conditions)->count();
$this->assertEquals(0, $newNotificationCount);

// Run shell command to generate zero notifications error
$this->CleanOldNotifs->main();
}
}

0 comments on commit b75e3a6

Please sign in to comment.