• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Draft / setUp() must be compatible with PHPUnit\Framework\TestCase::setUp

setUp() must be compatible with PHPUnit\Framework\TestCase::setUp

Last updated on August 26, 2019 by Sal Ferrarello

When setting up a new site using PHPUnit 8.x and PHP 7.2, I got the error message:

setUp() must be compatible with PHPUnit\Framework\TestCase::setUp

Because we can now define the method as returning no value (with :void) and this is part of the method definition starting in PHPUnit 8.0.0, we need to add this in our code, as well. In other words, cut and paste from my old project didn’t work. 😀

Code I Was Trying to use

protected function setUp() {
    parent::setUp();
    Monkey\setUp();
}

protected function tearDown() {
    Monkey\tearDown();
    parent::tearDown();
}

Code I Needed to Use

protected function setUp(): void {
    parent::setUp();
    Monkey\setUp();
}

protected function tearDown(): void {
    Monkey\tearDown();
    parent::tearDown();
}
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft, Programming, Solution Tagged With: PHP, PHPUnit

Reader Interactions

Comments

  1. mat says

    April 1, 2020 at 8:47 am

    it saved me time 😉
    thanks!

    Reply
  2. Dean Dye says

    October 21, 2021 at 6:05 pm

    Legend thanks for this!

    Reply
  3. Nathaniel Rogers says

    January 31, 2022 at 3:49 pm

    Heaps useful! Thanks

    Reply
  4. Leandro says

    March 22, 2023 at 1:39 pm

    my code shows this error:

    Declaration of Illuminate\Foundation\Testing\TestCase::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp()

    so I need to modify the file inside vendor folder?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in