1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/MethodCallTest.php
Jon Ursenbach 11bc153deb Rewriting and streamlining every unit test with data providers. (#147)
* Rewriting and streamlining every unit test with data providers.

All unit tests have been rewritten into PHPUnit data providers
to reduce the amount of unnecessary code-reuse through out the
test suite.
2017-04-24 23:45:02 -04:00

146 lines
4.4 KiB
PHP

<?php
namespace Psalm\Tests;
class MethodCallTest extends TestCase
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'parentStaticCall' => [
'<?php
class A {
/** @return void */
public static function foo(){}
}
class B extends A {
/** @return void */
public static function bar(){
parent::foo();
}
}'
],
'nonStaticInvocation' => [
'<?php
class Foo {
public static function barBar() : void {}
}
(new Foo())->barBar();'
],
'staticInvocation' => [
'<?php
class A {
public static function fooFoo() : void {}
}
class B extends A {
}
B::fooFoo();'
]
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'staticInvocation' => [
'<?php
class Foo {
public function barBar() : void {}
}
Foo::barBar();',
'error_message' => 'InvalidStaticInvocation'
],
'parentStaticCall' => [
'<?php
class A {
/** @return void */
public function foo(){}
}
class B extends A {
/** @return void */
public static function bar(){
parent::foo();
}
}',
'error_message' => 'InvalidStaticInvocation'
],
'mixedMethodCall' => [
'<?php
class Foo {
public static function barBar() : void {}
}
/** @var mixed */
$a = (new Foo());
$a->barBar();',
'error_message' => 'MixedMethodCall',
'error_levels' => [
'MissingPropertyType',
'MixedAssignment'
]
],
'selfNonStaticInvocation' => [
'<?php
class A {
public function fooFoo() : void {}
public function barBar() : void {
self::fooFoo();
}
}',
'error_message' => 'NonStaticSelfCall'
],
'noParent' => [
'<?php
class Foo {
public function barBar() : void {
parent::barBar();
}
}',
'error_message' => 'ParentNotFound'
],
'coercedClass' => [
'<?php
class NullableClass {
}
class NullableBug {
/**
* @param string $className
* @return object|null
*/
public static function mock($className) {
if (!$className) { return null; }
return new $className();
}
/**
* @return NullableClass
*/
public function returns_nullable_class() {
return self::mock("NullableClass");
}
}',
'error_message' => 'MoreSpecificReturnType',
'error_levels' => ['MixedInferredReturnType']
]
];
}
}