• Stars
    star
    427
  • Rank 97,725 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 6 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHPUnit extensions and rules for PHPStan

PHPStan PHPUnit extensions and rules

Build Latest Stable Version License

This extension provides following features:

  • createMock(), getMockForAbstractClass() and getMockFromWsdl() methods return an intersection type (see the detailed explanation of intersection types) of the mock object and the mocked class so that both methods from the mock object (like expects) and from the mocked class are available on the object.
  • getMock() called on MockBuilder is also supported.
  • Interprets Foo|PHPUnit_Framework_MockObject_MockObject in phpDoc so that it results in an intersection type instead of a union type.
  • Defines early terminating method calls for the PHPUnit\Framework\TestCase class to prevent undefined variable errors.
  • Specifies types of expressions passed to various assert methods like assertInstanceOf, assertTrue, assertInternalType etc.
  • Combined with PHPStan's level 4, it points out always-true and always-false asserts like assertTrue(true) etc.

It also contains this strict framework-specific rules (can be enabled separately):

  • Check that you are not using assertSame() with true as expected value. assertTrue() should be used instead.
  • Check that you are not using assertSame() with false as expected value. assertFalse() should be used instead.
  • Check that you are not using assertSame() with null as expected value. assertNull() should be used instead.
  • Check that you are not using assertSame() with count($variable) as second parameter. assertCount($variable) should be used instead.

How to document mock objects in phpDocs?

If you need to configure the mock even after you assign it to a property or return it from a method, you should add PHPUnit_Framework_MockObject_MockObject to the phpDoc:

/**
 * @return Foo&PHPUnit_Framework_MockObject_MockObject
 */
private function createFooMock()
{
	return $this->createMock(Foo::class);
}

public function testSomething()
{
	$fooMock = $this->createFooMock();
	$fooMock->method('doFoo')->will($this->returnValue('test'));
	$fooMock->doFoo();
}

Please note that the correct syntax for intersection types is Foo&PHPUnit_Framework_MockObject_MockObject. Foo|PHPUnit_Framework_MockObject_MockObject is also supported, but only for ecosystem and legacy reasons.

If the mock is fully configured and only the methods of the mocked class are supposed to be called on the value, it's fine to typehint only the mocked class:

/** @var Foo */
private $foo;

protected function setUp()
{
	$fooMock = $this->createMock(Foo::class);
	$fooMock->method('doFoo')->will($this->returnValue('test'));
	$this->foo = $fooMock;
}

public function testSomething()
{
	$this->foo->doFoo();
	// $this->foo->method() and expects() can no longer be called
}

Installation

To use this extension, require it in Composer:

composer require --dev phpstan/phpstan-phpunit

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

includes:
    - vendor/phpstan/phpstan-phpunit/extension.neon

To perform framework-specific checks, include also this file:

    - vendor/phpstan/phpstan-phpunit/rules.neon

More Repositories

1

phpstan

PHP Static Analysis Tool - discover bugs in your code without running it!
PHP
12,445
star
2

phpdoc-parser

Next-gen phpDoc parser with support for intersection types and generics
PHP
1,212
star
3

phpstan-symfony

Symfony extension for PHPStan
PHP
655
star
4

phpstan-doctrine

Doctrine extensions for PHPStan
PHP
547
star
5

phpstan-strict-rules

Extra strict and opinionated rules for PHPStan
PHP
545
star
6

extension-installer

Composer plugin for automatic installation of PHPStan extensions.
PHP
361
star
7

phpstan-deprecation-rules

PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.
PHP
341
star
8

phpstan-src

PHPStan's source code. This is where development happens. Check https://github.com/phpstan/phpstan for the distribution repository.
PHP
295
star
9

phpstan-webmozart-assert

PHPStan extension for webmozart/assert
PHP
150
star
10

phpstan-nette

Nette Framework class reflection extension for PHPStan & framework-specific rules
PHP
98
star
11

phpstan-shim

[DEPRECATED] This repository provides easy way to install PHPStan without the risk of conflicting dependencies.
PHP
87
star
12

phpstan-mockery

PHPStan extension for Mockery
PHP
76
star
13

phpstan-beberlei-assert

PHPStan extension for beberlei/assert
PHP
35
star
14

vim-phpstan

A Vim plugin for PHPStan - https://github.com/phpstan/phpstan. It calls `phpstan` to do static analysis of your PHP code and displays the errors in Vim's quickfix list.
PHP
28
star
15

php-8-stubs

PHP
25
star
16

phpstan-php-parser

PHP-Parser extension for PHPStan
Makefile
23
star
17

phpstan-dibi

Dibi class reflection extension for PHPStan
PHP
13
star
18

build-cs

Coding standard for 1st party PHPStan extensions
2
star