Skip to content

Commit

Permalink
Add ajax test (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh authored Mar 31, 2024
1 parent 2bfe379 commit 0bf3a5f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.lock
/demo/bridge/*/vendor
/src/DebugBar/Resources/vendor
.phpunit.result.cache
/drivers
/drivers
.phpunit.cache/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ 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/
2 changes: 2 additions & 0 deletions src/DebugBar/Resources/debugbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ if (typeof(PhpDebugBar) == 'undefined') {
self.showTab(name);
}
});
tab.$tab.attr('data-collector', name);
tab.$el.attr('data-collector', name);
tab.$el.appendTo(this.$body);

this.controls[name] = tab;
Expand Down
28 changes: 28 additions & 0 deletions tests/DebugBar/Browser/AbstractBrowserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace DebugBar\Tests;

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;

abstract class AbstractBrowserTest extends PantherTestCase
{
public function isTabActive(Crawler $crawler, $tab)
{
$node = $crawler->filter('a.phpdebugbar-tab[data-collector="'.$tab.'"]');

return strpos($node->attr('class'), 'phpdebugbar-active"') !== false;
}

public function getTabLink(Crawler $crawler, $tab): Link
{
return $crawler->filter('a.phpdebugbar-tab[data-collector="'.$tab.'"]')->link();
}
}
51 changes: 40 additions & 11 deletions tests/DebugBar/Browser/BasicBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,62 @@
use Facebook\WebDriver\WebDriverElement;
use Symfony\Component\Panther\PantherTestCase;

class BasicBrowserTest extends PantherTestCase
class BasicBrowserTest extends AbstractBrowserTest
{
public function testDebugbar(): void
public function testDebugbarTab(): void
{
// Start demo
$client = static::createPantherClient();
$client->request('GET', '/demo');
$crawler = $client->request('GET', '/demo/');

// Wait for Debugbar to load
$crawler = $client->waitFor('.phpdebugbar-body');

$firstTab = $crawler->filter('a.phpdebugbar-tab')->link();
$client->click($firstTab);
$client->click($this->getTabLink($crawler, 'messages'));

$crawler = $client->waitForVisibility('.phpdebugbar-widgets-messages .phpdebugbar-widgets-list');
$crawler = $client->waitForVisibility('.phpdebugbar-panel[data-collector=messages] .phpdebugbar-widgets-list');

$messages = $crawler->filter('.phpdebugbar-widgets-messages .phpdebugbar-widgets-value')
$messages = $crawler->filter('.phpdebugbar-panel[data-collector=messages] .phpdebugbar-widgets-value')
->each(function(WebDriverElement $node){
return $node->getText();
});

$this->assertEquals('hello', $messages[0]);
$this->assertCount(4, $messages);

$firstTab = $crawler->filter('a.phpdebugbar-tab')->link();
$client->click($firstTab);
$client->waitForInvisibility('.phpdebugbar-widgets-messages .phpdebugbar-widgets-list');
// Close it again
$client->click($this->getTabLink($crawler, 'messages'));
$client->waitForInvisibility('.phpdebugbar-panel[data-collector=messages] .phpdebugbar-widgets-list');
}

public function testDebugbarAjax(): void
{
$client = static::createPantherClient();
$crawler = $client->request('GET', '/demo/');

// Wait for Debugbar to load
$crawler = $client->waitFor('.phpdebugbar-body');

$client->click($this->getTabLink($crawler, 'messages'));

$crawler = $client->waitForVisibility('.phpdebugbar-widgets-messages .phpdebugbar-widgets-list');

$crawler->selectLink('load ajax content')->click();
$client->waitForElementToContain('.phpdebugbar-panel[data-collector=messages]', 'hello from ajax');
$client->waitForElementToContain('.phpdebugbar-datasets-switcher', 'ajax.php');

$messages = $crawler->filter('.phpdebugbar-panel[data-collector=messages] .phpdebugbar-widgets-value')
->each(function(WebDriverElement $node){
return $node->getText();
});

$this->assertEquals('hello from ajax', $messages[0]);

$crawler->selectLink('load ajax content with exception')->click();

$client->click($this->getTabLink($crawler, 'exceptions'));

$client->waitForElementToContain('.phpdebugbar-datasets-switcher', 'ajax_exception.php');
$client->waitForElementToContain('.phpdebugbar-panel[data-collector=exceptions] .phpdebugbar-widgets-message', 'Something failed!');
}

}

0 comments on commit 0bf3a5f

Please sign in to comment.