Skip to content

Commit f4c2af5

Browse files
committed
Support for saving members attribute upon group create (and similar use cases) #98
1 parent d634f0f commit f4c2af5

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Attribute/MutableCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public function replace($value, Model &$object, ?Path $path = null)
5858
);
5959
}
6060

61-
$object->{$this->attribute}()->sync($existingObjects->all());
61+
$object->saved(function (Model $model) use ($existingObjects) {
62+
$model->{$this->attribute}()->sync($existingObjects->all());
63+
$model->load($this->attribute);
64+
});
65+
6266
}
6367

6468
public function patch($operation, $value, Model &$object, ?Path $path = null)

tests/GroupsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ArieTimmerman\Laravel\SCIMServer\Tests;
44

55
use ArieTimmerman\Laravel\SCIMServer\Tests\Model\Group;
6+
use ArieTimmerman\Laravel\SCIMServer\Tests\Model\User;
67
use Illuminate\Database\Schema\Blueprint;
78
use Illuminate\Support\Facades\Schema;
89

@@ -69,6 +70,37 @@ public function testCreate(){
6970

7071
}
7172

73+
public function testCreateWithMembers(){
74+
$response = $this->post('/scim/v2/Groups', [
75+
'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:Group'], // Required
76+
'urn:ietf:params:scim:schemas:core:2.0:Group' => [
77+
'displayName' => 'TestGroup',
78+
'members' => [
79+
[
80+
'value' => User::first()->id,
81+
]
82+
]
83+
]
84+
]);
85+
86+
$response->assertJsonStructure([
87+
'id',
88+
'urn:ietf:params:scim:schemas:core:2.0:Group' => [
89+
'displayName',
90+
'members' => [
91+
0 => [
92+
'value',
93+
'display'
94+
]
95+
]
96+
]
97+
]);
98+
99+
$this->assertEquals(User::first()->id, $response->json(['urn:ietf:params:scim:schemas:core:2.0:Group'])['members'][0]['value']);
100+
$this->assertNotNull(Group::find($response->json('id')));
101+
$this->assertNotNull(Group::where('displayName', 'TestGroup')->first());
102+
}
103+
72104
public function testBulk(){
73105
$response = $this->post('/scim/v2/Bulk', [
74106
'schemas' => ['urn:ietf:params:scim:api:messages:2.0:BulkRequest'], // Required

0 commit comments

Comments
 (0)