mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Break out tests for readonly properties
This commit is contained in:
parent
e5e397a6c5
commit
be254efec6
@ -1829,69 +1829,6 @@ class PropertyTypeTest extends TestCase
|
||||
}
|
||||
}',
|
||||
],
|
||||
'readonlyPropertySetInConstructor' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
echo (new A)->bar;'
|
||||
],
|
||||
'readonlyWithPrivateMutationsAllowedPropertySetInAnotherMEthod' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
* @psalm-allow-private-mutation
|
||||
*/
|
||||
public ?string $bar = null;
|
||||
|
||||
public function setBar(string $s) : void {
|
||||
$this->bar = $s;
|
||||
}
|
||||
}
|
||||
|
||||
echo (new A)->bar;'
|
||||
],
|
||||
'readonlyPublicPropertySetInAnotherMEthod' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
public ?string $bar = null;
|
||||
|
||||
public function setBar(string $s) : void {
|
||||
$this->bar = $s;
|
||||
}
|
||||
}
|
||||
|
||||
echo (new A)->bar;'
|
||||
],
|
||||
'readonlyPropertySetChildClass' => [
|
||||
'<?php
|
||||
abstract class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
echo (new B)->bar;'
|
||||
],
|
||||
'staticPropertyDefaultWithStaticType' => [
|
||||
'<?php
|
||||
class Test {
|
||||
@ -3449,104 +3386,6 @@ class PropertyTypeTest extends TestCase
|
||||
}',
|
||||
'error_message' => 'InvalidPropertyAssignmentValue'
|
||||
],
|
||||
'readonlyPropertySetInConstructorAndAlsoAnotherMethodInsideClass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
|
||||
public function setBar() : void {
|
||||
$this->bar = "goodbye";
|
||||
}
|
||||
}',
|
||||
'error_message' => 'InaccessibleProperty',
|
||||
],
|
||||
'readonlyPropertySetInConstructorAndAlsoAnotherMethodInSublass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
public function setBar() : void {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}',
|
||||
'error_message' => 'InaccessibleProperty',
|
||||
],
|
||||
'readonlyPropertySetInConstructorAndAlsoOutsideClass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
$a = new A();
|
||||
$a->bar = "goodbye";',
|
||||
'error_message' => 'InaccessibleProperty',
|
||||
],
|
||||
'readonlyPropertySetInConstructorAndAlsoOutsideClassWithAllowPrivate' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
* @psalm-allow-private-mutation
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
|
||||
public function setAgain() : void {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
$a = new A();
|
||||
$a->bar = "goodbye";',
|
||||
'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:19:21',
|
||||
],
|
||||
'readonlyPublicPropertySetInConstructorAndAlsoOutsideClass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
|
||||
public function setAgain() : void {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
$a = new A();
|
||||
$a->bar = "goodbye";',
|
||||
'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:18:21',
|
||||
],
|
||||
'addNullToMixedAfterNullablePropertyFetch' => [
|
||||
'<?php
|
||||
class A {
|
||||
|
191
tests/ReadonlyPropertyTest.php
Normal file
191
tests/ReadonlyPropertyTest.php
Normal file
@ -0,0 +1,191 @@
|
||||
<?php
|
||||
namespace Psalm\Tests;
|
||||
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use Psalm\Config;
|
||||
use Psalm\Context;
|
||||
|
||||
class ReadonlyPropertyTest extends TestCase
|
||||
{
|
||||
use Traits\InvalidCodeAnalysisTestTrait;
|
||||
use Traits\ValidCodeAnalysisTestTrait;
|
||||
|
||||
/**
|
||||
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
||||
*/
|
||||
public function providerValidCodeParse(): iterable
|
||||
{
|
||||
return [
|
||||
'readonlyPropertySetInConstructor' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
echo (new A)->bar;'
|
||||
],
|
||||
'readonlyWithPrivateMutationsAllowedPropertySetInAnotherMEthod' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
* @psalm-allow-private-mutation
|
||||
*/
|
||||
public ?string $bar = null;
|
||||
|
||||
public function setBar(string $s) : void {
|
||||
$this->bar = $s;
|
||||
}
|
||||
}
|
||||
|
||||
echo (new A)->bar;'
|
||||
],
|
||||
'readonlyPublicPropertySetInAnotherMEthod' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
public ?string $bar = null;
|
||||
|
||||
public function setBar(string $s) : void {
|
||||
$this->bar = $s;
|
||||
}
|
||||
}
|
||||
|
||||
echo (new A)->bar;'
|
||||
],
|
||||
'readonlyPropertySetChildClass' => [
|
||||
'<?php
|
||||
abstract class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
echo (new B)->bar;'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string,array{string,error_message:string,1?:string[],2?:bool,3?:string}>
|
||||
*/
|
||||
public function providerInvalidCodeParse(): iterable
|
||||
{
|
||||
return [
|
||||
'readonlyPropertySetInConstructorAndAlsoAnotherMethodInsideClass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
|
||||
public function setBar() : void {
|
||||
$this->bar = "goodbye";
|
||||
}
|
||||
}',
|
||||
'error_message' => 'InaccessibleProperty',
|
||||
],
|
||||
'readonlyPropertySetInConstructorAndAlsoAnotherMethodInSublass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
public function setBar() : void {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}',
|
||||
'error_message' => 'InaccessibleProperty',
|
||||
],
|
||||
'readonlyPropertySetInConstructorAndAlsoOutsideClass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
$a = new A();
|
||||
$a->bar = "goodbye";',
|
||||
'error_message' => 'InaccessibleProperty',
|
||||
],
|
||||
'readonlyPropertySetInConstructorAndAlsoOutsideClassWithAllowPrivate' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @readonly
|
||||
* @psalm-allow-private-mutation
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
|
||||
public function setAgain() : void {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
$a = new A();
|
||||
$a->bar = "goodbye";',
|
||||
'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:19:21',
|
||||
],
|
||||
'readonlyPublicPropertySetInConstructorAndAlsoOutsideClass' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
public string $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
|
||||
public function setAgain() : void {
|
||||
$this->bar = "hello";
|
||||
}
|
||||
}
|
||||
|
||||
$a = new A();
|
||||
$a->bar = "goodbye";',
|
||||
'error_message' => 'InaccessibleProperty - src' . DIRECTORY_SEPARATOR . 'somefile.php:18:21',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user