1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-17 05:18:32 +01:00

added unit tests

This commit is contained in:
maximebf 2013-08-14 21:37:24 +10:00
parent d9a37e9e84
commit fb1605e93c
9 changed files with 362 additions and 0 deletions

6
.travis.yml Normal file
View File

@ -0,0 +1,6 @@
language: php
php:
- 5.3
- 5.4
before_script: composer install
script: phpunit

View File

@ -0,0 +1,58 @@
<?php
namespace DebugBar\Tests\DataCollector;
use DebugBar\Tests\DebugBarTestCase;
use DebugBar\DataCollector\AggregatedCollector;
class AggregatedCollectorTest extends DebugBarTestCase
{
public function setUp()
{
$this->c = new AggregatedCollector('test');
}
public function testAddCollector()
{
$this->c->addCollector($c = new MockCollector());
$this->assertContains($c, $this->c->getCollectors());
$this->assertEquals($c, $this->c['mock']);
$this->assertTrue(isset($this->c['mock']));
}
public function testCollect()
{
$this->c->addCollector(new MockCollector(array('foo' => 'bar'), 'm1'));
$this->c->addCollector(new MockCollector(array('bar' => 'foo'), 'm2'));
$data = $this->c->collect();
$this->assertCount(2, $data);
$this->assertArrayHasKey('foo', $data);
$this->assertEquals('bar', $data['foo']);
$this->assertArrayHasKey('bar', $data);
$this->assertEquals('foo', $data['bar']);
}
public function testMergeProperty()
{
$this->c->addCollector(new MockCollector(array('foo' => array('a' => 'b')), 'm1'));
$this->c->addCollector(new MockCollector(array('foo' => array('c' => 'd')), 'm2'));
$this->c->setMergeProperty('foo');
$data = $this->c->collect();
$this->assertCount(2, $data);
$this->assertArrayHasKey('a', $data);
$this->assertEquals('b', $data['a']);
$this->assertArrayHasKey('c', $data);
$this->assertEquals('d', $data['c']);
}
public function testSort()
{
$this->c->addCollector(new MockCollector(array(array('foo' => 2, 'id' => 1)), 'm1'));
$this->c->addCollector(new MockCollector(array(array('foo' => 1, 'id' => 2)), 'm2'));
$this->c->setSort('foo');
$data = $this->c->collect();
$this->assertCount(2, $data);
$this->assertEquals(2, $data[0]['id']);
$this->assertEquals(1, $data[1]['id']);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace DebugBar\Tests\DataCollector;
use DebugBar\Tests\DebugBarTestCase;
use DebugBar\DebugBar;
use DebugBar\DataCollector\ConfigCollector;
class ConfigCollectorTest extends DebugBarTestCase
{
public function testCollect()
{
$c = new ConfigCollector(array('s' => 'bar', 'a' => array(), 'o' => new \stdClass()));
$data = $c->collect();
$this->assertArrayHasKey('s', $data);
$this->assertEquals('bar', $data['s']);
$this->assertArrayHasKey('a', $data);
$this->assertEquals("Array\n(\n)\n", $data['a']);
$this->assertArrayHasKey('o', $data);
$this->assertEquals('Object(stdClass)', $data['o']);
}
public function testName()
{
$c = new ConfigCollector(array(), 'foo');
$this->assertEquals('foo', $c->getName());
$this->assertArrayHasKey('foo', $c->getWidgets());
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace DebugBar\Tests\DataCollector;
use DebugBar\Tests\DebugBarTestCase;
use DebugBar\DataCollector\MessagesCollector;
class MessagesCollectorTest extends DebugBarTestCase
{
public function testAddMessageAndLog()
{
$c = new MessagesCollector();
$c->addMessage('foobar');
$msgs = $c->getMessages();
$this->assertCount(1, $msgs);
$c->log('notice', 'hello');
$this->assertCount(2, $c->getMessages());
}
public function testAggregate()
{
$a = new MessagesCollector('a');
$c = new MessagesCollector('c');
$c->aggregate($a);
$c->addMessage('message from c');
$a->addMessage('message from a');
$msgs = $c->getMessages();
$this->assertCount(2, $msgs);
$this->assertArrayHasKey('collector', $msgs[1]);
$this->assertEquals('a', $msgs[1]['collector']);
}
public function testCollect()
{
$c = new MessagesCollector();
$c->addMessage('foo');
$data = $c->collect();
$this->assertEquals(1, $data['count']);
$this->assertEquals($c->getMessages(), $data['messages']);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace DebugBar\Tests\DataCollector;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
class MockCollector extends DataCollector implements Renderable
{
protected $data;
protected $name;
protected $widgets;
public function __construct($data = array(), $name = 'mock', $widgets = array())
{
$this->data = $data;
$this->name = $name;
$this->widgets = $widgets;
}
public function collect()
{
return $this->data;
}
public function getName()
{
return $this->name;
}
public function getWidgets()
{
return $this->widgets;
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace DebugBar\Tests\DataCollector;
use DebugBar\Tests\DebugBarTestCase;
use DebugBar\DataCollector\TimeDataCollector;
class TimeDataCollectorTest extends DebugBarTestCase
{
public function setUp()
{
$this->s = microtime(true);
$this->c = new TimeDataCollector($this->s);
}
public function testAddMeasure()
{
$this->c->addMeasure('foo', $this->s, $this->s + 10);
$m = $this->c->getMeasures();
$this->assertCount(1, $m);
$this->assertEquals('foo', $m[0]['label']);
$this->assertEquals(10, $m[0]['duration']);
}
public function testStartStopMeasure()
{
$this->c->startMeasure('foo');
$this->c->stopMeasure('foo');
$m = $this->c->getMeasures();
$this->assertCount(1, $m);
$this->assertEquals('foo', $m[0]['label']);
$this->assertTrue($m[0]['start'] < $m[0]['end']);
}
public function testCollect()
{
$this->c->addMeasure('foo', 0, 10);
$this->c->addMeasure('bar', 10, 20);
$data = $this->c->collect();
$this->assertTrue($data['end'] > $this->s);
$this->assertTrue($data['duration'] > 0);
$this->assertCount(2, $data['measures']);
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace DebugBar\Tests;
use DebugBar\DebugBar;
use DebugBar\Tests\DataCollector\MockCollector;
class DebugBarTest extends DebugBarTestCase
{
public function setUp()
{
$this->debugbar = new DebugBar();
}
public function testAddCollector()
{
$this->debugbar->addCollector($c = new MockCollector());
$this->assertTrue($this->debugbar->hasCollector('mock'));
$this->assertEquals($c, $this->debugbar->getCollector('mock'));
$this->assertContains($c, $this->debugbar->getCollectors());
}
/**
* @expectedException \DebugBar\DebugBarException
*/
public function testAddCollectorWithSameName()
{
$this->debugbar->addCollector(new MockCollector());
$this->debugbar->addCollector(new MockCollector());
}
public function testCollect()
{
$data = array('foo' => 'bar');
$this->debugbar->addCollector(new MockCollector($data));
$datac = $this->debugbar->collect();
$this->assertArrayHasKey('mock', $datac);
$this->assertEquals($datac['mock'], $data);
$this->assertEquals($datac, $this->debugbar->getData());
}
public function testArrayAccess()
{
$this->debugbar->addCollector($c = new MockCollector());
$this->assertEquals($c, $this->debugbar['mock']);
$this->assertTrue(isset($this->debugbar['mock']));
$this->assertFalse(isset($this->debugbar['foo']));
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace DebugBar\Tests;
use DebugBar\JavascriptRenderer;
use DebugBar\DebugBar;
use DebugBar\StandardDebugBar;
class JavascriptRendererTest extends DebugBarTestCase
{
public function setUp()
{
$this->debugbar = new DebugBar();
$this->r = new JavascriptRenderer($this->debugbar);
}
public function testOptions()
{
$this->r->setOptions(array(
'base_path' => '/foo',
'base_url' => '/foo',
'include_vendors' => false,
'javascript_class' => 'Foobar',
'variable_name' => 'foovar',
'initialization' => JavascriptRenderer::INITIALIZE_CONTROLS,
'controls' => array(
'memory' => array(
"icon" => "cogs",
"map" => "memory.peak_usage_str",
"default" => "'0B'"
)
),
'disable_controls' => array('messages'),
'ignore_collectors' => 'config'
));
$this->assertEquals('/foo', $this->r->getBasePath());
$this->assertEquals('/foo', $this->r->getBaseUrl());
$this->assertFalse($this->r->areVendorsIncluded());
$this->assertEquals('Foobar', $this->r->getJavascriptClass());
$this->assertEquals('foovar', $this->r->getVariableName());
$this->assertEquals(JavascriptRenderer::INITIALIZE_CONTROLS, $this->r->getInitialization());
$controls = $this->r->getControls();
$this->assertCount(2, $controls);
$this->assertArrayHasKey('memory', $controls);
$this->assertArrayHasKey('messages', $controls);
$this->assertNull($controls['messages']);
$this->assertContains('config', $this->r->getIgnoredCollectors());
}
public function testGetAssets()
{
$this->r->setBasePath('/foo');
list($css, $js) = $this->r->getAssets();
$this->assertContains('/foo/debugbar.css', $css);
$this->assertContains('/foo/widgets.js', $js);
$this->assertContains('/foo/vendor/jquery-1.8.3.min.js', $js);
$this->r->setIncludeVendors(false);
$js = $this->r->getAssets('js');
$this->assertContains('/foo/debugbar.js', $js);
$this->assertNotContains('/foo/vendor/jquery-1.8.3.min.js', $js);
}
public function testRenderHead()
{
$this->r->setBaseUrl('/foo');
$html = $this->r->renderHead();
$this->assertTag(array('tag' => 'script', 'attributes' => array('src' => '/foo/debugbar.js')), $html);
}
public function testRenderFullInitialization()
{
$this->debugbar->addCollector(new \DebugBar\DataCollector\MessagesCollector());
$this->r->addControl('time', array('icon' => 'time', 'map' => 'time', 'default' => '"0s"'));
$this->assertStringEqualsFile(__DIR__ . '/full_init.html', $this->r->render());
}
public function testRenderConstructorOnly()
{
$this->r->setInitialization(JavascriptRenderer::INITIALIZE_CONSTRUCTOR);
$this->r->setJavascriptClass('Foobar');
$this->r->setVariableName('foovar');
$this->assertEquals("<script type=\"text/javascript\">\nvar foovar = new Foobar();\nfoovar.addDataSet([]);\n\n</script>\n", $this->r->render());
}
}

View File

@ -0,0 +1,13 @@
<script type="text/javascript">
var phpdebugbar = new PhpDebugBar.DebugBar();
phpdebugbar.addTab("messages", new PhpDebugBar.DebugBar.Tab({"title":"Messages", "widget": new PhpDebugBar.Widgets.MessagesWidget()}));
phpdebugbar.addIndicator("time", new PhpDebugBar.DebugBar.Indicator({"icon":"time"}));
phpdebugbar.setDataMap({
"messages": ["messages.messages", []],
"messages:badge": ["messages.count", null],
"time": ["time", "0s"]
});
phpdebugbar.restoreState();
phpdebugbar.addDataSet({"messages":{"count":0,"messages":[]}});
</script>