From f9b594909358b4fd01101470db59260c467e99f7 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 31 Mar 2024 20:14:48 +0200 Subject: [PATCH] Refactor browser test, test monolog bridge (#641) * Refactor browser test, test monolog bridge * Rename * Add Doctrine --- .github/workflows/run-integration.yml | 45 +++++++++++++++++++ .github/workflows/run-tests.yml | 4 +- .gitignore | 3 +- README.md | 7 ++- composer.json | 2 +- demo/bridge/doctrine/cli-config.php | 2 + demo/bridge/doctrine/composer.json | 4 ++ phpunit.xml.dist | 8 +++- .../Browser/AbstractBrowserTest.php | 10 +---- .../Tests/Browser/Bridge/DoctrineTest.php | 39 ++++++++++++++++ .../Tests/Browser/Bridge/MonologTest.php | 38 ++++++++++++++++ .../Browser/DebugbarTest.php} | 20 ++++----- 12 files changed, 157 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/run-integration.yml rename tests/DebugBar/{ => Tests}/Browser/AbstractBrowserTest.php (61%) create mode 100644 tests/DebugBar/Tests/Browser/Bridge/DoctrineTest.php create mode 100644 tests/DebugBar/Tests/Browser/Bridge/MonologTest.php rename tests/DebugBar/{Browser/BasicBrowserTest.php => Tests/Browser/DebugbarTest.php} (83%) diff --git a/.github/workflows/run-integration.yml b/.github/workflows/run-integration.yml new file mode 100644 index 0000000..18644d7 --- /dev/null +++ b/.github/workflows/run-integration.yml @@ -0,0 +1,45 @@ +name: Integration + +on: + push: + branches: + - master + pull_request: + branches: + - "*" + schedule: + - cron: '0 0 * * *' + +jobs: + php-tests: + runs-on: ubuntu-latest + timeout-minutes: 15 + env: + COMPOSER_NO_INTERACTION: 1 + + strategy: + matrix: + php: [8.3, 7.4] + + name: PHP${{ matrix.php }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + extensions: pdo_sqlite + + - name: Install dependencies + run: | + composer update --prefer-dist --no-progress + composer update --prefer-dist --no-progress --working-dir=demo/bridge/monolog + composer update --prefer-dist --no-progress --working-dir=demo/bridge/doctrine + composer run --working-dir=demo/bridge/doctrine install-schema + + - name: Execute Unit Tests + run: vendor/bin/phpunit --testsuite=Browser diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 39f2e04..51a5bb3 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -12,7 +12,7 @@ on: jobs: php-tests: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest timeout-minutes: 15 env: COMPOSER_NO_INTERACTION: 1 @@ -38,4 +38,4 @@ jobs: run: composer update --prefer-dist --no-progress - name: Execute Unit Tests - run: vendor/bin/phpunit + run: vendor/bin/phpunit --testsuite=Unit diff --git a/.gitignore b/.gitignore index 0914e0c..db0f376 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ composer.lock /vendor /demo/bridge/*/vendor +/demo/bridge/doctrine/db.sqlite /src/DebugBar/Resources/vendor .phpunit.result.cache /drivers -.phpunit.cache/ \ No newline at end of file +.phpunit.cache/ diff --git a/README.md b/README.md index 810a45b..54add9a 100644 --- a/README.md +++ b/README.md @@ -115,4 +115,9 @@ To run the demo, clone this repository and start the Built-In PHP webserver from php -S localhost:8000 ``` -Then visit http://localhost:8000/demo/ \ No newline at end of file +Then visit http://localhost:8000/demo/ + +## Testing + +To test, run `php vendor/bin/phpunit`. +To debug Browser tests, you can run `PANTHER_NO_HEADLESS=1 vendor/bin/phpunit --debug` \ No newline at end of file diff --git a/composer.json b/composer.json index f1703a2..2c5a7c2 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ }, "autoload-dev": { "psr-4": { - "DebugBar\\": "tests/DebugBar/" + "DebugBar\\Tests\\": "tests/DebugBar/Tests" } }, "suggest": { diff --git a/demo/bridge/doctrine/cli-config.php b/demo/bridge/doctrine/cli-config.php index 546dba5..3bbc24d 100644 --- a/demo/bridge/doctrine/cli-config.php +++ b/demo/bridge/doctrine/cli-config.php @@ -7,3 +7,5 @@ $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array( 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) )); + +return $helperSet; \ No newline at end of file diff --git a/demo/bridge/doctrine/composer.json b/demo/bridge/doctrine/composer.json index 1e9bead..21733a2 100644 --- a/demo/bridge/doctrine/composer.json +++ b/demo/bridge/doctrine/composer.json @@ -1,9 +1,13 @@ { "require": { "doctrine/orm": "2.*", + "doctrine/cache": "1.11.*", "symfony/yaml": "2.*" }, "autoload": { "psr-0": {"": "src/"} + }, + "scripts": { + "install-schema": "@php vendor/bin/doctrine orm:schema-tool:update --force" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d302284..890afc9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,12 @@ - - ./tests/DebugBar/ + + ./tests/DebugBar/Tests + ./tests/DebugBar/Tests/Browser + + + ./tests/DebugBar/Tests/Browser diff --git a/tests/DebugBar/Browser/AbstractBrowserTest.php b/tests/DebugBar/Tests/Browser/AbstractBrowserTest.php similarity index 61% rename from tests/DebugBar/Browser/AbstractBrowserTest.php rename to tests/DebugBar/Tests/Browser/AbstractBrowserTest.php index 30752de..c9bb177 100644 --- a/tests/DebugBar/Browser/AbstractBrowserTest.php +++ b/tests/DebugBar/Tests/Browser/AbstractBrowserTest.php @@ -1,13 +1,7 @@ filter('a.phpdebugbar-tab[data-collector="'.$tab.'"]'); - return strpos($node->attr('class'), 'phpdebugbar-active"') !== false; + return strpos($node->attr('class'), 'phpdebugbar-active') !== false; } public function getTabLink(Crawler $crawler, $tab): Link diff --git a/tests/DebugBar/Tests/Browser/Bridge/DoctrineTest.php b/tests/DebugBar/Tests/Browser/Bridge/DoctrineTest.php new file mode 100644 index 0000000..2d9e320 --- /dev/null +++ b/tests/DebugBar/Tests/Browser/Bridge/DoctrineTest.php @@ -0,0 +1,39 @@ +markTestSkipped('Doctrine is not installed'); + } + + $client = static::createPantherClient(); + + $client->request('GET', '/demo/bridge/doctrine'); + + // Wait for Debugbar to load + $crawler = $client->waitFor('.phpdebugbar-body'); + usleep(1000); + + if (!$this->isTabActive($crawler, 'database')) { + $client->click($this->getTabLink($crawler, 'database')); + } + + $crawler = $client->waitForVisibility('.phpdebugbar-panel[data-collector=database]'); + + $statements = $crawler->filter('.phpdebugbar-panel[data-collector=database] .phpdebugbar-widgets-sql') + ->each(function($node){ + return $node->getText(); + }); + + $this->assertEquals('INSERT INTO products (name) VALUES (?)', $statements[1]); + $this->assertCount(3, $statements); + } + +} \ No newline at end of file diff --git a/tests/DebugBar/Tests/Browser/Bridge/MonologTest.php b/tests/DebugBar/Tests/Browser/Bridge/MonologTest.php new file mode 100644 index 0000000..2ab28cf --- /dev/null +++ b/tests/DebugBar/Tests/Browser/Bridge/MonologTest.php @@ -0,0 +1,38 @@ +markTestSkipped('Monolog is not installed'); + } + + $client = static::createPantherClient(); + + $client->request('GET', '/demo/bridge/monolog'); + + // Wait for Debugbar to load + $crawler = $client->waitFor('.phpdebugbar-body'); + usleep(1000); + + if (!$this->isTabActive($crawler, 'monolog')) { + $client->click($this->getTabLink($crawler, 'monolog')); + } + + $crawler = $client->waitForVisibility('.phpdebugbar-panel[data-collector=monolog]'); + + $messages = $crawler->filter('.phpdebugbar-panel[data-collector=monolog] .phpdebugbar-widgets-value') + ->each(function($node){ + return $node->getText(); + }); + + $this->assertStringContainsString('demo.INFO: hello world [] []', $messages[0]); + } + +} \ No newline at end of file diff --git a/tests/DebugBar/Browser/BasicBrowserTest.php b/tests/DebugBar/Tests/Browser/DebugbarTest.php similarity index 83% rename from tests/DebugBar/Browser/BasicBrowserTest.php rename to tests/DebugBar/Tests/Browser/DebugbarTest.php index 4e11b59..bf6a122 100644 --- a/tests/DebugBar/Browser/BasicBrowserTest.php +++ b/tests/DebugBar/Tests/Browser/DebugbarTest.php @@ -1,16 +1,10 @@ waitFor('.phpdebugbar-body'); - $client->click($this->getTabLink($crawler, 'messages')); + usleep(1000); + if (!$this->isTabActive($crawler, 'messages')) { + $client->click($this->getTabLink($crawler, 'messages')); + } $crawler = $client->waitForVisibility('.phpdebugbar-panel[data-collector=messages] .phpdebugbar-widgets-list'); @@ -44,8 +41,11 @@ class BasicBrowserTest extends AbstractBrowserTest // Wait for Debugbar to load $crawler = $client->waitFor('.phpdebugbar-body'); + usleep(1000); - $client->click($this->getTabLink($crawler, 'messages')); + if (!$this->isTabActive($crawler, 'messages')) { + $client->click($this->getTabLink($crawler, 'messages')); + } $crawler = $client->waitForVisibility('.phpdebugbar-widgets-messages .phpdebugbar-widgets-list');