Skip to content

Commit

Permalink
try catch and tests added for class au
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Jul 10, 2024
1 parent 6081009 commit ad73275
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
10 changes: 10 additions & 0 deletions classes/local/au.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
namespace mod_cmi5launch\local;

// Include the errorover (error override) funcs.
require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php');

class au {

// Lowercase values are for saving to DB.
Expand All @@ -34,6 +37,13 @@ class au {
// Constructs AUs. Is fed array and where array key matches property, sets the property.
public function __construct($statement) {

// What can go wrong here? It could be that a statement is null
// or that the statement is not an array.
if (is_null($statement) || !is_array($statement)) {

throw new nullException('Statement to build AU is null or not an array.', 0);
}
// If it is an array, create the object.
foreach ($statement as $key => $value) {

$this->$key = ($value);
Expand Down
75 changes: 53 additions & 22 deletions cmi5PHP/tests/auTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace cmi5Test;

use mod_cmi5launch\local\nullException;
use PHPUnit\Framework\TestCase;
use mod_cmi5launch\local\au;

Expand All @@ -14,12 +15,12 @@
*/
class auTest extends TestCase
{
private $auProperties, $emptyStatement, $mockStatementValues;
private $auproperties, $emptystatement, $mockstatementvalues;

protected function setUp(): void
{
// All the properties in an AU object.
$this->auProperties = array(
$this->auproperties = array(
'id',
'attempt',
'url',
Expand Down Expand Up @@ -54,10 +55,10 @@ protected function setUp(): void
'userid'
);

$this->emptyStatement = array();
$this->emptystatement = array();

// Perhaps a good test would be to test the constructor with a statement that has all the properties set.
$this->mockStatementValues = array(
// A good test would be to test the constructor with a statement that has all the properties set.
$this->mockstatementvalues = array(
'id' => 'id',
'attempt' => 'attempt',
'url' => 'url',
Expand Down Expand Up @@ -105,16 +106,15 @@ protected function tearDown(): void
*/
public function testInstantiationWithEmpty()
{
$obj = new au($this->emptyStatement);
// Make an AU object with no values.
$obj = new au($this->emptystatement);

// Is an AU object?
// Assert its an AU object.
$this->assertInstanceOf(au::class, $obj);

//It is saying AU is not transversable
//Implementing traversable in AU is breaking the code,
//Make sure the AU object does not have any 'extra' properties, only the amount passed in
$expectedAmount = count($this->auProperties);
//could typecasting the object as an array help? dirty fix
// It is saying AU is not transversable. Implementing traversable in AU is breaking the code, typecast the object as array for dirty fix.
// Make sure the AU object does not have any 'extra' properties, only the amount passed in
$expectedAmount = count($this->auproperties);
$auArray = (array) $obj;
$this->assertCount($expectedAmount, $auArray, "AU has $expectedAmount properties");

Expand All @@ -124,27 +124,23 @@ public function testInstantiationWithEmpty()
$this->assertArrayHasKey($property, $auArray, "$property exists");
$this->assertNull($value, "$property empty");
}

}

/**
* Test of AU constructor class
* Should instantiate an AU object with values.
* @return void
*/

public function testInstantiationWithValues()
{
$obj = new au($this->mockStatementValues);
$obj = new au($this->mockstatementvalues);

// Is an AU object?
// Assert it's an AU object?
$this->assertInstanceOf(au::class, $obj);

//It is saying AU is not transversable
//Implementing traversable in AU is breaking the code,
//Make sure the AU object does not have any 'extra' properties, only the amount passed in
$expectedAmount = count($this->auProperties);
//could typecasting the object as an array help? dirty fix

// It is saying AU is not transversable. Implementing traversable in AU is breaking the code, typecast the object as array for dirty fix.
// Make sure the AU object does not have any 'extra' properties, only the amount passed in
$expectedAmount = count($this->auproperties);
$auArray = (array) $obj;
$this->assertCount($expectedAmount, $auArray, "AU has $expectedAmount properties");

Expand All @@ -155,4 +151,39 @@ public function testInstantiationWithValues()
$this->assertEquals($property, $value, "$value does not equal $property");
}
}

/**
* Test of AU constructor class exceptions. This one tests if statement is null.
* @return void
*/
public function testInstantiation_except_null()
{
// Null statement to send and trigger exception.
$nullstatement = null;

// Expected message
// Catch the exception.
$this->expectException(nullException::class);
$this->expectExceptionMessage("Statement to build AU is null or not an array." );

$obj = new au($nullstatement);

}

/**
* Test of AU constructor class exceptions. This one tests if statement passed in is not an array.
* @return void
*/
public function testInstantiation_except_nonarray()
{
// Null statement to send and trigger exception.
$nullstatement = "string";

// Catch the exception.
$this->expectException(nullException::class);
$this->expectExceptionMessage("Statement to build AU is null or not an array." );

$obj = new au($nullstatement);

}
}

0 comments on commit ad73275

Please sign in to comment.