Skip to content

Commit

Permalink
Bundles: added type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Jan 7, 2024
1 parent f973494 commit a415913
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 54 deletions.
12 changes: 6 additions & 6 deletions src/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ public static function getCachedRenderer()
*
* @return Parser
*/
abstract public static function getParser();
abstract public static function getParser(): Parser;

/**
* Return a new instance of s9e\TextFormatter\Renderer
*
* @return Renderer
*/
abstract public static function getRenderer();
abstract public static function getRenderer(): Renderer;

/**
* Return the source of the JavaScript parser if available
*
* @return string
*/
public static function getJS()
public static function getJS(): string
{
return '';
}
Expand All @@ -69,7 +69,7 @@ public static function getJS()
* @param string $text Original text
* @return string Intermediate representation
*/
public static function parse($text)
public static function parse($text): string
{
if (isset(static::$beforeParse))
{
Expand All @@ -93,7 +93,7 @@ public static function parse($text)
* @param array $params Stylesheet parameters
* @return string Rendered result
*/
public static function render($xml, array $params = [])
public static function render($xml, array $params = []): string
{
$renderer = static::getCachedRenderer();

Expand Down Expand Up @@ -122,7 +122,7 @@ public static function render($xml, array $params = [])
*
* @return void
*/
public static function reset()
public static function reset(): void
{
static::$parser = null;
static::$renderer = null;
Expand Down
14 changes: 7 additions & 7 deletions src/Bundles/Fatdown.php

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/Bundles/Forum.php

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/Bundles/MediaPack.php

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/Configurator/BundleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public function generate($className, array $options = [])
$php[] = 'abstract class ' . $className . ' extends \\s9e\\TextFormatter\\Bundle';
$php[] = '{';
$php[] = ' /**';
$php[] = ' * @var s9e\\TextFormatter\\Parser Singleton instance used by parse()';
$php[] = ' * @var ?\\s9e\\TextFormatter\\Parser Singleton instance used by parse()';
$php[] = ' */';
$php[] = ' protected static $parser;';
$php[] = ' protected static ?\\s9e\\TextFormatter\\Parser $parser;';
$php[] = '';
$php[] = ' /**';
$php[] = ' * @var s9e\\TextFormatter\\Renderer Singleton instance used by render()';
$php[] = ' * @var ?\\s9e\\TextFormatter\\Renderer Singleton instance used by render()';
$php[] = ' */';
$php[] = ' protected static $renderer;';
$php[] = ' protected static ?\\s9e\\TextFormatter\\Renderer $renderer;';
$php[] = '';

// Add the event callbacks if applicable
Expand Down Expand Up @@ -128,7 +128,7 @@ public function generate($className, array $options = [])
$php[] = ' /**';
$php[] = ' * {@inheritdoc}';
$php[] = ' */';
$php[] = ' public static function getJS()';
$php[] = ' public static function getJS(): string';
$php[] = ' {';
$php[] = ' return ' . var_export($objects['js'], true) . ';';
$php[] = ' }';
Expand All @@ -138,7 +138,7 @@ public function generate($className, array $options = [])
$php[] = ' /**';
$php[] = ' * {@inheritdoc}';
$php[] = ' */';
$php[] = ' public static function getParser()';
$php[] = ' public static function getParser(): \\s9e\\TextFormatter\\Parser';
$php[] = ' {';

if (isset($options['parserSetup']))
Expand All @@ -158,7 +158,7 @@ public function generate($className, array $options = [])
$php[] = ' /**';
$php[] = ' * {@inheritdoc}';
$php[] = ' */';
$php[] = ' public static function getRenderer()';
$php[] = ' public static function getRenderer(): \\s9e\\TextFormatter\\Renderer';
$php[] = ' {';

// If this is a PHP renderer and we know where it's saved, automatically load it as needed
Expand Down
65 changes: 45 additions & 20 deletions tests/BundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace s9e\TextFormatter\Tests;

use s9e\TextFormatter\Bundle;
use s9e\TextFormatter\Parser;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Tests\Test;

/**
Expand All @@ -15,6 +17,22 @@ protected function setUp(): void
DummyBundle::_reset($this);
}

protected function getMockParser(): Parser
{
return $this->getMockBuilder(Parser::class)
->disableOriginalConstructor()
->onlyMethods(['parse'])
->getMock();
}

protected function getMockRenderer(): Renderer
{
return $this->getMockBuilder(Renderer::class)
->disableOriginalConstructor()
->onlyMethods(['render', 'renderRichText', 'setParameters'])
->getMock();
}

/**
* @testdox The default implementation for getJS() returns an empty string
*/
Expand All @@ -23,6 +41,7 @@ public function testGetJS()
$this->assertSame('', DummyBundle::getJS());
}


/**
* @testdox parse() creates a parser, parses the input and returns the output
*/
Expand All @@ -31,7 +50,7 @@ public function testParse()
$text = 'Hello world';
$xml = '<t>Hello world</t>';

$mock = $this->getMockBuilder('stdClass')->addMethods(['parse'])->getMock();
$mock = $this->getMockParser();
$mock->expects($this->once())
->method('parse')
->with($text)
Expand All @@ -54,7 +73,7 @@ public function testParseSingleton()
$text2 = 'Sup Earth :)';
$xml2 = '<r>Sup Earth <E>:)</e></r>';

$mock = $this->getMockBuilder('stdClass')->addMethods(['parse'])->getMock();
$mock = $this->getMockParser();
$mock->expects($this->exactly(2))
->method('parse')
->willReturnOnConsecutiveCalls($xml1, $xml2);
Expand All @@ -71,7 +90,7 @@ public function testParseSingleton()
*/
public function testBeforeParse()
{
$mock = $this->getMockBuilder('stdClass')->addMethods(['parse'])->getMock();
$mock = $this->getMockParser();
$mock->expects($this->once())
->method('parse')
->with('beforeParse')
Expand All @@ -93,7 +112,7 @@ public function testBeforeParse()
*/
public function testAfterParse()
{
$mock = $this->getMockBuilder('stdClass')->addMethods(['parse'])->getMock();
$mock = $this->getMockParser();
$mock->expects($this->once())
->method('parse')
->with('')
Expand All @@ -118,7 +137,7 @@ public function testRender()
$xml = '<t>Hello world</t>';
$html = 'Hello world';

$mock = $this->getMockBuilder('stdClass')->addMethods(['render', 'setParameters'])->getMock();
$mock = $this->getMockRenderer();
$mock->expects($this->once())
->method('render')
->with($xml)
Expand All @@ -138,7 +157,7 @@ public function testRenderSingleton()
$xml = '<t>Hello world</t>';
$html = 'Hello world';

$mock = $this->getMockBuilder('stdClass')->addMethods(['render', 'setParameters'])->getMock();
$mock = $this->getMockRenderer();
$mock->expects($this->exactly(2))
->method('render')
->with($xml)
Expand All @@ -160,7 +179,7 @@ public function testRenderParameters()
$html = 'Hello world';
$params = ['foo' => 'bar'];

$mock = $this->getMockBuilder('stdClass')->addMethods(['render', 'setParameters'])->getMock();
$mock = $this->getMockRenderer();
$mock->expects($this->once())
->method('render')
->with($xml)
Expand All @@ -180,7 +199,7 @@ public function testRenderParameters()
*/
public function testBeforeRender()
{
$mock = $this->getMockBuilder('stdClass')->addMethods(['render'])->getMock();
$mock = $this->getMockRenderer();
$mock->expects($this->once())
->method('render')
->with('<t>beforeRender</t>')
Expand All @@ -202,7 +221,7 @@ public function testBeforeRender()
*/
public function testAfterRender()
{
$mock = $this->getMockBuilder('stdClass')->addMethods(['render'])->getMock();
$mock = $this->getMockRenderer();
$mock->expects($this->once())
->method('render')
->with('')
Expand Down Expand Up @@ -291,21 +310,27 @@ class DummyBundle extends Bundle
public static $beforeUnparse;
public static $afterUnparse;

public static function _reset(Test $test)
public static function _reset(Test $test): void
{
self::$calls = ['getParser' => 0, 'getRenderer' => 0];

$mock = $test->getMockBuilder('stdClass')
->addMethods(['parse', 'render', 'setParameters'])
->getMock();
$mock->expects($test->never())->method('parse');
$mock->expects($test->never())->method('render');
$mock->expects($test->never())->method('setParameters');
$parser = $test->getMockBuilder(Parser::class)
->disableOriginalConstructor()
->onlyMethods(['parse'])
->getMock();
$parser->expects($test->never())->method('parse');

$renderer = $test->getMockBuilder(Renderer::class)
->disableOriginalConstructor()
->onlyMethods(['render', 'renderRichText', 'setParameters'])
->getMock();
$renderer->expects($test->never())->method('render');
$renderer->expects($test->never())->method('setParameters');

static::$parser = null;
static::$_parser = $mock;
static::$_parser = $parser;
static::$renderer = null;
static::$_renderer = $mock;
static::$_renderer = $renderer;

static::$beforeParse = null;
static::$afterParse = null;
Expand All @@ -315,14 +340,14 @@ public static function _reset(Test $test)
static::$afterUnparse = null;
}

public static function getParser()
public static function getParser(): Parser
{
++self::$calls['getParser'];

return self::$_parser;
}

public static function getRenderer()
public static function getRenderer(): Renderer
{
++self::$calls['getRenderer'];

Expand Down

0 comments on commit a415913

Please sign in to comment.