Skip to content

Commit 92b0884

Browse files
authored
Merge pull request #288 from cakephp/3.next-phpunit11
add phpunit 11 support
2 parents 5c41fc4 + 34e163e commit 92b0884

File tree

5 files changed

+94
-109
lines changed

5 files changed

+94
-109
lines changed

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
},
3333
"require-dev": {
3434
"cakephp/authentication": "^3.0",
35-
"cakephp/bake": "^3.0",
36-
"cakephp/cakephp": "^5.0",
35+
"cakephp/bake": "dev-3.next",
36+
"cakephp/cakephp": "dev-5.next as 5.1.0",
3737
"cakephp/cakephp-codesniffer": "^5.0",
38-
"phpunit/phpunit": "^10.1.0"
38+
"phpunit/phpunit": "^10.5.5 || ^11.1.3"
3939
},
4040
"suggest": {
4141
"cakephp/http": "To use \"RequestPolicyInterface\" (Not needed separately if using full CakePHP framework).",
@@ -80,5 +80,7 @@
8080
"stan-setup": "phive install",
8181
"test": "phpunit",
8282
"test-coverage": "phpunit --coverage-clover=clover.xml"
83-
}
83+
},
84+
"minimum-stability": "dev",
85+
"prefer-stable": true
8486
}

tests/TestCase/AuthorizationServiceTest.php

Lines changed: 80 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818

1919
use Authorization\AuthorizationService;
2020
use Authorization\IdentityDecorator;
21+
use Authorization\IdentityInterface;
2122
use Authorization\Policy\BeforePolicyInterface;
2223
use Authorization\Policy\BeforeScopeInterface;
2324
use Authorization\Policy\Exception\MissingMethodException;
2425
use Authorization\Policy\MapResolver;
2526
use Authorization\Policy\OrmResolver;
2627
use Authorization\Policy\Result;
2728
use Authorization\Policy\ResultInterface;
29+
use Cake\Datasource\EntityInterface;
2830
use Cake\Datasource\QueryInterface;
2931
use Cake\TestSuite\TestCase;
32+
use PHPUnit\Framework\ExpectationFailedException;
3033
use TestApp\Model\Entity\Article;
3134
use TestApp\Model\Table\ArticlesTable;
3235
use TestApp\Policy\ArticlePolicy;
@@ -317,24 +320,22 @@ public function testApplyScopeAdditionalArguments()
317320
public function testBeforeFalse()
318321
{
319322
$entity = new Article();
320-
321-
$policy = $this->getMockBuilder(BeforePolicyInterface::class)
322-
->onlyMethods(['before'])
323-
->addMethods(['canAdd'])
324-
->getMock();
325-
326-
$policy->expects($this->once())
327-
->method('before')
328-
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
329-
->willReturn(false);
323+
$policy = new class implements BeforePolicyInterface {
324+
public function before($identity, $resource, $action): bool|ResultInterface|null
325+
{
326+
return false;
327+
}
328+
329+
public function canAdd($user, $entity)
330+
{
331+
throw new ExpectationFailedException('This method should not be called');
332+
}
333+
};
330334

331335
$resolver = new MapResolver([
332336
Article::class => $policy,
333337
]);
334338

335-
$policy->expects($this->never())
336-
->method('canAdd');
337-
338339
$service = new AuthorizationService($resolver);
339340

340341
$user = new IdentityDecorator($service, [
@@ -348,19 +349,17 @@ public function testBeforeFalse()
348349
public function testBeforeTrue()
349350
{
350351
$entity = new Article();
351-
352-
$policy = $this->getMockBuilder(BeforePolicyInterface::class)
353-
->onlyMethods(['before'])
354-
->addMethods(['canAdd'])
355-
->getMock();
356-
357-
$policy->expects($this->once())
358-
->method('before')
359-
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
360-
->willReturn(true);
361-
362-
$policy->expects($this->never())
363-
->method('canAdd');
352+
$policy = new class implements BeforePolicyInterface {
353+
public function before($identity, $resource, $action): bool|ResultInterface|null
354+
{
355+
return true;
356+
}
357+
358+
public function canAdd($user, $entity)
359+
{
360+
throw new ExpectationFailedException('This method should not be called');
361+
}
362+
};
364363

365364
$resolver = new MapResolver([
366365
Article::class => $policy,
@@ -379,21 +378,17 @@ public function testBeforeTrue()
379378
public function testBeforeNull()
380379
{
381380
$entity = new Article();
382-
383-
$policy = $this->getMockBuilder(BeforePolicyInterface::class)
384-
->onlyMethods(['before'])
385-
->addMethods(['canAdd'])
386-
->getMock();
387-
388-
$policy->expects($this->once())
389-
->method('before')
390-
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
391-
->willReturn(null);
392-
393-
$policy->expects($this->once())
394-
->method('canAdd')
395-
->with($this->isInstanceOf(IdentityDecorator::class), $entity)
396-
->willReturn(true);
381+
$policy = new class implements BeforePolicyInterface {
382+
public function before($identity, $resource, $action): bool|ResultInterface|null
383+
{
384+
return null;
385+
}
386+
387+
public function canAdd($user, $entity): bool
388+
{
389+
return true;
390+
}
391+
};
397392

398393
$resolver = new MapResolver([
399394
Article::class => $policy,
@@ -412,19 +407,17 @@ public function testBeforeNull()
412407
public function testBeforeResultTrue()
413408
{
414409
$entity = new Article();
415-
416-
$policy = $this->getMockBuilder(BeforePolicyInterface::class)
417-
->onlyMethods(['before'])
418-
->addMethods(['canAdd'])
419-
->getMock();
420-
421-
$policy->expects($this->once())
422-
->method('before')
423-
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
424-
->willReturn(new Result(true));
425-
426-
$policy->expects($this->never())
427-
->method('canAdd');
410+
$policy = new class implements BeforePolicyInterface {
411+
public function before($identity, $resource, $action): bool|ResultInterface|null
412+
{
413+
return new Result(true);
414+
}
415+
416+
public function canAdd($user, $entity)
417+
{
418+
throw new ExpectationFailedException('This method should not be called');
419+
}
420+
};
428421

429422
$resolver = new MapResolver([
430423
Article::class => $policy,
@@ -443,19 +436,17 @@ public function testBeforeResultTrue()
443436
public function testBeforeResultFalse()
444437
{
445438
$entity = new Article();
446-
447-
$policy = $this->getMockBuilder(BeforePolicyInterface::class)
448-
->onlyMethods(['before'])
449-
->addMethods(['canAdd'])
450-
->getMock();
451-
452-
$policy->expects($this->once())
453-
->method('before')
454-
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
455-
->willReturn(new Result(false));
456-
457-
$policy->expects($this->never())
458-
->method('canAdd');
439+
$policy = new class implements BeforePolicyInterface {
440+
public function before($identity, $resource, $action): bool|ResultInterface|null
441+
{
442+
return new Result(false);
443+
}
444+
445+
public function canAdd($user, $entity)
446+
{
447+
throw new ExpectationFailedException('This method should not be called');
448+
}
449+
};
459450

460451
$resolver = new MapResolver([
461452
Article::class => $policy,
@@ -474,19 +465,17 @@ public function testBeforeResultFalse()
474465
public function testBeforeScopeNonNull()
475466
{
476467
$entity = new Article();
477-
478-
$policy = $this->getMockBuilder(BeforeScopeInterface::class)
479-
->onlyMethods(['beforeScope'])
480-
->addMethods(['scopeIndex'])
481-
->getMock();
482-
483-
$policy->expects($this->once())
484-
->method('beforeScope')
485-
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'index')
486-
->willReturn('foo');
487-
488-
$policy->expects($this->never())
489-
->method('scopeIndex');
468+
$policy = new class implements BeforeScopeInterface {
469+
public function beforeScope(?IdentityInterface $identity, mixed $resource, string $action): mixed
470+
{
471+
return 'foo';
472+
}
473+
474+
public function scopeIndex(IdentityInterface $user, QueryInterface $query)
475+
{
476+
throw new ExpectationFailedException('This method should not be called');
477+
}
478+
};
490479

491480
$resolver = new MapResolver([
492481
Article::class => $policy,
@@ -505,21 +494,17 @@ public function testBeforeScopeNonNull()
505494
public function testBeforeScopeNull()
506495
{
507496
$entity = new Article();
508-
509-
$policy = $this->getMockBuilder(BeforeScopeInterface::class)
510-
->onlyMethods(['beforeScope'])
511-
->addMethods(['scopeIndex'])
512-
->getMock();
513-
514-
$policy->expects($this->once())
515-
->method('beforeScope')
516-
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'index')
517-
->willReturn(null);
518-
519-
$policy->expects($this->once())
520-
->method('scopeIndex')
521-
->with($this->isInstanceOf(IdentityDecorator::class), $entity)
522-
->willReturn('bar');
497+
$policy = new class implements BeforeScopeInterface {
498+
public function beforeScope(?IdentityInterface $identity, mixed $resource, string $action): mixed
499+
{
500+
return null;
501+
}
502+
503+
public function scopeIndex(IdentityInterface $user, EntityInterface $entity)
504+
{
505+
return 'bar';
506+
}
507+
};
523508

524509
$resolver = new MapResolver([
525510
Article::class => $policy,

tests/TestCase/IdentityDecoratorTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Authorization\IdentityDecorator;
99
use BadMethodCallException;
1010
use Cake\TestSuite\TestCase;
11+
use PHPUnit\Framework\Attributes\DataProvider;
1112
use stdClass;
1213
use TestApp\Model\Entity\Article;
1314

@@ -31,9 +32,7 @@ public static function constructorDataProvider()
3132
];
3233
}
3334

34-
/**
35-
* @dataProvider constructorDataProvider
36-
*/
35+
#[DataProvider('constructorDataProvider')]
3736
public function testConstructorAccepted($data)
3837
{
3938
$auth = $this->createMock(AuthorizationServiceInterface::class);
@@ -50,7 +49,7 @@ public function testCanDelegation()
5049
$auth->expects($this->once())
5150
->method('can')
5251
->with($identity, 'update', $resource)
53-
->will($this->returnValue(true));
52+
->willReturn(true);
5453
$this->assertTrue($identity->can('update', $resource));
5554
}
5655

@@ -63,7 +62,7 @@ public function testApplyScopeDelegation()
6362
$auth->expects($this->once())
6463
->method('applyScope')
6564
->with($identity, 'update', $resource)
66-
->will($this->returnValue(true));
65+
->willReturn(true);
6766
$this->assertTrue($identity->applyScope('update', $resource));
6867
}
6968

@@ -76,7 +75,7 @@ public function testApplyScopeAdditionalParams()
7675
$auth->expects($this->once())
7776
->method('applyScope')
7877
->with($identity, 'update', $resource, 'first argument', false)
79-
->will($this->returnValue(true));
78+
->willReturn(true);
8079
$this->assertTrue($identity->applyScope('update', $resource, 'first argument', false));
8180
}
8281

tests/TestCase/Middleware/AuthorizationMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testInvokeAuthorizationRequiredError()
6363
$service = $this->createMock(AuthorizationServiceInterface::class);
6464
$service->expects($this->once())
6565
->method('authorizationChecked')
66-
->will($this->returnValue(false));
66+
->willReturn(false);
6767

6868
$request = (new ServerRequest())->withAttribute('identity', ['id' => 1]);
6969
$handler = new TestRequestHandler();

tests/TestCase/Middleware/UnauthorizedHandler/RedirectHandlerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Cake\Core\Configure;
2222
use Cake\Http\ServerRequestFactory;
2323
use Cake\TestSuite\TestCase;
24+
use PHPUnit\Framework\Attributes\DataProvider;
2425

2526
class RedirectHandlerTest extends TestCase
2627
{
@@ -100,9 +101,7 @@ public static function httpMethodProvider()
100101
];
101102
}
102103

103-
/**
104-
* @dataProvider httpMethodProvider
105-
*/
104+
#[DataProvider('httpMethodProvider')]
106105
public function testHandleRedirectionIgnoreNonIdempotentMethods($method)
107106
{
108107
$handler = new RedirectHandler();

0 commit comments

Comments
 (0)