Skip to content

Commit

Permalink
Refactor browser test, test monolog bridge (#641)
Browse files Browse the repository at this point in the history
* Refactor browser test, test monolog bridge

* Rename

* Add Doctrine
  • Loading branch information
barryvdh committed Mar 31, 2024
1 parent 0bf3a5f commit f9b5949
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 25 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/run-integration.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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/
.phpunit.cache/
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
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`
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"autoload-dev": {
"psr-4": {
"DebugBar\\": "tests/DebugBar/"
"DebugBar\\Tests\\": "tests/DebugBar/Tests"
}
},
"suggest": {
Expand Down
2 changes: 2 additions & 0 deletions demo/bridge/doctrine/cli-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));

return $helperSet;
4 changes: 4 additions & 0 deletions demo/bridge/doctrine/composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
8 changes: 6 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="tests/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="DebugBar Test Suite">
<directory>./tests/DebugBar/</directory>
<testsuite name="Unit">
<directory>./tests/DebugBar/Tests</directory>
<exclude>./tests/DebugBar/Tests/Browser</exclude>
</testsuite>
<testsuite name="Browser">
<directory>./tests/DebugBar/Tests/Browser</directory>
</testsuite>
</testsuites>
<extensions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<?php

namespace DebugBar\Tests;
namespace DebugBar\Tests\Browser;

use DebugBar\DebugBar;
use DebugBar\DebugBarException;
use DebugBar\Tests\DataCollector\MockCollector;
use DebugBar\Tests\Storage\MockStorage;
use DebugBar\RandomRequestIdGenerator;
use Facebook\WebDriver\WebDriverElement;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Panther\DomCrawler\Link;
use Symfony\Component\Panther\PantherTestCase;
Expand All @@ -18,7 +12,7 @@ public function isTabActive(Crawler $crawler, $tab)
{
$node = $crawler->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
Expand Down
39 changes: 39 additions & 0 deletions tests/DebugBar/Tests/Browser/Bridge/DoctrineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace DebugBar\Tests\Browser\Bridge;

use DebugBar\Browser\Bridge\WebDriverElement;
use DebugBar\Tests\Browser\AbstractBrowserTest;

class DoctrineTest extends AbstractBrowserTest
{
public function testMonologCollector(): void
{
if (!file_exists(__DIR__ . '/../../../../../demo/bridge/doctrine/vendor/autoload.php')) {
$this->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);
}

}
38 changes: 38 additions & 0 deletions tests/DebugBar/Tests/Browser/Bridge/MonologTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace DebugBar\Tests\Browser\Bridge;

use DebugBar\Browser\Bridge\WebDriverElement;
use DebugBar\Tests\Browser\AbstractBrowserTest;

class MonologTest extends AbstractBrowserTest
{
public function testMonologCollector(): void
{
if (!file_exists(__DIR__ . '/../../../../../demo/bridge/monolog/vendor/autoload.php')) {
$this->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]);
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?php

namespace DebugBar\Tests;
namespace DebugBar\Tests\Browser;

use DebugBar\DebugBar;
use DebugBar\DebugBarException;
use DebugBar\Tests\DataCollector\MockCollector;
use DebugBar\Tests\Storage\MockStorage;
use DebugBar\RandomRequestIdGenerator;
use Facebook\WebDriver\WebDriverElement;
use Symfony\Component\Panther\PantherTestCase;

class BasicBrowserTest extends AbstractBrowserTest
class DebugbarTest extends AbstractBrowserTest
{
public function testDebugbarTab(): void
{
Expand All @@ -20,7 +14,10 @@ public function testDebugbarTab(): void
// Wait for Debugbar to load
$crawler = $client->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');

Expand All @@ -44,8 +41,11 @@ public function testDebugbarAjax(): void

// 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');

Expand Down

0 comments on commit f9b5949

Please sign in to comment.