Skip to content

Commit

Permalink
Update PHPUnit tests & after save hook based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
lschaefer-sugarcrm committed Apr 3, 2018
1 parent 7f9c771 commit c27d7f6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
22 changes: 16 additions & 6 deletions package/src/custom/Extension/modules/Schedulers/Ext/ScheduledTasks/StudentGradebookJob.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use Sugarcrm\Sugarcrm\custom\gradebook_fake\RecordManager;

class StudentGradebookJob implements RunnableSchedulerJob {
class StudentGradebookJob implements RunnableSchedulerJob
{

/**
* @var SchedulersJob
Expand All @@ -18,16 +19,15 @@ public function setJob(SchedulersJob $job)

/**
* This function defines the job that adds a new student to the GradebookFake app
* @param $job Information about the job. $job->data should be the id for the new Student (contact) record
* @param string $data The id for the new Student (contact) record
* @return bool true if a record was successfully created in the GradebookFake app
*/
public function run($data)
{
if (!empty($data))
{
if (!empty($data)) {
$bean = $this->getContactBean($data);

try{
try {
//Call the external GradebookFake app to create a new record in it
$rm = $this->getRecordManager();
$success = $rm->createStudentRecord($bean->email1, $bean->first_name, $bean->last_name);
Expand All @@ -50,10 +50,20 @@ public function run($data)
return false;
}

protected function getContactBean($id){
/**
* Get the Contact (Student) bean for the given id
* @param $id The id for which you want to retrieve a Contact (Student) bean
* @return null|SugarBean
*/
protected function getContactBean($id)
{
return BeanFactory::getBean('Contacts', $id);
}

/**
* Get the Record Manager for the GradebookFake app
* @return RecordManager The Record Manager for the GradebookFake app
*/
protected function getRecordManager()
{
return new RecordManager();
Expand Down
4 changes: 0 additions & 4 deletions package/src/custom/modules/Contacts/Students_Gradebook.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

require_once 'include/SugarQueue/SugarJobQueue.php';

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

/**
* Class Students_Gradebook
* Handles creating a job for the Sugar Job Queue that adds a new student to the GradebookFake app
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

namespace Sugarcrm\SugarcrmTestsUnit\modules\Schedulers\Ext\ScheduledTasks;

use Sugarcrm\SugarcrmTestsUnit\TestMockHelper;
use Sugarcrm\Sugarcrm\Util\Uuid;

require_once 'custom/Extension/modules/Schedulers/Ext/ScheduledTasks/StudentGradebookJob.php';
/**
* @coversDefaultClass \StudentGradebookJob
*/
class StudentGradebookJobTest extends \PHPUnit_Framework_TestCase
class StudentGradebookJobTest extends \PHPUnit\Framework\TestCase
{
/**
* @var a partial mock of a Student (Contact) with an id, first name, last name, and email
Expand All @@ -28,10 +31,9 @@ class StudentGradebookJobTest extends \PHPUnit_Framework_TestCase
*/
private $sgJob;

public function setup()
protected function setUp()
{
\SugarAutoLoader::load('../../../Extension/modules/Schedulers/Ext/ScheduledTasks/StudentGradebookJob.php');
parent::setup();
parent::setUp();

// Create a new Student (Contact) with an id, first name, last name, and email
$this->student = TestMockHelper::createPartialMock($this, '\\Contact', []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RecordManagerTest extends TestCase
{
/**
* Check that when valid params are sent to createStudentRecord, true is returned
* @covers createStudentRecord
* @covers ::createStudentRecord
*/
public function testRecordManagerValidParams(){
$rm = new RecordManager();
Expand All @@ -23,7 +23,7 @@ public function testRecordManagerValidParams(){
/**
* Check that when the email address forceerror@example.com is used as a param for createStudentRecord, an
* exception is thrown
* @covers createStudentRecord
* @covers ::createStudentRecord
*/
public function testRecordManagerForceException(){
$rm = new RecordManager();
Expand Down
10 changes: 4 additions & 6 deletions package/src/custom/tests/School/unit-php/modules/Contacts/Student_GradebookTest.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
use Sugarcrm\SugarcrmTestsUnit\TestMockHelper;
use Sugarcrm\Sugarcrm\Util\Uuid;

require_once 'include/SugarQueue/SugarJobQueue.php';
require_once 'custom/modules/Contacts/Students_Gradebook.php';

/**
* @coversDefaultClass \Students_Gradebook
*/
class Student_GradebookTest extends \PHPUnit_Framework_TestCase
class Student_GradebookTest extends \PHPUnit\Framework\TestCase
{

/**
Expand All @@ -33,8 +33,6 @@ class Student_GradebookTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
\SugarAutoLoader::load('../../../modules/Contacts/Students_Gradebook.php');

parent::setUp();
$GLOBALS['current_user'] = $this->createPartialMock('\\User', []);
$GLOBALS['current_user']->id = Uuid::uuid1();
Expand Down Expand Up @@ -80,17 +78,17 @@ public function testAddStudentToGradebook_CreatesTheJob()
*/
public function testAddStudentToGradebook_DoesNotCreateTheJob_NotAfterSaveEvent()
{
$this->sg->addStudentToGradebook($this->student, 'after_relationship_add', ['isUpdate' => false]);
$this->queue->expects($this->never())->method('submitJob');
$this->sg->addStudentToGradebook($this->student, 'after_relationship_add', ['isUpdate' => false]);
}

/**
* @covers ::addStudentToGradebook
*/
public function testAddStudentToGradebook_DoesNotCreateTheJob_IsUpdateEvent()
{
$this->sg->addStudentToGradebook($this->student, 'after_save', ['isUpdate' => true]);
$this->queue->expects($this->never())->method('submitJob');
$this->sg->addStudentToGradebook($this->student, 'after_save', ['isUpdate' => true]);
}

}
8 changes: 1 addition & 7 deletions package/src/custom/tests/School/unit-php/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,11 @@
<testsuites>
<testsuite name="all">
<directory>./</directory>
<directory>modules/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../../data/</directory>
<directory suffix=".php">../../src/</directory>
<directory suffix=".php">../../include/</directory>
<directory suffix=".php">../../clients/</directory>
<directory suffix=".php">../../modules/</directory>
<directory suffix=".php">../../api/</directory>
<directory suffix=".php">../../</directory>
</whitelist>
</filter>
<listeners>
Expand Down
4 changes: 2 additions & 2 deletions scripts/RunProfMPHPUnitTests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# This script runs the Sugar PHPUnit tests. It assumes the Sugar Docker stack has already been started and that
# SetupSugarPHPUnit tests has completed successfully.
# This script runs the PHPUnit tests for the Professor M Module Loadable Package. It assumes the Sugar Docker stack has
# already been started and that SetupSugarPHPUnit tests has completed successfully.


######################################################################
Expand Down

0 comments on commit c27d7f6

Please sign in to comment.