Skip to content

Commit 4e76df1

Browse files
Make AllowMockObjectsWithoutExpectations usable on test methods
1 parent 818e27b commit 4e76df1

14 files changed

+127
-22
lines changed

ChangeLog-12.5.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes of the PHPUnit 12.5 release series are documented in this fi
44

55
## [12.5.4] - 2025-MM-DD
66

7+
### Changed
8+
9+
* The `#[AllowMockObjectsWithoutExpectations]` attribute can now be used on the method level
10+
711
### Fixed
812

913
* [#6446](https://github.com/sebastianbergmann/phpunit/issues/6446): Test runner crashes with `Timer::start() has to be called before Timer::stop()`

src/Framework/Attributes/AllowMockObjectsWithoutExpectations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
1818
*/
19-
#[Attribute(Attribute::TARGET_CLASS)]
19+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
2020
final readonly class AllowMockObjectsWithoutExpectations
2121
{
2222
}

src/Framework/TestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,11 @@ private function stopErrorLogCapture(): void
24542454
$this->previousErrorLogTarget = false;
24552455
}
24562456

2457+
private function allowsMockObjectsWithoutExpectations(): bool
2458+
{
2459+
return MetadataRegistry::parser()->forClassAndMethod(static::class, $this->methodName)->isAllowMockObjectsWithoutExpectations()->isNotEmpty();
2460+
}
2461+
24572462
/**
24582463
* Returns a builder object to create test stubs using a fluent interface.
24592464
*
@@ -2542,11 +2547,6 @@ final protected static function createConfiguredStub(string $type, array $config
25422547
return $o;
25432548
}
25442549

2545-
private static function allowsMockObjectsWithoutExpectations(): bool
2546-
{
2547-
return MetadataRegistry::parser()->forClass(static::class)->isAllowMockObjectsWithoutExpectations()->isNotEmpty();
2548-
}
2549-
25502550
private static function generateReturnValuesForTestDoubles(): bool
25512551
{
25522552
return MetadataRegistry::parser()->forClass(static::class)->isDisableReturnValueGenerationForTestDoubles()->isEmpty();

src/Metadata/Metadata.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ public static function afterClass(int $priority): AfterClass
3737
return new AfterClass(self::METHOD_LEVEL, $priority);
3838
}
3939

40-
public static function allowMockObjectsWithoutExpectations(): AllowMockObjectsWithoutExpectations
40+
public static function allowMockObjectsWithoutExpectationsOnClass(): AllowMockObjectsWithoutExpectations
4141
{
4242
return new AllowMockObjectsWithoutExpectations(self::CLASS_LEVEL);
4343
}
4444

45+
public static function allowMockObjectsWithoutExpectationsOnMethod(): AllowMockObjectsWithoutExpectations
46+
{
47+
return new AllowMockObjectsWithoutExpectations(self::METHOD_LEVEL);
48+
}
49+
4550
public static function backupGlobalsOnClass(bool $enabled): BackupGlobals
4651
{
4752
return new BackupGlobals(self::CLASS_LEVEL, $enabled);

src/Metadata/Parser/AttributeParser.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function forClass(string $className): MetadataCollection
143143
case AllowMockObjectsWithoutExpectations::class:
144144
assert($attributeInstance instanceof AllowMockObjectsWithoutExpectations);
145145

146-
$result[] = Metadata::allowMockObjectsWithoutExpectations();
146+
$result[] = Metadata::allowMockObjectsWithoutExpectationsOnClass();
147147

148148
break;
149149

@@ -566,6 +566,13 @@ public function forMethod(string $className, string $methodName): MetadataCollec
566566

567567
break;
568568

569+
case AllowMockObjectsWithoutExpectations::class:
570+
assert($attributeInstance instanceof AllowMockObjectsWithoutExpectations);
571+
572+
$result[] = Metadata::allowMockObjectsWithoutExpectationsOnMethod();
573+
574+
break;
575+
569576
case BackupGlobals::class:
570577
assert($attributeInstance instanceof BackupGlobals);
571578

tests/_files/Metadata/Attribute/tests/AllowMockObjectsWithoutExpectationsTest.php renamed to tests/_files/Metadata/Attribute/tests/AllowMockObjectsWithoutExpectationsOnClassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHPUnit\Framework\TestCase;
1414

1515
#[AllowMockObjectsWithoutExpectations]
16-
final class AllowMockObjectsWithoutExpectationsTest extends TestCase
16+
final class AllowMockObjectsWithoutExpectationsOnClassTest extends TestCase
1717
{
1818
public function testOne(): void
1919
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\Metadata\Attribute;
11+
12+
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class AllowMockObjectsWithoutExpectationsOnMethodTest extends TestCase
16+
{
17+
#[AllowMockObjectsWithoutExpectations]
18+
public function testOne(): void
19+
{
20+
}
21+
}

tests/end-to-end/event/_files/CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest.php renamed to tests/end-to-end/event/_files/CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PHPUnit\TestFixture\Event\Example;
1515

1616
#[AllowMockObjectsWithoutExpectations]
17-
final class CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest extends TestCase
17+
final class CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest extends TestCase
1818
{
1919
public function testOne(): void
2020
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace TestFixture\PHPUnit\Event;
11+
12+
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
13+
use PHPUnit\Framework\TestCase;
14+
use PHPUnit\TestFixture\Event\Example;
15+
16+
final class CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnMethodTest extends TestCase
17+
{
18+
#[AllowMockObjectsWithoutExpectations]
19+
public function testOne(): void
20+
{
21+
$this->createMock(Example::class);
22+
23+
$this->assertTrue(true);
24+
}
25+
}

tests/end-to-end/event/mock-object-created-with-createMock-without-expectations-and-AllowMockObjectsWithoutExpectations.phpt renamed to tests/end-to-end/event/mock-object-created-with-createMock-without-expectations-and-AllowMockObjectsWithoutExpectations-on-test-class.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
--TEST--
2-
The right events are emitted in the right order for a test that creates a mock object using createMock() and does not configure expectations but AllowMockObjectsWithoutExpectations is used
2+
The right events are emitted in the right order for a test that creates a mock object using createMock() and does not configure expectations but AllowMockObjectsWithoutExpectations is used on test class
33
--FILE--
44
<?php declare(strict_types=1);
55
$_SERVER['argv'][] = '--do-not-cache-result';
66
$_SERVER['argv'][] = '--no-configuration';
77
$_SERVER['argv'][] = '--bootstrap';
88
$_SERVER['argv'][] = __DIR__ . '/_files/Example.php';
99
$_SERVER['argv'][] = '--debug';
10-
$_SERVER['argv'][] = __DIR__ . '/_files/CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest.php';
10+
$_SERVER['argv'][] = __DIR__ . '/_files/CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest.php';
1111

1212
require __DIR__ . '/../../bootstrap.php';
1313

@@ -21,13 +21,13 @@ Test Suite Loaded (1 test)
2121
Test Runner Started
2222
Test Suite Sorted
2323
Test Runner Execution Started (1 test)
24-
Test Suite Started (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest, 1 test)
25-
Test Preparation Started (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest::testOne)
26-
Test Prepared (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest::testOne)
24+
Test Suite Started (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest, 1 test)
25+
Test Preparation Started (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest::testOne)
26+
Test Prepared (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest::testOne)
2727
Mock Object Created (PHPUnit\TestFixture\Event\Example)
28-
Test Passed (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest::testOne)
29-
Test Finished (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest::testOne)
30-
Test Suite Finished (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsTest, 1 test)
28+
Test Passed (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest::testOne)
29+
Test Finished (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest::testOne)
30+
Test Suite Finished (TestFixture\PHPUnit\Event\CreateMockWithoutExpectationsAndAllowMockObjectsWithoutExpectationsOnClassTest, 1 test)
3131
Test Runner Execution Finished
3232
Test Runner Finished
3333
PHPUnit Finished (Shell Exit Code: 0)

0 commit comments

Comments
 (0)