Skip to content

Setting a readonly property in setUp triggers error #118

@IonBazan

Description

@IonBazan

Readonly properties are supposed to be set in constructor only but PHPUnit tests should not override the constructor, thus it is perfectly valid to set them in setUp() or setUpBeforeClass() methods.

Consider example:

class SubjectTest extends TestCase
{
    protected readonly Subject $subject;

    protected function setUp(): void
    {
        parent::setUp();

        $this->subject = new Subject('test');
    }
}

Triggers following error:

Readonly property App\Tests\SubjectTest::$subject is assigned outside of the constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions