Added SQL and iterators tests.
This commit is contained in:
1 parent
420305143b
commit
61dd387484
4 files changed
+146
No files matched your search
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
use Rarst\Laps\Record\Recursive_Record_Iterator;
|
||||
use Rarst\Laps\Record\Timeline_Iterator;
|
||||
use Rarst\Laps\Tests\LapsTestCase;
|
||||
use Rarst\Laps\Tests\Record\StubRecord;
|
||||
|
||||
class IteratorTest extends LapsTestCase {
|
||||
|
||||
public function testRecord() {
|
||||
|
||||
$record_a = new StubRecord( 'Parent', 0, 100 );
|
||||
$record_b = new StubRecord( 'Child', 50, 100 );
|
||||
|
||||
$records = [ $record_b, $record_a ];
|
||||
|
||||
$iterator = new Recursive_Record_Iterator( $records );
|
||||
|
||||
$this->assertCount( 1, $iterator->getArrayCopy() );
|
||||
|
||||
foreach ( $iterator as $record ) {
|
||||
$this->assertEquals( 'Parent', $record->get_name() );
|
||||
}
|
||||
|
||||
$this->assertTrue( $iterator->hasChildren() );
|
||||
$children = $iterator->getChildren();
|
||||
$this->assertCount( 1, $children );
|
||||
|
||||
foreach ( $children as $record ) {
|
||||
$this->assertEquals( 'Child', $record->get_name() );
|
||||
}
|
||||
|
||||
return $iterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testRecord
|
||||
*/
|
||||
public function testTimeline( Recursive_Record_Iterator $iterator ) {
|
||||
|
||||
$iterator = new Timeline_Iterator( $iterator );
|
||||
|
||||
$iterator->rewind();
|
||||
$this->assertTrue( $iterator->valid() );
|
||||
|
||||
$records = $iterator->current();
|
||||
$this->assertInternalType( 'array', $records );
|
||||
|
||||
$this->assertArrayHasKey( 'description', $records[0] );
|
||||
$this->assertArrayHasKey( 'category', $records[0] );
|
||||
$this->assertArrayHasKey( 'offset', $records[0] );
|
||||
$this->assertArrayHasKey( 'width', $records[0] );
|
||||
|
||||
$iterator->next();
|
||||
$this->assertTrue( $iterator->valid() );
|
||||
|
||||
$records = $iterator->current();
|
||||
$this->assertInternalType( 'array', $records );
|
||||
|
||||
$iterator->next();
|
||||
$this->assertFalse( $iterator->valid() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Rarst\Laps\Tests\Record;
|
||||
|
||||
use Rarst\Laps\Record\Sql_Record;
|
||||
use Rarst\Laps\Record\Sql_Record_Collector;
|
||||
use Rarst\Laps\Tests\LapsTestCase;
|
||||
|
||||
class SqlTest extends LapsTestCase {
|
||||
|
||||
public function testCollector() {
|
||||
|
||||
define( 'SAVEQUERIES', true );
|
||||
|
||||
$collector = new Sql_Record_Collector();
|
||||
|
||||
$this->assertTrue( has_filter( 'query', [ $collector, 'query' ] ) );
|
||||
|
||||
$query = 'SELECT * FROM wp_posts';
|
||||
|
||||
$collector->query( $query );
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$wpdb = new \stdClass();
|
||||
$duration = 100;
|
||||
$wpdb->queries[] = [ $query, $duration ];
|
||||
|
||||
$records = $collector->get_records();
|
||||
|
||||
$this->assertInternalType( 'array', $records );
|
||||
$this->assertCount( 1, $records );
|
||||
|
||||
$record = $records[0];
|
||||
|
||||
$this->assertInstanceOf( Sql_Record::class, $record );
|
||||
$this->assertEquals( $query, $record->get_name() );
|
||||
$this->assertContains( $query, $record->get_description() );
|
||||
$this->assertInternalType( 'float', $record->get_origin() );
|
||||
$this->assertEquals( $duration * 1000, $record->get_duration() );
|
||||
$this->assertEquals( 'query-read', $record->get_category() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Rarst\Laps\Tests\Record;
|
||||
|
||||
use Rarst\Laps\Record\Record_Interface;
|
||||
|
||||
class StubRecord implements Record_Interface {
|
||||
|
||||
protected $name;
|
||||
protected $origin;
|
||||
protected $duration;
|
||||
|
||||
public function __construct( $name, $origin, $duration ) {
|
||||
$this->name = $name;
|
||||
$this->origin = $origin;
|
||||
$this->duration = $duration;
|
||||
}
|
||||
|
||||
public function get_name() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function get_description() {
|
||||
|
||||
}
|
||||
|
||||
public function get_origin() {
|
||||
return $this->origin;
|
||||
}
|
||||
|
||||
public function get_duration() {
|
||||
return $this->duration;
|
||||
}
|
||||
|
||||
public function get_category() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user