Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in assertion and mock type definitions #3708

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
910cf67
Adding @vimeo/psalm-specific templated type declarations for the mock…
Ocramius May 26, 2019
764d671
Added workaround to allow Psalm to recognize `MockObject#method()` calls
Ocramius May 26, 2019
ae6da69
Decorating `Assert` with @vimeo/psalm-specific type annotations
Ocramius May 26, 2019
d50a353
Split assertion type-check tests into isolated test scenarios
Ocramius Jun 5, 2019
db2a042
Added `psalm.xml` configuration file to check the happy-path static s…
Ocramius Jun 5, 2019
7edbe46
Added static-analysis tests to CI runs
Ocramius Jun 5, 2019
ce564bc
Reverted BC brak: `MockObject#method()` now correctly detected by Psalm
Ocramius Jun 5, 2019
a503a31
Removed `vimeo/psalm` from `require-dev`: PHPUnit already has a scope…
Ocramius Jun 5, 2019
4a47a83
`PropertyNotSetInConstructor` cannot be avoided in `TestCase` child c…
Ocramius Jun 5, 2019
6b89215
Disabling type declarations for trait mocking `TestCase` methods
Ocramius Jun 5, 2019
cde4a44
Psalm must run against static analysis tests with `totallyTyped="true"`
Ocramius Jun 5, 2019
0d46ceb
Added PHPStorm type meta-hints, allowing downstream intellisense on m…
Ocramius Jun 5, 2019
47f3ec0
Corrected `Assert::assertIs(Not)Scalar()` type assertions
Ocramius Jun 5, 2019
ed1cc0d
Using objects/integers to verify `assert(Not)Empty` type assertions
Ocramius Jun 5, 2019
a2514b4
Failures should be allowed on PHP nightly
Ocramius Jun 5, 2019
a967a09
Corrected type-check errors detected by Psalm run over the whole code…
Ocramius Jun 5, 2019
a330d23
Excluded static-analysis directory from CS-fixer paths
Ocramius Jun 5, 2019
c7cfb86
`TestCase#expectException()` requires a `class-string<\Throwable>`
Ocramius Jun 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .php_cs.dist
Expand Up @@ -198,7 +198,11 @@ return PhpCsFixer\Config::create()
->files()
->in(__DIR__ . '/build')
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->in(__DIR__ . '/tests/_files')
->in(__DIR__ . '/tests/basic')
->in(__DIR__ . '/tests/end-to-end')
->in(__DIR__ . '/tests/fail')
->in(__DIR__ . '/tests/unit')
->notName('*.phpt')
->notName('ClassWithAllPossibleReturnTypes.php')
);
39 changes: 39 additions & 0 deletions .phpstorm.meta.php
@@ -0,0 +1,39 @@
<?php

namespace PHPSTORM_META {

override(
\PHPUnit\Framework\TestCase::createMock(0),
map([
'@&\PHPUnit\Framework\MockObject\MockObject',
])
);

override(
\PHPUnit\Framework\TestCase::createConfiguredMock(0),
map([
'@&\PHPUnit\Framework\MockObject\MockObject',
])
);

override(
\PHPUnit\Framework\TestCase::createPartialMock(0),
map([
'@&\PHPUnit\Framework\MockObject\MockObject',
])
);

override(
\PHPUnit\Framework\TestCase::createTestProxy(0),
map([
'@&\PHPUnit\Framework\MockObject\MockObject',
])
);

override(
\PHPUnit\Framework\TestCase::getMockForAbstractClass(0),
map([
'@&\PHPUnit\Framework\MockObject\MockObject',
])
);
}
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -13,7 +13,7 @@ php:

matrix:
allow_failures:
- php: master
- php: nightly
fast_finish: true

env:
Expand Down Expand Up @@ -64,4 +64,5 @@ jobs:
- phpenv config-rm xdebug.ini
script:
- travis_retry ./tools/composer update $DEFAULT_COMPOSER_FLAGS
- ./tools/psalm --config=tests/static-analysis/config.xml --no-progress
- ./tools/psalm --config=.psalm/config.xml --no-progress --shepherd --stats
78 changes: 78 additions & 0 deletions src/Framework/Assert.php
Expand Up @@ -819,6 +819,8 @@ public static function assertAttributeNotEquals($expected, string $actualAttribu
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert empty $actual
*/
public static function assertEmpty($actual, string $message = ''): void
{
Expand Down Expand Up @@ -853,6 +855,8 @@ public static function assertAttributeEmpty(string $haystackAttributeName, $hays
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !empty $actual
*/
public static function assertNotEmpty($actual, string $message = ''): void
{
Expand Down Expand Up @@ -1305,6 +1309,8 @@ public static function assertFileNotIsWritable(string $file, string $message = '
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert true $condition
*/
public static function assertTrue($condition, string $message = ''): void
{
Expand All @@ -1316,6 +1322,8 @@ public static function assertTrue($condition, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !true $condition
*/
public static function assertNotTrue($condition, string $message = ''): void
{
Expand All @@ -1327,6 +1335,8 @@ public static function assertNotTrue($condition, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert false $condition
*/
public static function assertFalse($condition, string $message = ''): void
{
Expand All @@ -1338,6 +1348,8 @@ public static function assertFalse($condition, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !false $condition
*/
public static function assertNotFalse($condition, string $message = ''): void
{
Expand All @@ -1349,6 +1361,8 @@ public static function assertNotFalse($condition, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert null $actual
*/
public static function assertNull($actual, string $message = ''): void
{
Expand All @@ -1360,6 +1374,8 @@ public static function assertNull($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !null $actual
*/
public static function assertNotNull($actual, string $message = ''): void
{
Expand Down Expand Up @@ -1556,6 +1572,10 @@ public static function assertObjectNotHasAttribute(string $attributeName, $objec
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-template ExpectedType
* @psalm-param ExpectedType $expected
* @psalm-assert =ExpectedType $actual
*/
public static function assertSame($expected, $actual, string $message = ''): void
{
Expand Down Expand Up @@ -1643,6 +1663,10 @@ public static function assertAttributeNotSame($expected, string $actualAttribute
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws Exception
*
* @psalm-template ExpectedType of object
* @psalm-param class-string<ExpectedType> $expected
* @psalm-assert ExpectedType $actual
*/
public static function assertInstanceOf(string $expected, $actual, string $message = ''): void
{
Expand All @@ -1668,6 +1692,8 @@ public static function assertInstanceOf(string $expected, $actual, string $messa
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338
* @codeCoverageIgnore
*
* @psalm-param class-string $expected
*/
public static function assertAttributeInstanceOf(string $expected, string $attributeName, $classOrObject, string $message = ''): void
{
Expand All @@ -1686,6 +1712,10 @@ public static function assertAttributeInstanceOf(string $expected, string $attri
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws Exception
*
* @psalm-template ExpectedType of object
* @psalm-param class-string<ExpectedType> $expected
* @psalm-assert !ExpectedType $actual
*/
public static function assertNotInstanceOf(string $expected, $actual, string $message = ''): void
{
Expand Down Expand Up @@ -1713,6 +1743,8 @@ public static function assertNotInstanceOf(string $expected, $actual, string $me
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338
* @codeCoverageIgnore
*
* @psalm-param class-string $expected
*/
public static function assertAttributeNotInstanceOf(string $expected, string $attributeName, $classOrObject, string $message = ''): void
{
Expand Down Expand Up @@ -1773,6 +1805,8 @@ public static function assertAttributeInternalType(string $expected, string $att
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert array $actual
*/
public static function assertIsArray($actual, string $message = ''): void
{
Expand All @@ -1788,6 +1822,8 @@ public static function assertIsArray($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert bool $actual
*/
public static function assertIsBool($actual, string $message = ''): void
{
Expand All @@ -1803,6 +1839,8 @@ public static function assertIsBool($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert float $actual
*/
public static function assertIsFloat($actual, string $message = ''): void
{
Expand All @@ -1818,6 +1856,8 @@ public static function assertIsFloat($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert int $actual
*/
public static function assertIsInt($actual, string $message = ''): void
{
Expand All @@ -1833,6 +1873,8 @@ public static function assertIsInt($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert numeric $actual
*/
public static function assertIsNumeric($actual, string $message = ''): void
{
Expand All @@ -1848,6 +1890,8 @@ public static function assertIsNumeric($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert object $actual
*/
public static function assertIsObject($actual, string $message = ''): void
{
Expand All @@ -1863,6 +1907,8 @@ public static function assertIsObject($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert resource $actual
*/
public static function assertIsResource($actual, string $message = ''): void
{
Expand All @@ -1878,6 +1924,8 @@ public static function assertIsResource($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert string $actual
*/
public static function assertIsString($actual, string $message = ''): void
{
Expand All @@ -1893,6 +1941,8 @@ public static function assertIsString($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert scalar $actual
*/
public static function assertIsScalar($actual, string $message = ''): void
{
Expand All @@ -1908,6 +1958,8 @@ public static function assertIsScalar($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert callable $actual
*/
public static function assertIsCallable($actual, string $message = ''): void
{
Expand All @@ -1923,6 +1975,8 @@ public static function assertIsCallable($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert iterable $actual
*/
public static function assertIsIterable($actual, string $message = ''): void
{
Expand Down Expand Up @@ -1960,6 +2014,8 @@ public static function assertNotInternalType(string $expected, $actual, string $
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !array $actual
*/
public static function assertIsNotArray($actual, string $message = ''): void
{
Expand All @@ -1975,6 +2031,8 @@ public static function assertIsNotArray($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !bool $actual
*/
public static function assertIsNotBool($actual, string $message = ''): void
{
Expand All @@ -1990,6 +2048,8 @@ public static function assertIsNotBool($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !float $actual
*/
public static function assertIsNotFloat($actual, string $message = ''): void
{
Expand All @@ -2005,6 +2065,8 @@ public static function assertIsNotFloat($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !int $actual
*/
public static function assertIsNotInt($actual, string $message = ''): void
{
Expand All @@ -2020,6 +2082,8 @@ public static function assertIsNotInt($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !numeric $actual
*/
public static function assertIsNotNumeric($actual, string $message = ''): void
{
Expand All @@ -2035,6 +2099,8 @@ public static function assertIsNotNumeric($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !object $actual
*/
public static function assertIsNotObject($actual, string $message = ''): void
{
Expand All @@ -2050,6 +2116,8 @@ public static function assertIsNotObject($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !resource $actual
*/
public static function assertIsNotResource($actual, string $message = ''): void
{
Expand All @@ -2065,6 +2133,8 @@ public static function assertIsNotResource($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !string $actual
*/
public static function assertIsNotString($actual, string $message = ''): void
{
Expand All @@ -2080,6 +2150,8 @@ public static function assertIsNotString($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !scalar $actual
*/
public static function assertIsNotScalar($actual, string $message = ''): void
{
Expand All @@ -2095,6 +2167,8 @@ public static function assertIsNotScalar($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !callable $actual
*/
public static function assertIsNotCallable($actual, string $message = ''): void
{
Expand All @@ -2110,6 +2184,8 @@ public static function assertIsNotCallable($actual, string $message = ''): void
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @psalm-assert !iterable $actual
*/
public static function assertIsNotIterable($actual, string $message = ''): void
{
Expand Down Expand Up @@ -2986,6 +3062,8 @@ public static function countOf(int $count): Count
* Fails a test with the given message.
*
* @throws AssertionFailedError
*
* @psalm-return never-return
*/
public static function fail(string $message = ''): void
{
Expand Down