From 70fd4594d428e90c7c1ab8b57c300571c98a17b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Mon, 7 Mar 2022 17:44:43 +0100 Subject: [PATCH] Fix: Import --- tests/unit/Framework/AssertTest.php | 22 +-- .../Constraint/ClassHasAttributeTest.php | 18 ++- .../ClassHasStaticAttributeTest.php | 18 ++- .../Framework/Constraint/IsIdenticalTest.php | 8 +- .../Framework/Constraint/IsInstanceOfTest.php | 17 ++- .../Constraint/ObjectHasAttributeTest.php | 18 ++- tests/unit/Framework/ConstraintTest.php | 135 +++++++++++++----- .../Framework/MockObject/GeneratorTest.php | 2 +- .../Framework/MockObject/MockBuilderTest.php | 12 +- .../Framework/MockObject/MockObjectTest.php | 111 +++++++++----- tests/unit/Framework/TestCaseTest.php | 10 +- tests/unit/Framework/TestSuiteTest.php | 3 +- tests/unit/Runner/TestSuiteSorterTest.php | 8 +- tests/unit/Util/ConfigurationTest.php | 5 +- tests/unit/Util/TestClassTest.php | 111 ++++++++------ tests/unit/Util/XmlTest.php | 6 +- 16 files changed, 341 insertions(+), 163 deletions(-) diff --git a/tests/unit/Framework/AssertTest.php b/tests/unit/Framework/AssertTest.php index e4f95769d90..c0765da428c 100644 --- a/tests/unit/Framework/AssertTest.php +++ b/tests/unit/Framework/AssertTest.php @@ -216,20 +216,20 @@ public function testAssertArrayNotContainsOnlyIntegers(): void public function testAssertArrayContainsOnlyStdClass(): void { - $this->assertContainsOnly('StdClass', [new stdClass]); + $this->assertContainsOnly(stdClass::class, [new stdClass]); $this->expectException(AssertionFailedError::class); - $this->assertContainsOnly('StdClass', ['StdClass']); + $this->assertContainsOnly(stdClass::class, [stdClass::class]); } public function testAssertArrayNotContainsOnlyStdClass(): void { - $this->assertNotContainsOnly('StdClass', ['StdClass']); + $this->assertNotContainsOnly(stdClass::class, [stdClass::class]); $this->expectException(AssertionFailedError::class); - $this->assertNotContainsOnly('StdClass', [new stdClass]); + $this->assertNotContainsOnly(stdClass::class, [new stdClass]); } public function equalProvider(): array @@ -993,7 +993,7 @@ public function testAssertClassHasAttributeThrowsExceptionIfClassDoesNotExist(): { $this->expectException(Exception::class); - $this->assertClassHasAttribute('attribute', 'ClassThatDoesNotExist'); + $this->assertClassHasAttribute('attribute', ClassThatDoesNotExist::class); } public function testAssertClassNotHasAttributeThrowsExceptionIfAttributeNameIsNotValid(): void @@ -1007,7 +1007,7 @@ public function testAssertClassNotHasAttributeThrowsExceptionIfClassDoesNotExist { $this->expectException(Exception::class); - $this->assertClassNotHasAttribute('attribute', 'ClassThatDoesNotExist'); + $this->assertClassNotHasAttribute('attribute', ClassThatDoesNotExist::class); } public function testAssertClassHasStaticAttributeThrowsExceptionIfAttributeNameIsNotValid(): void @@ -1021,7 +1021,7 @@ public function testAssertClassHasStaticAttributeThrowsExceptionIfClassDoesNotEx { $this->expectException(Exception::class); - $this->assertClassHasStaticAttribute('attribute', 'ClassThatDoesNotExist'); + $this->assertClassHasStaticAttribute('attribute', ClassThatDoesNotExist::class); } public function testAssertClassNotHasStaticAttributeThrowsExceptionIfAttributeNameIsNotValid(): void @@ -1035,7 +1035,7 @@ public function testAssertClassNotHasStaticAttributeThrowsExceptionIfClassDoesNo { $this->expectException(Exception::class); - $this->assertClassNotHasStaticAttribute('attribute', 'ClassThatDoesNotExist'); + $this->assertClassNotHasStaticAttribute('attribute', ClassThatDoesNotExist::class); } public function testAssertObjectHasAttributeThrowsException2(): void @@ -1321,7 +1321,7 @@ public function testAssertThatIdenticalTo(): void public function testAssertThatIsInstanceOf(): void { - $this->assertThat(new stdClass, $this->isInstanceOf('StdClass')); + $this->assertThat(new stdClass, $this->isInstanceOf(stdClass::class)); } public function testAssertThatIsType(): void @@ -1828,7 +1828,7 @@ public function testAssertInstanceOfThrowsExceptionIfTypeDoesNotExist(): void { $this->expectException(Exception::class); - $this->assertInstanceOf('ClassThatDoesNotExist', new stdClass); + $this->assertInstanceOf(ClassThatDoesNotExist::class, new stdClass); } public function testAssertInstanceOf(): void @@ -1844,7 +1844,7 @@ public function testAssertNotInstanceOfThrowsExceptionIfTypeDoesNotExist(): void { $this->expectException(Exception::class); - $this->assertNotInstanceOf('ClassThatDoesNotExist', new stdClass); + $this->assertNotInstanceOf(ClassThatDoesNotExist::class, new stdClass); } public function testAssertNotInstanceOf(): void diff --git a/tests/unit/Framework/Constraint/ClassHasAttributeTest.php b/tests/unit/Framework/Constraint/ClassHasAttributeTest.php index 9c356fdc47b..a011d290b8f 100644 --- a/tests/unit/Framework/Constraint/ClassHasAttributeTest.php +++ b/tests/unit/Framework/Constraint/ClassHasAttributeTest.php @@ -34,11 +34,14 @@ public function testConstraintClassHasAttribute(): void $constraint->evaluate(stdClass::class); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that class "stdClass" has attribute "privateAttribute". + sprintf( + <<<'EOF' +Failed asserting that class "%s" has attribute "privateAttribute". EOF - , + , + stdClass::class + ), TestFailure::exceptionToString($e) ); @@ -58,12 +61,15 @@ public function testConstraintClassHasAttribute2(): void $constraint->evaluate(stdClass::class, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that class "stdClass" has attribute "privateAttribute". +Failed asserting that class "%s" has attribute "privateAttribute". EOF - , + , + stdClass::class + ), TestFailure::exceptionToString($e) ); diff --git a/tests/unit/Framework/Constraint/ClassHasStaticAttributeTest.php b/tests/unit/Framework/Constraint/ClassHasStaticAttributeTest.php index fea349a9351..1d0192e5d3b 100644 --- a/tests/unit/Framework/Constraint/ClassHasStaticAttributeTest.php +++ b/tests/unit/Framework/Constraint/ClassHasStaticAttributeTest.php @@ -32,11 +32,14 @@ public function testConstraintClassHasStaticAttribute(): void $constraint->evaluate(stdClass::class); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that class "stdClass" has static attribute "privateStaticAttribute". + sprintf( + <<<'EOF' +Failed asserting that class "%s" has static attribute "privateStaticAttribute". EOF - , + , + stdClass::class + ), TestFailure::exceptionToString($e) ); @@ -54,12 +57,15 @@ public function testConstraintClassHasStaticAttribute2(): void $constraint->evaluate(stdClass::class, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that class "stdClass" has static attribute "foo". +Failed asserting that class "%s" has static attribute "foo". EOF - , + , + stdClass::class + ), TestFailure::exceptionToString($e) ); diff --git a/tests/unit/Framework/Constraint/IsIdenticalTest.php b/tests/unit/Framework/Constraint/IsIdenticalTest.php index 5ee4cddd857..f5e132cf899 100644 --- a/tests/unit/Framework/Constraint/IsIdenticalTest.php +++ b/tests/unit/Framework/Constraint/IsIdenticalTest.php @@ -27,7 +27,13 @@ public function testConstraintIsIdentical(): void $this->assertFalse($constraint->evaluate($b, '', true)); $this->assertTrue($constraint->evaluate($a, '', true)); - $this->assertEquals('is identical to an object of class "stdClass"', $constraint->toString()); + $this->assertEquals( + sprintf( + 'is identical to an object of class "%s"', + stdClass::class + ), + $constraint->toString() + ); $this->assertCount(1, $constraint); try { diff --git a/tests/unit/Framework/Constraint/IsInstanceOfTest.php b/tests/unit/Framework/Constraint/IsInstanceOfTest.php index a0d728ec697..bde67d1afe2 100644 --- a/tests/unit/Framework/Constraint/IsInstanceOfTest.php +++ b/tests/unit/Framework/Constraint/IsInstanceOfTest.php @@ -31,14 +31,18 @@ public function testConstraintFailsOnString(): void $constraint = new IsInstanceOf(stdClass::class); try { - $constraint->evaluate('stdClass'); + $constraint->evaluate(stdClass::class); } catch (ExpectationFailedException $e) { $this->assertSame( - <<<'EOT' -Failed asserting that 'stdClass' is an instance of class "stdClass". + sprintf( + <<<'EOT' +Failed asserting that '%s' is an instance of class "%s". EOT - , + , + stdClass::class, + stdClass::class + ), TestFailure::exceptionToString($e) ); } @@ -51,7 +55,10 @@ public function testCronstraintsThrowsReflectionException(): void $constraint = new IsInstanceOf(NotExistingClass::class); $this->assertSame( - 'is instance of class "PHPUnit\Framework\Constraint\NotExistingClass"', + sprintf( + 'is instance of class "%s"', + NotExistingClass::class + ), $constraint->toString() ); } diff --git a/tests/unit/Framework/Constraint/ObjectHasAttributeTest.php b/tests/unit/Framework/Constraint/ObjectHasAttributeTest.php index 9750b5621ec..28eba4516aa 100644 --- a/tests/unit/Framework/Constraint/ObjectHasAttributeTest.php +++ b/tests/unit/Framework/Constraint/ObjectHasAttributeTest.php @@ -32,11 +32,14 @@ public function testConstraintObjectHasAttribute(): void $constraint->evaluate(new stdClass); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that object of class "stdClass" has attribute "privateAttribute". + sprintf( + <<<'EOF' +Failed asserting that object of class "%s" has attribute "privateAttribute". EOF - , + , + stdClass::class + ), TestFailure::exceptionToString($e) ); @@ -54,12 +57,15 @@ public function testConstraintObjectHasAttribute2(): void $constraint->evaluate(new stdClass, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that object of class "stdClass" has attribute "privateAttribute". +Failed asserting that object of class "%s" has attribute "privateAttribute". EOF - , + , + stdClass::class + ), TestFailure::exceptionToString($e) ); diff --git a/tests/unit/Framework/ConstraintTest.php b/tests/unit/Framework/ConstraintTest.php index 755c8a1d33a..39e266b56c3 100644 --- a/tests/unit/Framework/ConstraintTest.php +++ b/tests/unit/Framework/ConstraintTest.php @@ -395,7 +395,13 @@ public function testConstraintIsNotIdentical(): void $this->assertTrue($constraint->evaluate($b, '', true)); $this->assertFalse($constraint->evaluate($a, '', true)); - $this->assertEquals('is not identical to an object of class "stdClass"', $constraint->toString()); + $this->assertEquals( + sprintf( + 'is not identical to an object of class "%s"', + stdClass::class + ), + $constraint->toString() + ); $this->assertCount(1, $constraint); try { @@ -474,23 +480,39 @@ public function testConstraintIsInstanceOf(): void $this->assertFalse($constraint->evaluate(new stdClass, '', true)); $this->assertTrue($constraint->evaluate(new \Exception, '', true)); - $this->assertEquals('is instance of class "Exception"', $constraint->toString()); + $this->assertEquals( + sprintf( + 'is instance of class "%s"', + \Exception::class + ), + $constraint->toString() + ); $this->assertCount(1, $constraint); $interfaceConstraint = Assert::isInstanceOf(Countable::class); $this->assertFalse($interfaceConstraint->evaluate(new stdClass, '', true)); $this->assertTrue($interfaceConstraint->evaluate(new ArrayObject, '', true)); - $this->assertEquals('is instance of interface "Countable"', $interfaceConstraint->toString()); + $this->assertEquals( + sprintf( + 'is instance of interface "%s"', + Countable::class + ), + $interfaceConstraint->toString() + ); try { $constraint->evaluate(new stdClass); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that stdClass Object () is an instance of class "Exception". + sprintf( + <<<'EOF' +Failed asserting that %s Object () is an instance of class "%s". EOF - , + , + stdClass::class, + \Exception::class + ), TestFailure::exceptionToString($e) ); @@ -508,12 +530,16 @@ public function testConstraintIsInstanceOf2(): void $constraint->evaluate(new stdClass, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that stdClass Object () is an instance of class "Exception". +Failed asserting that %s Object () is an instance of class "%s". EOF - , + , + stdClass::class, + \Exception::class + ), TestFailure::exceptionToString($e) ); @@ -531,18 +557,28 @@ public function testConstraintIsNotInstanceOf(): void $this->assertFalse($constraint->evaluate(new stdClass, '', true)); $this->assertTrue($constraint->evaluate(new Exception, '', true)); - $this->assertEquals('is not instance of class "stdClass"', $constraint->toString()); + $this->assertEquals( + sprintf( + 'is not instance of class "%s"', + stdClass::class + ), + $constraint->toString() + ); $this->assertCount(1, $constraint); try { $constraint->evaluate(new stdClass); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that stdClass Object () is not an instance of class "stdClass". + sprintf( + <<<'EOF' +Failed asserting that %s Object () is not an instance of class "%s". EOF - , + , + stdClass::class, + stdClass::class + ), TestFailure::exceptionToString($e) ); @@ -562,12 +598,16 @@ public function testConstraintIsNotInstanceOf2(): void $constraint->evaluate(new stdClass, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that stdClass Object () is not an instance of class "stdClass". +Failed asserting that %s Object () is not an instance of class "%s". EOF - , + , + stdClass::class, + stdClass::class + ), TestFailure::exceptionToString($e) ); @@ -858,11 +898,14 @@ public function testConstraintClassNotHasAttribute(): void $constraint->evaluate(ClassWithNonPublicAttributes::class); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". + sprintf( + <<<'EOF' +Failed asserting that class "%s" does not have attribute "privateAttribute". EOF - , + , + ClassWithNonPublicAttributes::class + ), TestFailure::exceptionToString($e) ); @@ -882,12 +925,15 @@ public function testConstraintClassNotHasAttribute2(): void $constraint->evaluate(ClassWithNonPublicAttributes::class, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". +Failed asserting that class "%s" does not have attribute "privateAttribute". EOF - , + , + ClassWithNonPublicAttributes::class + ), TestFailure::exceptionToString($e) ); @@ -912,11 +958,14 @@ public function testConstraintClassNotHasStaticAttribute(): void $constraint->evaluate(ClassWithNonPublicAttributes::class); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute". + sprintf( + <<<'EOF' +Failed asserting that class "%s" does not have static attribute "privateStaticAttribute". EOF - , + , + ClassWithNonPublicAttributes::class + ), TestFailure::exceptionToString($e) ); @@ -936,12 +985,15 @@ public function testConstraintClassNotHasStaticAttribute2(): void $constraint->evaluate(ClassWithNonPublicAttributes::class, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute". +Failed asserting that class "%s" does not have static attribute "privateStaticAttribute". EOF - , + , + ClassWithNonPublicAttributes::class + ), TestFailure::exceptionToString($e) ); @@ -966,11 +1018,14 @@ public function testConstraintObjectNotHasAttribute(): void $constraint->evaluate(new ClassWithNonPublicAttributes); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' -Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". + sprintf( + <<<'EOF' +Failed asserting that object of class "%s" does not have attribute "privateAttribute". EOF - , + , + ClassWithNonPublicAttributes::class + ), TestFailure::exceptionToString($e) ); @@ -990,12 +1045,15 @@ public function testConstraintObjectNotHasAttribute2(): void $constraint->evaluate(new ClassWithNonPublicAttributes, 'custom message'); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<<'EOF' + sprintf( + <<<'EOF' custom message -Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". +Failed asserting that object of class "%s" does not have attribute "privateAttribute". EOF - , + , + ClassWithNonPublicAttributes::class + ), TestFailure::exceptionToString($e) ); @@ -1419,12 +1477,15 @@ public function testConstraintException(): void $constraint->evaluate($exception); } catch (ExpectationFailedException $e) { $this->assertEquals( - <<generator->getMock(SingletonClass::class, ['doSomething'], [], '', false); - $this->assertInstanceOf('SingletonClass', $mock); + $this->assertInstanceOf(SingletonClass::class, $mock); } public function testExceptionIsRaisedForMutuallyExclusiveOptions(): void diff --git a/tests/unit/Framework/MockObject/MockBuilderTest.php b/tests/unit/Framework/MockObject/MockBuilderTest.php index 9c721ac3b65..2b7f0374b7d 100644 --- a/tests/unit/Framework/MockObject/MockBuilderTest.php +++ b/tests/unit/Framework/MockObject/MockBuilderTest.php @@ -65,7 +65,10 @@ public function testSetMethodsAllowsNonExistentMethodNames(): void public function testOnlyMethodsWithNonExistentMethodNames(): void { $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Trying to set mock method "mockableMethodWithCrazyName" with onlyMethods, but it does not exist in class "Mockable". Use addMethods() for methods that don\'t exist in the class.'); + $this->expectExceptionMessage(sprintf( + 'Trying to set mock method "mockableMethodWithCrazyName" with onlyMethods, but it does not exist in class "%s". Use addMethods() for methods that don\'t exist in the class.', + Mockable::class + )); $this->getMockBuilder(Mockable::class) ->onlyMethods(['mockableMethodWithCrazyName']) @@ -94,7 +97,10 @@ public function testOnlyMethodsWithEmptyArray(): void public function testAddMethodsWithNonExistentMethodNames(): void { $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Trying to set mock method "mockableMethod" with addMethods(), but it exists in class "Mockable". Use onlyMethods() for methods that exist in the class.'); + $this->expectExceptionMessage(sprintf( + 'Trying to set mock method "mockableMethod" with addMethods(), but it exists in class "%s". Use onlyMethods() for methods that exist in the class.', + Mockable::class + )); $this->getMockBuilder(Mockable::class) ->addMethods(['mockableMethod']) @@ -214,7 +220,7 @@ public function testByDefaultDoesNotPassArgumentsToTheConstructor(): void public function testMockClassNameCanBeSpecified(): void { $mock = $this->getMockBuilder(Mockable::class) - ->setMockClassName('ACustomClassName') + ->setMockClassName(ACustomClassName::class) ->getMock(); $this->assertInstanceOf(ACustomClassName::class, $mock); diff --git a/tests/unit/Framework/MockObject/MockObjectTest.php b/tests/unit/Framework/MockObject/MockObjectTest.php index 4e762ce915e..296e442ecaf 100644 --- a/tests/unit/Framework/MockObject/MockObjectTest.php +++ b/tests/unit/Framework/MockObject/MockObjectTest.php @@ -22,8 +22,10 @@ use ClassWithStaticMethod; use ExampleTrait; use Exception; +use FunctionCallbackWrapper; use InterfaceWithStaticMethod; use MethodCallback; +use MethodCallbackByReference; use PartialMockTestClass; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; @@ -33,6 +35,7 @@ use StringableClass; use TraitWithConstructor; use Traversable; +use TraversableMockTestInterface; /** * @small @@ -297,7 +300,10 @@ public function testFunctionCallback(): void $mock->expects($this->once()) ->method('doSomething') - ->will($this->returnCallback('FunctionCallbackWrapper::functionCallback')); + ->will($this->returnCallback(sprintf( + '%s::functionCallback', + FunctionCallbackWrapper::class + ))); $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); @@ -307,7 +313,10 @@ public function testFunctionCallback(): void $mock->expects($this->once()) ->method('doSomething') - ->willReturnCallback('FunctionCallbackWrapper::functionCallback'); + ->willReturnCallback(sprintf( + '%s::functionCallback', + FunctionCallbackWrapper::class + )); $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); } @@ -366,7 +375,7 @@ public function testStaticMethodCallback(): void $mock->expects($this->once()) ->method('doSomething') - ->will($this->returnCallback(['MethodCallback', 'staticCallback'])); + ->will($this->returnCallback([MethodCallback::class, 'staticCallback'])); $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); } @@ -601,7 +610,7 @@ public function testObjectMethodCallWithArgumentCloningEnabled(): void { $expectedObject = new stdClass; - $mock = $this->getMockBuilder('SomeClass') + $mock = $this->getMockBuilder(SomeClass::class) ->setMethods(['doSomethingElse']) ->enableArgumentCloning() ->getMock(); @@ -630,7 +639,7 @@ public function testObjectMethodCallWithArgumentCloningDisabled(): void { $expectedObject = new stdClass; - $mock = $this->getMockBuilder('SomeClass') + $mock = $this->getMockBuilder(SomeClass::class) ->setMethods(['doSomethingElse']) ->disableArgumentCloning() ->getMock(); @@ -656,12 +665,12 @@ static function () use (&$actualArguments): void public function testArgumentCloningOptionGeneratesUniqueMock(): void { - $mockWithCloning = $this->getMockBuilder('SomeClass') + $mockWithCloning = $this->getMockBuilder(SomeClass::class) ->setMethods(['doSomethingElse']) ->enableArgumentCloning() ->getMock(); - $mockWithoutCloning = $this->getMockBuilder('SomeClass') + $mockWithoutCloning = $this->getMockBuilder(SomeClass::class) ->setMethods(['doSomethingElse']) ->disableArgumentCloning() ->getMock(); @@ -733,9 +742,15 @@ public function testVerificationOfMethodNameFailsWithWrongParameters(): void $mock->right(['second']); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is \"right\" when invoked 1 time(s)\n" . - 'Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.' . "\n" . - 'Failed asserting that two arrays are equal.', + sprintf( + <<<'EOF' +Expectation failed for method name is "right" when invoked 1 time(s) +Parameter 0 for invocation %s::right(Array (...)) does not match expected value. +Failed asserting that two arrays are equal. +EOF + , + SomeClass::class + ), $e->getMessage() ); } @@ -747,17 +762,24 @@ public function testVerificationOfMethodNameFailsWithWrongParameters(): void // $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is \"right\" when invoked 1 time(s).\n" . - 'Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.' . "\n" . - 'Failed asserting that two arrays are equal.' . "\n" . - '--- Expected' . "\n" . - '+++ Actual' . "\n" . - '@@ @@' . "\n" . - ' Array (' . "\n" . - '- 0 => \'first\'' . "\n" . - '- 1 => \'second\'' . "\n" . - '+ 0 => \'second\'' . "\n" . - ' )' . "\n", + sprintf( + <<<'EOF' +Expectation failed for method name is "right" when invoked 1 time(s). +Parameter 0 for invocation %s::right(Array (...)) does not match expected value. +Failed asserting that two arrays are equal. +--- Expected ++++ Actual +@@ @@ + Array ( +- 0 => 'first' +- 1 => 'second' ++ 0 => 'second' + ) + +EOF + , + SomeClass::class + ), $e->getMessage() ); } @@ -780,7 +802,10 @@ public function testVerificationOfNeverFailsWithEmptyParameters(): void $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - 'SomeClass::right() was not expected to be called.', + sprintf( + '%s::right() was not expected to be called.', + SomeClass::class + ), $e->getMessage() ); } @@ -803,7 +828,10 @@ public function testVerificationOfNeverFailsWithAnyParameters(): void $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - 'SomeClass::right() was not expected to be called.', + sprintf( + '%s::right() was not expected to be called.', + SomeClass::class + ), $e->getMessage() ); } @@ -826,9 +854,15 @@ public function testWithAnythingInsteadOfWithAnyParameters(): void $this->fail('Expected exception'); } catch (ExpectationFailedException $e) { $this->assertSame( - "Expectation failed for method name is \"right\" when invoked 1 time(s)\n" . - 'Parameter count for invocation SomeClass::right() is too low.' . "\n" . - 'To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.', + sprintf( + <<<'EOF' +Expectation failed for method name is "right" when invoked 1 time(s) +Parameter count for invocation %s::right() is too low. +To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead. +EOF + , + SomeClass::class + ), $e->getMessage() ); } @@ -841,7 +875,7 @@ public function testWithAnythingInsteadOfWithAnyParameters(): void */ public function testMockArgumentsPassedByReference(): void { - $foo = $this->getMockBuilder('MethodCallbackByReference') + $foo = $this->getMockBuilder(MethodCallbackByReference::class) ->setMethods(['bar']) ->disableOriginalConstructor() ->disableArgumentCloning() @@ -863,7 +897,7 @@ public function testMockArgumentsPassedByReference(): void */ public function testMockArgumentsPassedByReference2(): void { - $foo = $this->getMockBuilder('MethodCallbackByReference') + $foo = $this->getMockBuilder(MethodCallbackByReference::class) ->disableOriginalConstructor() ->disableArgumentCloning() ->getMock(); @@ -889,7 +923,7 @@ static function (&$a, &$b, $c): void */ public function testMockArgumentsPassedByReference3(): void { - $foo = $this->getMockBuilder('MethodCallbackByReference') + $foo = $this->getMockBuilder(MethodCallbackByReference::class) ->setMethods(['bar']) ->disableOriginalConstructor() ->disableArgumentCloning() @@ -911,7 +945,7 @@ public function testMockArgumentsPassedByReference3(): void */ public function testMockArgumentsPassedByReference4(): void { - $foo = $this->getMockBuilder('MethodCallbackByReference') + $foo = $this->getMockBuilder(MethodCallbackByReference::class) ->setMethods(['bar']) ->disableOriginalConstructor() ->disableArgumentCloning() @@ -1043,9 +1077,8 @@ public function testStringableClassCanBeMocked(): void public function traversableProvider(): array { return [ - 'Traversable' => ['Traversable'], - '\Traversable' => ['\Traversable'], - 'TraversableMockTestInterface' => ['TraversableMockTestInterface'], + Traversable::class => [Traversable::class], + TraversableMockTestInterface::class => [TraversableMockTestInterface::class], ]; } @@ -1087,9 +1120,10 @@ public function testDisableAutomaticReturnValueGeneration(): void ->getMock(); $this->expectException(ExpectationFailedException::class); - $this->expectExceptionMessage( - 'Return value inference disabled and no expectation set up for SomeClass::doSomethingElse()' - ); + $this->expectExceptionMessage(sprintf( + 'Return value inference disabled and no expectation set up for %s::doSomethingElse()', + SomeClass::class + )); $mock->doSomethingElse(1); } @@ -1108,7 +1142,10 @@ public function testDisableAutomaticReturnValueGenerationWithToString(): void $this->fail('Exception expected'); } catch (ExpectationFailedException $e) { $this->assertSame( - 'Return value inference disabled and no expectation set up for StringableClass::__toString()', + sprintf( + 'Return value inference disabled and no expectation set up for %s::__toString()', + StringableClass::class + ), $e->getMessage() ); } diff --git a/tests/unit/Framework/TestCaseTest.php b/tests/unit/Framework/TestCaseTest.php index 619a112b1bf..3182e2e7e8d 100644 --- a/tests/unit/Framework/TestCaseTest.php +++ b/tests/unit/Framework/TestCaseTest.php @@ -95,7 +95,10 @@ public static function tearDownAfterClass(): void public function testCaseToString(): void { $this->assertEquals( - 'PHPUnit\Framework\TestCaseTest::testCaseToString', + sprintf( + '%s::testCaseToString', + self::class + ), $this->toString() ); } @@ -1083,7 +1086,10 @@ public function testRunBareThrowsExceptionWhenTestHasInvalidName($name): void $testCase = new TestWithDifferentNames($name); $this->expectException(Exception::class); - $this->expectExceptionMessage('PHPUnit\Framework\TestCase::$name must be a non-blank string.'); + $this->expectExceptionMessage(sprintf( + '%s::$name must be a non-blank string.', + TestCase::class + )); $testCase->runBare(); } diff --git a/tests/unit/Framework/TestSuiteTest.php b/tests/unit/Framework/TestSuiteTest.php index 1f2771db17b..38ca607134b 100644 --- a/tests/unit/Framework/TestSuiteTest.php +++ b/tests/unit/Framework/TestSuiteTest.php @@ -26,6 +26,7 @@ use OneTestCase; use OverrideTestCase; use RequirementsClassBeforeClassHookTest; +use stdClass; use TestCaseWithExceptionInHook; use TestWithTest; @@ -54,7 +55,7 @@ protected function tearDown(): void */ public function testSuiteNameCanBeSameAsExistingNonTestClassName(): void { - $suite = new TestSuite('stdClass'); + $suite = new TestSuite(stdClass::class); $suite->addTestSuite(OneTestCase::class); $suite->run($this->result); diff --git a/tests/unit/Runner/TestSuiteSorterTest.php b/tests/unit/Runner/TestSuiteSorterTest.php index e8dec9bb162..2a94b68c9c0 100644 --- a/tests/unit/Runner/TestSuiteSorterTest.php +++ b/tests/unit/Runner/TestSuiteSorterTest.php @@ -591,7 +591,13 @@ public function testCanHandleSuiteWithEmptyTestCase(): void $sorter->reorderTestsInSuite($suite, TestSuiteSorter::ORDER_DEFAULT, false, TestSuiteSorter::ORDER_DEFAULT); $this->assertSame(EmptyTestCaseTest::class, $suite->tests()[0]->getName()); - $this->assertSame('No tests found in class "EmptyTestCaseTest".', $suite->tests()[0]->tests()[0]->getMessage()); + $this->assertSame( + sprintf( + 'No tests found in class "%s".', + EmptyTestCaseTest::class + ), + $suite->tests()[0]->tests()[0]->getMessage() + ); } public function suiteSorterOptionPermutationsProvider(): array diff --git a/tests/unit/Util/ConfigurationTest.php b/tests/unit/Util/ConfigurationTest.php index 33bae272ee6..28097b49628 100644 --- a/tests/unit/Util/ConfigurationTest.php +++ b/tests/unit/Util/ConfigurationTest.php @@ -24,6 +24,7 @@ use function unlink; use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\StandardTestSuiteLoader; use PHPUnit\Runner\TestSuiteSorter; use PHPUnit\TextUI\ResultPrinter; use PHPUnit\Util\TestDox\CliTestDoxPrinter; @@ -509,8 +510,8 @@ public function testPHPUnitConfigurationIsReadCorrectly(): void 'defaultTimeLimit' => 123, 'enforceTimeLimit' => false, 'extensionsDirectory' => '/tmp', - 'printerClass' => 'PHPUnit\TextUI\ResultPrinter', - 'testSuiteLoaderClass' => 'PHPUnit\Runner\StandardTestSuiteLoader', + 'printerClass' => ResultPrinter::class, + 'testSuiteLoaderClass' => StandardTestSuiteLoader::class, 'defaultTestSuite' => 'My Test Suite', 'verbose' => false, 'timeoutForSmallTests' => 1, diff --git a/tests/unit/Util/TestClassTest.php b/tests/unit/Util/TestClassTest.php index 5c593082122..bcf12736e61 100644 --- a/tests/unit/Util/TestClassTest.php +++ b/tests/unit/Util/TestClassTest.php @@ -13,9 +13,42 @@ use function preg_match; use function range; use function realpath; +use CoverageClassExtendedTest; +use CoverageClassNothingTest; +use CoverageClassTest; +use CoverageCoversOverridesCoversNothingTest; +use CoverageFunctionParenthesesTest; +use CoverageFunctionParenthesesWhitespaceTest; +use CoverageFunctionTest; +use CoverageMethodNothingCoversMethod; +use CoverageMethodNothingTest; +use CoverageMethodOneLineAnnotationTest; +use CoverageMethodParenthesesTest; +use CoverageMethodParenthesesWhitespaceTest; +use CoverageMethodTest; use CoverageNamespacedFunctionTest; +use CoverageNoneTest; +use CoverageNotPrivateTest; +use CoverageNotProtectedTest; +use CoverageNotPublicTest; +use CoveragePrivateTest; +use CoverageProtectedTest; +use CoveragePublicTest; +use CoverageTwoDefaultClassAnnotations; use DuplicateKeyDataProviderTest; use MultipleDataProviderTest; +use NamespaceCoverageClassExtendedTest; +use NamespaceCoverageClassTest; +use NamespaceCoverageCoversClassPublicTest; +use NamespaceCoverageCoversClassTest; +use NamespaceCoverageMethodTest; +use NamespaceCoverageNotPrivateTest; +use NamespaceCoverageNotProtectedTest; +use NamespaceCoverageNotPublicTest; +use NamespaceCoveragePrivateTest; +use NamespaceCoverageProtectedTest; +use NamespaceCoveragePublicTest; +use NotExistingCoveredElementTest; use ParseTestMethodAnnotationsMock; use PharIo\Version\VersionConstraint; use PHPUnit\Framework\CodeCoverageException; @@ -1176,7 +1209,7 @@ public function testGetLinesToBeCovered2(): void $this->expectException(CodeCoverageException::class); Test::getLinesToBeCovered( - 'NotExistingCoveredElementTest', + NotExistingCoveredElementTest::class, 'testOne' ); } @@ -1186,7 +1219,7 @@ public function testGetLinesToBeCovered3(): void $this->expectException(CodeCoverageException::class); Test::getLinesToBeCovered( - 'NotExistingCoveredElementTest', + NotExistingCoveredElementTest::class, 'testTwo' ); } @@ -1196,7 +1229,7 @@ public function testGetLinesToBeCovered4(): void $this->expectException(CodeCoverageException::class); Test::getLinesToBeCovered( - 'NotExistingCoveredElementTest', + NotExistingCoveredElementTest::class, 'testThree' ); } @@ -1206,7 +1239,7 @@ public function testGetLinesToBeCoveredSkipsNonExistentMethods(): void $this->assertSame( [], Test::getLinesToBeCovered( - 'NotExistingCoveredElementTest', + NotExistingCoveredElementTest::class, 'methodDoesNotExist' ) ); @@ -1217,7 +1250,7 @@ public function testTwoCoversDefaultClassAnnotationsAreNotAllowed(): void $this->expectException(CodeCoverageException::class); Test::getLinesToBeCovered( - 'CoverageTwoDefaultClassAnnotations', + CoverageTwoDefaultClassAnnotations::class, 'testSomething' ); } @@ -1227,7 +1260,7 @@ public function testFunctionParenthesesAreAllowed(): void $this->assertSame( [TEST_FILES_PATH . 'CoveredFunction.php' => range(10, 12)], Test::getLinesToBeCovered( - 'CoverageFunctionParenthesesTest', + CoverageFunctionParenthesesTest::class, 'testSomething' ) ); @@ -1238,7 +1271,7 @@ public function testFunctionParenthesesAreAllowedWithWhitespace(): void $this->assertSame( [TEST_FILES_PATH . 'CoveredFunction.php' => range(10, 12)], Test::getLinesToBeCovered( - 'CoverageFunctionParenthesesWhitespaceTest', + CoverageFunctionParenthesesWhitespaceTest::class, 'testSomething' ) ); @@ -1249,7 +1282,7 @@ public function testMethodParenthesesAreAllowed(): void $this->assertSame( [TEST_FILES_PATH . 'CoveredClass.php' => range(29, 33)], Test::getLinesToBeCovered( - 'CoverageMethodParenthesesTest', + CoverageMethodParenthesesTest::class, 'testSomething' ) ); @@ -1260,7 +1293,7 @@ public function testMethodParenthesesAreAllowedWithWhitespace(): void $this->assertSame( [TEST_FILES_PATH . 'CoveredClass.php' => range(29, 33)], Test::getLinesToBeCovered( - 'CoverageMethodParenthesesWhitespaceTest', + CoverageMethodParenthesesWhitespaceTest::class, 'testSomething' ) ); @@ -1283,164 +1316,158 @@ public function getLinesToBeCoveredProvider(): array { return [ [ - 'CoverageNoneTest', + CoverageNoneTest::class, [], ], [ - 'CoverageClassExtendedTest', + CoverageClassExtendedTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => array_merge(range(27, 44), range(10, 25)), ], ], [ - 'CoverageClassTest', + CoverageClassTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => range(27, 44), ], ], [ - 'CoverageMethodTest', + CoverageMethodTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => range(29, 33), ], ], [ - 'CoverageMethodOneLineAnnotationTest', + CoverageMethodOneLineAnnotationTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => range(29, 33), ], - ], [ - 'CoverageNotPrivateTest', + CoverageNotPrivateTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => array_merge(range(29, 33), range(35, 39)), ], - ], [ - 'CoverageNotProtectedTest', + CoverageNotProtectedTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => array_merge(range(29, 33), range(41, 43)), ], - ], [ - 'CoverageNotPublicTest', + CoverageNotPublicTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => array_merge(range(35, 39), range(41, 43)), ], - ], [ - 'CoveragePrivateTest', + CoveragePrivateTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => range(41, 43), ], - ], [ - 'CoverageProtectedTest', + CoverageProtectedTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => range(35, 39), ], ], [ - 'CoveragePublicTest', + CoveragePublicTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => range(29, 33), ], - ], [ - 'CoverageFunctionTest', + CoverageFunctionTest::class, [ TEST_FILES_PATH . 'CoveredFunction.php' => range(10, 12), ], ], [ - 'NamespaceCoverageClassExtendedTest', + NamespaceCoverageClassExtendedTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => array_merge(range(29, 46), range(12, 27)), ], ], [ - 'NamespaceCoverageClassTest', + NamespaceCoverageClassTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => range(29, 46), ], ], [ - 'NamespaceCoverageMethodTest', + NamespaceCoverageMethodTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => range(31, 35), ], ], [ - 'NamespaceCoverageNotPrivateTest', + NamespaceCoverageNotPrivateTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => array_merge(range(31, 35), range(37, 41)), ], ], [ - 'NamespaceCoverageNotProtectedTest', + NamespaceCoverageNotProtectedTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => array_merge(range(31, 35), range(43, 45)), ], ], [ - 'NamespaceCoverageNotPublicTest', + NamespaceCoverageNotPublicTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => array_merge(range(37, 41), range(43, 45)), ], ], [ - 'NamespaceCoveragePrivateTest', + NamespaceCoveragePrivateTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => range(43, 45), ], ], [ - 'NamespaceCoverageProtectedTest', + NamespaceCoverageProtectedTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => range(37, 41), ], ], [ - 'NamespaceCoveragePublicTest', + NamespaceCoveragePublicTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => range(31, 35), ], ], [ - 'NamespaceCoverageCoversClassTest', + NamespaceCoverageCoversClassTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => array_merge(range(43, 45), range(37, 41), range(31, 35), range(24, 26), range(19, 22), range(14, 17)), ], ], [ - 'NamespaceCoverageCoversClassPublicTest', + NamespaceCoverageCoversClassPublicTest::class, [ TEST_FILES_PATH . 'NamespaceCoveredClass.php' => range(31, 35), ], ], [ - 'CoverageClassNothingTest', + CoverageClassNothingTest::class, false, ], [ - 'CoverageMethodNothingTest', + CoverageMethodNothingTest::class, false, ], [ - 'CoverageCoversOverridesCoversNothingTest', + CoverageCoversOverridesCoversNothingTest::class, [ TEST_FILES_PATH . 'CoveredClass.php' => range(29, 33), ], ], [ - 'CoverageMethodNothingCoversMethod', + CoverageMethodNothingCoversMethod::class, false, ], ]; diff --git a/tests/unit/Util/XmlTest.php b/tests/unit/Util/XmlTest.php index e63752452db..6b3e8a87a6b 100644 --- a/tests/unit/Util/XmlTest.php +++ b/tests/unit/Util/XmlTest.php @@ -40,8 +40,10 @@ public function testPrepareString(string $char): void $this->assertNull( $e, sprintf( - '\PHPUnit\Util\Xml::prepareString("\x%02x") should not crash DomDocument', - ord($char) + '%s::prepareString("\x%02x") should not crash %s', + Xml::class, + ord($char), + DOMDocument::class ) ); }