Files
sandros fad92f739c
Tests / tests (push) Successful in 5s
Release / upload-asset (push) Successful in 4s
Fix Hook_Formatter::format to accept WP_Hook object as argument
2026-09-27 23:12:44 +02:00

84 lines
2.0 KiB
PHP

<?php
declare( strict_types=1 );
namespace Rarst\Laps\Tests\Formatter;
use Brain\Monkey\Functions;
use Rarst\Laps\Formatter\Hook_Formatter;
use Rarst\Laps\Tests\LapsTestCase;
/**
* @coversDefaultClass \Rarst\Laps\Formatter\Hook_Formatter
*/
class HookTest extends LapsTestCase {
/**
* @covers ::__construct
*
* @return Hook_Formatter
*/
public function test__construct() {
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', '/wp' );
define( 'WP_CONTENT_DIR', '/wp-content/' );
}
Functions\expect( 'wp_normalize_path' )->zeroOrMoreTimes()->andReturnFirstArg();
return new Hook_Formatter();
}
/**
* @depends test__construct
*/
public function testFormat( Hook_Formatter $formatter ) {
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', '/wp' );
define( 'WP_CONTENT_DIR', '/wp-content/' );
}
Functions\expect( 'wp_normalize_path' )->zeroOrMoreTimes()->andReturnFirstArg();
$hook = [
[ [ 'function' => 'function', 'accepted_args' => 2 ] ],
[ [ 'function' => [ 'class', 'method' ], 'accepted_args' => 1 ] ],
[ [ 'function' => function() {}, 'accepted_args' => 1 ] ],
[ [ 'function' => new class(){}, 'accepted_args' => 1 ] ],
];
$result = $formatter->format( $hook );
$this->assertIsArray( $result );
$this->assertEquals( 'function(2)', $result[0] );
$this->assertEquals( 'class::method', $result[1] );
$this->assertStringStartsWith( 'closure from', $result[2] );
$this->assertStringStartsWith( 'anonymous class from', $result[3] );
$this->assertEquals( [], $formatter->format( [] ) );
}
/**
* @depends test__construct
*/
public function testFormatWpHook( Hook_Formatter $formatter ) {
if ( ! class_exists( 'WP_Hook' ) ) {
eval( 'class WP_Hook { public $callbacks = []; }' );
}
$wp_hook = new \WP_Hook();
$wp_hook->callbacks = [
10 => [
[ 'function' => 'trim', 'accepted_args' => 1 ],
],
];
$result = $formatter->format( $wp_hook );
$this->assertIsArray( $result );
$this->assertEquals( [ 'trim' ], $result );
}
}