Skip to content

Commit

Permalink
Fix validation on inherited properties
Browse files Browse the repository at this point in the history
Properties on parent classes were not being checked by the validator when ran on a child class
  • Loading branch information
cwmiller committed May 30, 2022
1 parent 1bb9d32 commit cc96699
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Ocip/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,28 @@ public static function validate($instance)
*/
private static function getGroups($instance)
{
$annotations = ReflectionUtils::getAnnotations($instance);
$groups = [];

if (!isset($annotations['Groups'])) {
throw new ConfigurationNotFoundException('No @Groups annotation found on object.');
$ref = new ReflectionClass($instance);

// A loop is needed to also get the Group annotations on parent classes
while ($ref != NULL) {
$annotations = ReflectionUtils::getAnnotations($ref);

if (!isset($annotations['Groups'])) {
throw new ConfigurationNotFoundException('No @Groups annotation found on type ' . $ref->getName() . '.');
}

$groups = array_merge($groups, self::fromJson($annotations['Groups']));

if ($ref->getParentClass() !== false) {
$ref = $ref->getParentClass();
} else {
$ref = NULL;
}
}

return self::fromJson($annotations['Groups']);
return $groups;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
namespace CWM\BroadWorksConnector\Tests;

use CWM\BroadWorksConnector\Ocip\Models\CallCenterSkillAgentList;
use CWM\BroadWorksConnector\Ocip\Models\CallForwardingService;
use CWM\BroadWorksConnector\Ocip\Models\FaxMessagingMenuKeysModifyEntry;
use CWM\BroadWorksConnector\Ocip\Models\GroupAccessDeviceGetListRequest;
use CWM\BroadWorksConnector\Ocip\Models\GroupCallCenterAddAgentListRequest;
use CWM\BroadWorksConnector\Ocip\Models\GroupCommunicationBarringAuthorizationCodeAddListRequest;
use CWM\BroadWorksConnector\Ocip\Models\GroupUserCallForwardingSettingsGetListRequest;
use CWM\BroadWorksConnector\Ocip\Models\LoginRequest14sp4;
use CWM\BroadWorksConnector\Ocip\Models\ResponsePagingControl;
use CWM\BroadWorksConnector\Ocip\Models\SearchCriteriaDeviceMACAddress;
use CWM\BroadWorksConnector\Ocip\Models\SearchCriteriaDeviceName;
use CWM\BroadWorksConnector\Ocip\Models\SearchMode;
use CWM\BroadWorksConnector\Ocip\Models\SortByExtension;
use CWM\BroadWorksConnector\Ocip\Models\SystemExtensionLengthModifyRequest;
use CWM\BroadWorksConnector\Ocip\Models\SystemGetRegistrationContactListRequest;
use CWM\BroadWorksConnector\Ocip\Models\UserAddRequest21;
Expand Down Expand Up @@ -56,6 +60,22 @@ public function testRequirementWithArrayMet()
$this->assertEquals(true, Validator::validate($request));
}

public function testRequirementInParentClass()
{
$this->expect('CWM\BroadWorksConnector\Ocip\Validation\FieldNotSetException');

$request = (new GroupUserCallForwardingSettingsGetListRequest())
->setServiceProviderId('SP')
->setGroupId('GRO')
->setCallForwardingService(CallForwardingService::CALL_FORWARDING_ALWAYS())
->setResponsePagingControl((new ResponsePagingControl())
->setResponseStartIndex(1)
->setResponsePageSize(10))
->setSortByExtension(new SortByExtension());

Validator::validate($request);
}

public function testChoiceNotMet()
{
$this->expect('CWM\BroadWorksConnector\Ocip\Validation\ChoiceNotSetException');
Expand Down

0 comments on commit cc96699

Please sign in to comment.