lucatume / wp-browser

The easy and reliable way to test WordPress with Codeception. 10 years of proven success.
https://wpbrowser.wptestkit.dev/
MIT License
604 stars 86 forks source link

[BUG] Call to undefined method testClass::getAnnotations() #484

Closed dingo-d closed 3 years ago

dingo-d commented 3 years ago

Environment OS: MacOS 11.1 PHP version: 7.4.13 Installed Codeception version: 4.1.15 Installed wp-browser version: 2.6.17 WordPress version: 5.6
Local development environment: Laravel Valet WordPress structure and management: default

Can you perform the test manually? The feature I'm testing works when running from browser.

Codeception configuration file

paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/Support
    envs: tests/Envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
    commands:
        - Codeception\Command\GenerateWPUnit
        - Codeception\Command\GenerateWPRestApi
        - Codeception\Command\GenerateWPRestController
        - Codeception\Command\GenerateWPRestPostTypeController
        - Codeception\Command\GenerateWPAjax
        - Codeception\Command\GenerateWPCanonical
        - Codeception\Command\GenerateWPXMLRPC
params:
    - .env.testing
namespace: Tests
coverage:
    enabled: true
    include:
        - src/*
    exclude:
        - src/Core/CompiledContainer.php
    low_limit: 30
    high_limit: 75
settings:
    colors: true
    memory_limit: 1024M

Suite configuration file Paste, in a fenced YAML block, the content of the suite configuration file; remove any sensitive data!

# Codeception Test Suite Configuration
#
# Suite for unit or integration tests that require WordPress functions and classes.

actor: IntegrationTester
modules:
    enabled:
        - WPLoader
        - \Helper\Wpunit
    config:
        WPLoader:
            wpRootFolder: "%WP_ROOT_FOLDER%"
            dbName: "%TEST_DB_NAME%"
            dbHost: "%TEST_DB_HOST%"
            dbUser: "%TEST_DB_USER%"
            dbPassword: "%TEST_DB_PASSWORD%"
            tablePrefix: "%TEST_TABLE_PREFIX%"
            domain: "%TEST_SITE_WP_DOMAIN%"
            adminEmail: "%TEST_SITE_ADMIN_EMAIL%"
            title: "Test"
            theme: a1-careers-page
            plugins: ['advanced-custom-fields-pro/acf.php']
            activatePlugins: ['advanced-custom-fields-pro/acf.php']

Describe the bug When running integration tests i get the

[Error] Call to undefined method testClass::getAnnotations()

Pointing to the parent::setUp() method.

Test looks like this:

<?php

namespace Tests\Integration\Rest;

use Codeception\TestCase\WPTestCase;

class CandidatesTest extends WPTestCase
{
    private $routeName;
    private $request;
    private $server;

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

        global $wp_rest_server;

        $this->server = $wp_rest_server = new \WP_REST_Server;
        do_action('rest_api_init');

        $this->routeName = '/wp-json/a1c/v1/candidate';

        $_SERVER['REQUEST_URI'] = $this->routeName;

        $this->request = new \WP_REST_Request('GET', $this->routeName);
    }

    public function tearDown(): void
    {
        global $wp_rest_server;
        $wp_rest_server = null;

        $_SERVER['REQUEST_URI'] = '';

        parent::tearDown();
    }

    /**
     * Tests if the route was registered with WordPress
     */
    public function testRouteRegistered()
    {
        $routes = $this->server->get_routes();
        $this->assertArrayHasKey($this->routeName, $routes);
    }
}

Expected behavior Test should pass. Same test, on a different project worked. Maybe something broke in the latest versions of PHPUnit/Codeception?

lucatume commented 3 years ago

@dingo-d I have just seen that and missed the notification completely...

I will look into this next.

lucatume commented 3 years ago

@dingo-d I'm looking into this: what PHPUnit version was installed?

dingo-d commented 3 years ago

The PHPUnit version was 9.5.1. It's not a priority now, but I never saw this issue, and on other projects, it worked fine. So it was a bit odd 🤷🏼‍♂️

lucatume commented 3 years ago

Closing this as I could not reproduce locally and could not track down the issue source.

dingo-d commented 3 years ago

Yeah, it could have been a fluke on the project 🤷🏼‍♂️ Thanks for the help 👍🏼

phil-davis commented 3 years ago

Note: https://github.com/sebastianbergmann/phpunit/commit/68582043e149039cfa3596b42ed35753dcf54fb2

getAnnotations() was removed from phpunit9.

It was tagged: @internal This method is not covered by the backward compatibility promise for PHPUnit

So, sadly, it "just broke". You need to change to code like:

use PHPUnit\Util\Test as TestUtil;

$annotations = TestUtil::parseTestMethodAnnotations(
    static::class,
    $this->name
);

But still, that TestUtil class is tagged: @internal This class is not covered by the backward compatibility promise for PHPUnit

So it could still change at any moment.

overclokk commented 1 year ago

I was getting the same error, in my case the solution was to modify wpRootFolder: "%WP_ROOT_FOLDER%" in wpunit.suite.yml with a correct value because recently a canged the unit from E to C and updated composer vendor with latest packages, a better solution for me is to set WP_ROOT_FOLDER="../../../" to have a relative path and not absolute one.

(3 hours of debugging only for a fu****g single letter 😅)