Skip to content

Commit

Permalink
add ListResponse and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilos committed Nov 25, 2017
1 parent cf2cc1c commit d041c9b
Show file tree
Hide file tree
Showing 32 changed files with 397 additions and 161 deletions.
25 changes: 0 additions & 25 deletions src/Accessor/PropertyAccessor.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Builder/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setSchemasEndpointUrl($schemasEndpointUrl)
/**
* @param string $schemaId
*
* @return Schema
* @return \Tmilos\ScimSchema\Model\v1\Schema|\Tmilos\ScimSchema\Model\v2\Schema|Schema
*/
public function get($schemaId)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Builder/SchemaBuilderV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,11 @@ class SchemaBuilderV1 extends SchemaBuilder
),
);

public function __construct($schemasEndpointUrl = null)
{
parent::__construct($schemasEndpointUrl ?: 'http://localhost/'.ScimConstantsV1::ENDPOINT_SCHEMAS);
}

protected function getSchemaClass()
{
return Schema::class;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/ListResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getResources()
public function serializeObject()
{
$result = [
'schemas' => [Schema::LIST_RESPONSE],
'schemas' => $this->getSchemas(),
'totalResults' => $this->totalResults,
'Resources' => [],
];
Expand Down
8 changes: 5 additions & 3 deletions src/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ protected static function deserializeCommonAttributes(array $data)
if (isset($arr['externalId'])) {
$result->externalId = $data['externalId'];
}
foreach ($data['schemas'] as $schemaId) {
if ($schemaId != $result->getSchemaId()) {
$result->extensions[$schemaId] = isset($data[$schemaId]) ? $data[$schemaId] : null;
if (isset($data['schemas'])) {
foreach ($data['schemas'] as $schemaId) {
if ($schemaId != $result->getSchemaId()) {
$result->extensions[$schemaId] = isset($data[$schemaId]) ? $data[$schemaId] : null;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Model/ServiceProviderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function getAuthenticationSchemes()
public function serializeObject()
{
$result = parent::serializeObject();
unset($result['id']);

if ($this->documentationUri) {
$result['documentationUri'] = $this->documentationUri;
Expand Down
13 changes: 13 additions & 0 deletions src/Model/v1/ListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tmilos\ScimSchema\Model\v1;

use Tmilos\ScimSchema\ScimConstantsV1;

class ListResponse extends \Tmilos\ScimSchema\Model\ListResponse
{
public function getSchemaId()
{
return ScimConstantsV1::MESSAGE_LIST_RESPONSE;
}
}
19 changes: 19 additions & 0 deletions src/Model/v1/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,23 @@ public function getSchemaId()
{
return ScimConstantsV1::SCHEMA_SCHEMA;
}

public function serializeObject()
{
$parentValue = parent::serializeObject();

$result = [
'id' => $parentValue['id'],
'schema' => ScimConstantsV1::CORE,
];

unset($parentValue['id']);
unset($parentValue['schemas']);

foreach ($parentValue as $k=>$v) {
$result[$k] = $v;
}

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Model/v1/ServiceProviderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class ServiceProviderConfig extends \Tmilos\ScimSchema\Model\ServiceProviderConf
{
public function getSchemaId()
{
return ScimConstantsV1::SCHEMA_SERVICE_PROVIDER_CONFIG;
return ScimConstantsV1::CORE;
}
}
13 changes: 13 additions & 0 deletions src/Model/v2/ListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tmilos\ScimSchema\Model\v2;

use Tmilos\ScimSchema\ScimConstantsV2;

class ListResponse extends \Tmilos\ScimSchema\Model\ListResponse
{
public function getSchemaId()
{
return ScimConstantsV2::MESSAGE_LIST_RESPONSE;
}
}
9 changes: 5 additions & 4 deletions src/ScimConstantsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

abstract class ScimConstantsV1
{
const SCHEMA_USER = 'urn:scim:schemas:core:1.0';
const CORE = 'urn:scim:schemas:core:1.0';
const SCHEMA_USER = 'urn:scim:schemas:core:1.0:User';
const SCHEMA_ENTERPRISE_USER = 'urn:scim:schemas:extension:enterprise:1.0';
const SCHEMA_GROUP = 'urn:scim:schemas:core:1.0';
const SCHEMA_SERVICE_PROVIDER_CONFIG = 'urn:scim:schemas:core:1.0';
const SCHEMA_SCHEMA = 'urn:scim:schemas:core:1.0';
const SCHEMA_GROUP = 'urn:scim:schemas:core:1.0:Group';
const SCHEMA_SERVICE_PROVIDER_CONFIG = 'urn:scim:schemas:core:1.0:Service Provider Configuration';
const SCHEMA_SCHEMA = 'urn:scim:schemas:core:1.0:Schema';

const MESSAGE_LIST_RESPONSE = 'urn:scim:schemas:core:1.0';
const MESSAGE_SEARCH_REQUEST = 'urn:ietf:params:scim:api:messages:2.0:SearchRequest';
Expand Down
13 changes: 2 additions & 11 deletions tests/Builder/ResourceTypeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Tmilos\ScimSchema\Builder;

use Tests\Tmilos\ScimSchema\TestHelper;
use Tmilos\ScimSchema\Builder\ResourceTypeBuilder;
use Tmilos\ScimSchema\ScimConstants;

Expand All @@ -17,16 +18,6 @@ public function test_group()
$resourceTypes[] = $builder->build(ScimConstants::RESOURCE_TYPE_SCHEMA)->serializeObject();
$resourceTypes[] = $builder->build(ScimConstants::RESOURCE_TYPE_RESOURCE_TYPE)->serializeObject();

$this->assertEquals($this->getExpected(), $resourceTypes);
}

/**
* @return \stdClass
*/
private function getExpected()
{
$json = file_get_contents(__DIR__.'/resource_type.all.json');

return json_decode($json, true);
$this->assertEquals(TestHelper::getExpected('v2.resource_type.all.json'), $resourceTypes);
}
}
17 changes: 17 additions & 0 deletions tests/Builder/SchemaBuilderV1Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Tmilos\ScimSchema\Builder;

use Tests\Tmilos\ScimSchema\TestHelper;
use Tmilos\ScimSchema\Builder\SchemaBuilderV1;

class SchemaBuilderV1Test extends \PHPUnit_Framework_TestCase
{
public function test_group()
{
$builder = new SchemaBuilderV1();
$schema = $builder->getGroup()->serializeObject();

TestHelper::compare(TestHelper::getExpected('v1.schema.group.json'), $schema, $this);
}
}
97 changes: 7 additions & 90 deletions tests/Builder/SchemaBuilderV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Tmilos\ScimSchema\Builder;

use Tests\Tmilos\ScimSchema\TestHelper;
use Tmilos\ScimSchema\Builder\SchemaBuilderV2;

class SchemaBuilderV2Test extends \PHPUnit_Framework_TestCase
Expand All @@ -11,130 +12,46 @@ public function test_group()
$builder = new SchemaBuilderV2();
$schema = $builder->getGroup()->serializeObject();

$this->compare($this->getExpectedGroup(), $schema);
TestHelper::compare(TestHelper::getExpected('v2.schema.group.json'), $schema, $this);
}

public function test_user()
{
$builder = new SchemaBuilderV2();
$schema = $builder->getUser()->serializeObject();

$this->compare($this->getExpectedUser(), $schema);
TestHelper::compare(TestHelper::getExpected('v2.schema.user.json'), $schema, $this);
}

public function test_schema()
{
$builder = new SchemaBuilderV2();
$schema = $builder->getSchema()->serializeObject();

$this->compare($this->getExpectedSchema(), $schema);
TestHelper::compare(TestHelper::getExpected('v2.schema.schema.json'), $schema, $this);
}

public function test_resource_type()
{
$builder = new SchemaBuilderV2();
$schema = $builder->getResourceType()->serializeObject();

$this->compare($this->getExpectedResourceType(), $schema);
TestHelper::compare(TestHelper::getExpected('v2.schema.resource_type.json'), $schema, $this);
}

public function test_service_provider_config()
{
$builder = new SchemaBuilderV2();
$schema = $builder->getServiceProviderConfig()->serializeObject();

$this->compare($this->getExpectedServiceProviderConfig(), $schema);
TestHelper::compare(TestHelper::getExpected('v2.schema.service_provider_config.json'), $schema, $this);
}

public function test_enterprise_user()
{
$builder = new SchemaBuilderV2();
$schema = $builder->getEnterpriseUser()->serializeObject();

$this->compare($this->getExpectedEnterpriseUser(), $schema);
}

private function compare($expected, $actual)
{
$expectedArr = explode("\n", str_replace("\r", '', json_encode($expected, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)));
$actualArr = explode("\n", str_replace("\r", '', json_encode($actual, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)));

$minSize = min(count($expectedArr), count($actualArr));
for ($i=0; $i<$minSize; $i++) {
if ($expectedArr[$i] != $actualArr[$i]) {
$expectedArr[$i] = '!'.$expectedArr[$i];
$actualArr[$i] = '!'.$actualArr[$i];
print "\nMismatch at line $i:\n";
$start = $i - 10;
if ($start < 0) { $start = 0; }
$expectedBuffer = implode("\n", array_slice($expectedArr, $start, 20));
$actualBuffer = implode("\n", array_slice($actualArr, $start, 20));

print "Expected:\n$expectedBuffer\n\nbut got:\n$actualBuffer\n";

$this->fail();
}
}

$this->assertEquals($expected, $actual);
}

private function getExpected($filename)
{
$json = trim(file_get_contents(__DIR__.'/'.$filename));
$result = json_decode($json, true);
if (!$result) {
$this->fail("Error json decoding file '$filename': ".json_last_error_msg());
}

return $result;
}

/**
* @return \stdClass
*/
private function getExpectedGroup()
{
return $this->getExpected('schema.group.json');
}

/**
* @return \stdClass
*/
private function getExpectedUser()
{
return $this->getExpected('schema.user.json');
}

/**
* @return \stdClass
*/
private function getExpectedSchema()
{
return $this->getExpected('schema.schema.json');
}

/**
* @return \stdClass
*/
private function getExpectedResourceType()
{
return $this->getExpected('schema.resource_type.json');
}

/**
* @return \stdClass
*/
private function getExpectedServiceProviderConfig()
{
return $this->getExpected('schema.service_provider_config.json');
}

/**
* @return \stdClass
*/
private function getExpectedEnterpriseUser()
{
return $this->getExpected('schema.enterprise_user.json');
TestHelper::compare(TestHelper::getExpected('v2.schema.enterprise_user.json'), $schema, $this);
}
}
18 changes: 18 additions & 0 deletions tests/Builder/ServiceProviderConfigBuilderV1Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Tmilos\ScimSchema\Builder;

use Tests\Tmilos\ScimSchema\TestHelper;
use Tmilos\ScimSchema\Builder\ServiceProviderConfigBuilderV1;

class ServiceProviderConfigBuilderV1Test extends \PHPUnit_Framework_TestCase
{
public function test_builds_default_service_provider_config()
{
$builder = new ServiceProviderConfigBuilderV1();
$spc = $builder->buildServiceProviderConfig();
$arr = $spc->serializeObject();

$this->assertEquals(TestHelper::getExpected('v1.service_provider_config.default.json'), $arr);
}
}
Loading

0 comments on commit d041c9b

Please sign in to comment.