File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed
tests/phpunit/Application/UseCases Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ class UseCaseError extends UseCaseException {
6060 public const CONTEXT_PATH = 'path ' ;
6161 public const CONTEXT_PROPERTY_ID = 'property_id ' ;
6262 public const CONTEXT_DENIAL_REASON = 'denial_reason ' ;
63+ public const CONTEXT_DENIAL_CONTEXT = 'denial_context ' ;
6364 public const CONTEXT_REDIRECT_TARGET = 'redirect_target ' ;
6465 public const CONTEXT_RESOURCE_TYPE = 'resource_type ' ;
6566 public const CONTEXT_SITE_ID = 'site_id ' ;
@@ -212,8 +213,13 @@ public static function newPatchResultModifiedReadOnlyValue( string $path ): self
212213 );
213214 }
214215
215- public static function newPermissionDenied ( string $ reason ): self {
216- return new self ( self ::PERMISSION_DENIED , 'Access to resource is denied ' , [ self ::CONTEXT_DENIAL_REASON => $ reason ] );
216+ public static function newPermissionDenied ( string $ reason , array $ denialContext = [] ): self {
217+ $ error = new self ( self ::PERMISSION_DENIED , 'Access to resource is denied ' , [ self ::CONTEXT_DENIAL_REASON => $ reason ] );
218+ if ( $ denialContext ) {
219+ $ error ->errorContext [self ::CONTEXT_DENIAL_CONTEXT ] = $ denialContext ;
220+ }
221+
222+ return $ error ;
217223 }
218224
219225 public static function newResourceNotFound ( string $ resourceType ): self {
Original file line number Diff line number Diff line change @@ -70,4 +70,26 @@ public function provideInvalidUseCaseErrorData(): Generator {
7070 ];
7171 }
7272
73+ /**
74+ * @dataProvider permissionDeniedContextProvider
75+ */
76+ public function testNewPermissionDenied ( UseCaseError $ error , array $ expectedContext ): void {
77+ $ this ->assertEquals ( $ expectedContext , $ error ->getErrorContext () );
78+ }
79+
80+ public function permissionDeniedContextProvider (): Generator {
81+ yield 'no additional context ' => [
82+ UseCaseError::newPermissionDenied ( 'some-reason ' ),
83+ [ UseCaseError::CONTEXT_DENIAL_REASON => 'some-reason ' ],
84+ ];
85+
86+ yield 'with additional context ' => [
87+ UseCaseError::newPermissionDenied ( 'some-other-reason ' , [ 'some ' => 'context ' ] ),
88+ [
89+ UseCaseError::CONTEXT_DENIAL_REASON => 'some-other-reason ' ,
90+ UseCaseError::CONTEXT_DENIAL_CONTEXT => [ 'some ' => 'context ' ],
91+ ],
92+ ];
93+ }
94+
7395}
You can’t perform that action at this time.
0 commit comments