Skip to content

Commit

Permalink
Import classes rather than using fully qualfied strings
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Oct 30, 2024
1 parent 9854372 commit bd01721
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 50 deletions.
27 changes: 15 additions & 12 deletions tests/CLImateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use League\CLImate\Exceptions\InvalidArgumentException;
use League\CLImate\Exceptions\UnexpectedValueException;
use League\CLImate\Tests\CustomObject\Basic;
use League\CLImate\Tests\CustomObject\BasicObject;
use League\CLImate\Tests\CustomObject\BasicObjectArgument;
use League\CLImate\Tests\CustomObject\Dummy;
use League\CLImate\Tests\CustomObject\Dynamic;

class CLImateTest extends TestBase
{
Expand Down Expand Up @@ -57,7 +60,7 @@ public function it_can_be_extended_using_a_basic_object_as_string()
$this->shouldWrite("\e[mBy Custom Object: This is something my custom object is handling.\e[0m");
$this->shouldHavePersisted();

$this->cli->extend('League\CLImate\Tests\CustomObject\Basic');
$this->cli->extend(Basic::class);
$this->cli->basic('This is something my custom object is handling.');
}

Expand Down Expand Up @@ -90,16 +93,16 @@ public function it_can_be_extended_using_a_basic_object_with_argument_setter()
/** @test */
public function it_can_be_extended_using_a_dynamic_object()
{
$this->cli->extend('League\CLImate\Tests\CustomObject\Dynamic');
$this->cli->extend(Dynamic::class);
$obj = $this->cli->dynamic();

$this->assertInstanceOf('League\CLImate\Tests\CustomObject\Dynamic', $obj);
$this->assertInstanceOf(Dynamic::class, $obj);
}

/** @test */
public function it_will_yell_if_extending_and_class_doesnt_exist()
{
$class = 'League\CLImate\Tests\CustomObject\NowhereToBeFound';
$class = "NoSuchClass";
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage("Class does not exist: {$class}");
$this->cli->extend($class);
Expand All @@ -108,7 +111,7 @@ public function it_will_yell_if_extending_and_class_doesnt_exist()
/** @test */
public function it_will_yell_if_it_doesnt_implement_proper_interfaces()
{
$class = 'League\CLImate\Tests\CustomObject\Dummy';
$class = Dummy::class;
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Class must implement either");
$this->cli->extend($class);
Expand All @@ -123,7 +126,7 @@ public function it_will_accept_a_custom_key_for_an_extension()
$this->shouldWrite("\e[mBy Custom Object: This is something my custom object is handling.\e[0m");
$this->shouldHavePersisted();

$this->cli->extend('League\CLImate\Tests\CustomObject\Basic', 'myCustomMethod');
$this->cli->extend(Basic::class, 'myCustomMethod');
$this->cli->myCustomMethod('This is something my custom object is handling.');
}

Expand All @@ -134,8 +137,8 @@ public function it_will_accept_an_array_of_extensions()
$this->shouldHavePersisted();

$extensions = [
'League\CLImate\Tests\CustomObject\Basic',
'League\CLImate\Tests\CustomObject\Dynamic',
Basic::class,
Dynamic::class,
];

$this->cli->extend($extensions);
Expand All @@ -144,7 +147,7 @@ public function it_will_accept_an_array_of_extensions()

$obj = $this->cli->dynamic();

$this->assertInstanceOf('League\CLImate\Tests\CustomObject\Dynamic', $obj);
$this->assertInstanceOf(Dynamic::class, $obj);
}

/** @test */
Expand All @@ -154,8 +157,8 @@ public function it_will_accept_an_array_of_extensions_with_custom_keys()
$this->shouldHavePersisted();

$extensions = [
'whatever' => 'League\CLImate\Tests\CustomObject\Basic',
'something' => 'League\CLImate\Tests\CustomObject\Dynamic',
'whatever' => Basic::class,
'something' => Dynamic::class,
];

$this->cli->extend($extensions);
Expand All @@ -164,6 +167,6 @@ public function it_will_accept_an_array_of_extensions_with_custom_keys()

$obj = $this->cli->something();

$this->assertInstanceOf('League\CLImate\Tests\CustomObject\Dynamic', $obj);
$this->assertInstanceOf(Dynamic::class, $obj);
}
}
11 changes: 6 additions & 5 deletions tests/Decorator/Parser/AnsiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use League\CLImate\Decorator\Parser\ParserFactory;
use League\CLImate\Decorator\Style;
use League\CLImate\Decorator\Tags;
use League\CLImate\TerminalObject;
use League\CLImate\TerminalObject\Basic\BasicTerminalObject;
use League\CLImate\TerminalObject\Router\BasicRouter;
use League\CLImate\Tests\TestBase;
use League\CLImate\Util\System\Linux;
Expand All @@ -28,7 +28,8 @@ public function it_can_output_with_ansi()

$style = new Style();
$parser = new Ansi($style->current(), new Tags($style->all()));
$obj = Mockery::mock('League\CLImate\TerminalObject');

$obj = Mockery::mock(BasicTerminalObject::class);
$obj->shouldReceive('result')->once()->andReturn("<green>I am green</green>");
$obj->shouldReceive('sameLine')->once()->andReturn(false);
$obj->shouldReceive('getParser')->once()->andReturn($parser);
Expand All @@ -52,7 +53,7 @@ public function it_can_output_without_ansi()
$style = new Style();
$parser = new NonAnsi($style->current(), new Tags($style->all()));

$obj = Mockery::mock('League\CLImate\TerminalObject');
$obj = Mockery::mock(BasicTerminalObject::class);
$obj->shouldReceive('result')->once()->andReturn("<green>I am not green</green>");
$obj->shouldReceive('sameLine')->once()->andReturn(false);
$obj->shouldReceive('getParser')->once()->andReturn($parser);
Expand All @@ -67,12 +68,12 @@ public function it_can_output_without_ansi()
/** @test */
public function it_will_recognize_non_ansi_systems()
{
$system = Mockery::mock('League\CLImate\Util\System\Windows');
$system = Mockery::mock(Windows::class);
$system->shouldReceive('hasAnsiSupport')->andReturn(false);

$parser = ParserFactory::getInstance($system, [], new Tags([]));

$this->assertInstanceOf('League\CLImate\Decorator\Parser\NonAnsi', $parser);
$this->assertInstanceOf(NonAnsi::class, $parser);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/TerminalObject/Dynamic/Animation/AnimationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace League\CLImate\Tests\TerminalObject\Dynamic;

use League\CLImate\TerminalObject\Helper\Sleeper;
use League\CLImate\Tests\TestBase;
use Mockery;

Expand Down Expand Up @@ -37,7 +38,7 @@ protected function blankLines($count = 1)

protected function getSleeper($count)
{
$sleeper = Mockery::mock('League\CLImate\TerminalObject\Helper\Sleeper');
$sleeper = Mockery::mock(Sleeper::class);
$sleeper->shouldReceive('sleep')->times($count);

return $sleeper;
Expand Down
9 changes: 6 additions & 3 deletions tests/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace League\CLImate\Tests;

use League\CLImate\CLImate;
use League\CLImate\Util\Output;
use League\CLImate\Util\Reader\Stdin;
use League\CLImate\Util\System\Linux;
use Mockery;
use PHPUnit\Framework\TestCase;

Expand All @@ -28,13 +31,13 @@ public function setUp(): void
{
self::$functions = Mockery::mock();

$system = Mockery::mock('League\CLImate\Util\System\Linux');
$system = Mockery::mock(Linux::class);
$system->shouldReceive('hasAnsiSupport')->andReturn(true);
$system->shouldReceive('width')->andReturn(80);

$this->util = new \League\CLImate\Util\UtilFactory($system);
$this->output = Mockery::mock('League\CLImate\Util\Output');
$this->reader = Mockery::mock('League\CLImate\Util\Reader\Stdin');
$this->output = Mockery::mock(Output::class);
$this->reader = Mockery::mock(Stdin::class);

$this->cli = new CLImate();
$this->cli->setOutput($this->output);
Expand Down
43 changes: 23 additions & 20 deletions tests/Util/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use League\CLImate\Exceptions\InvalidArgumentException;
use League\CLImate\Tests\TestBase;
use League\CLImate\Util\Output;
use League\CLImate\Util\Writer\Buffer;
use League\CLImate\Util\Writer\StdErr;
use League\CLImate\Util\Writer\StdOut;
use Mockery;

use function get_class;
Expand All @@ -17,7 +20,7 @@ class OutputTest extends TestBase
*/
public function it_can_output_content()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with("Oh, hey there." . \PHP_EOL);

$output = new Output();
Expand All @@ -33,7 +36,7 @@ public function it_can_output_content()
*/
public function it_can_output_content_without_a_new_line()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with("Oh, hey there.");

$output = new Output();
Expand All @@ -49,7 +52,7 @@ public function it_can_output_content_without_a_new_line()
*/
public function it_can_default_to_a_writer()
{
$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$error = Mockery::mock(StdErr::class);
$error->shouldReceive('write')->once()->with("Oh, hey there.");

$output = new Output();
Expand All @@ -66,10 +69,10 @@ public function it_can_default_to_a_writer()
*/
public function it_can_default_to_multiple_writers()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with("Oh, hey there.");

$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$error = Mockery::mock(StdErr::class);
$error->shouldReceive('write')->once()->with("Oh, hey there.");

$output = new Output();
Expand All @@ -87,10 +90,10 @@ public function it_can_default_to_multiple_writers()
*/
public function it_can_add_a_default()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with("Oh, hey there.");

$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$error = Mockery::mock(StdErr::class);
$error->shouldReceive('write')->once()->with("Oh, hey there.");

$output = new Output();
Expand All @@ -109,10 +112,10 @@ public function it_can_add_a_default()
*/
public function it_can_handle_multiple_writers_for_one_key()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with('Oh, hey there.');

$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$error = Mockery::mock(StdErr::class);
$error->shouldReceive('write')->once()->with('Oh, hey there.');

$output = new Output();
Expand All @@ -129,10 +132,10 @@ public function it_can_handle_multiple_writers_for_one_key()
*/
public function it_can_take_existing_writer_keys_and_resolve_them()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with('Oh, hey there.');

$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$error = Mockery::mock(StdErr::class);
$error->shouldReceive('write')->once()->with('Oh, hey there.');

$output = new Output();
Expand All @@ -148,9 +151,9 @@ public function it_can_take_existing_writer_keys_and_resolve_them()
/** @test */
public function it_can_get_the_available_writers()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$buffer = Mockery::mock('League\CLImate\Util\Writer\Buffer');
$out = Mockery::mock(StdOut::class);
$error = Mockery::mock(StdErr::class);
$buffer = Mockery::mock(Buffer::class);

$output = new Output();

Expand Down Expand Up @@ -185,7 +188,7 @@ public function it_can_get_the_available_writers()
/** @test */
public function it_will_yell_if_writer_does_not_implement_writer_interface()
{
$out = Mockery::mock('League\CLImate\Util\Writer\Wat');
$out = Mockery::mock();
$output = new Output();

$this->expectException(InvalidArgumentException::class);
Expand All @@ -211,10 +214,10 @@ public function it_will_yell_if_trying_to_add_a_non_existent_nested_writer_key()
*/
public function it_can_write_to_a_non_default_writer_once()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with('Second time.');

$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$error = Mockery::mock(StdErr::class);
$error->shouldReceive('write')->once()->with('First time.');

$output = new Output();
Expand All @@ -234,10 +237,10 @@ public function it_can_write_to_a_non_default_writer_once()
*/
public function it_will_persist_writer_if_told_to()
{
$out = Mockery::mock('League\CLImate\Util\Writer\StdOut');
$out = Mockery::mock(StdOut::class);
$out->shouldReceive('write')->once()->with('Second time.');

$error = Mockery::mock('League\CLImate\Util\Writer\StdErr');
$error = Mockery::mock(StdErr::class);
$error->shouldReceive('write')->times(3)->with('First time.');

$output = new Output();
Expand All @@ -258,7 +261,7 @@ public function it_will_persist_writer_if_told_to()
/** @test */
public function it_can_retrieve_a_writer()
{
$buffer = Mockery::mock('League\CLImate\Util\Writer\Buffer');
$buffer = Mockery::mock(Buffer::class);
$buffer->shouldReceive('write')->once()->with('Oh, hey there.');
$buffer->shouldReceive('get')->once()->andReturn('Oh, hey there.');

Expand Down
9 changes: 5 additions & 4 deletions tests/Util/System/LinuxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace League\CLImate\Tests\Util\System;

use League\CLImate\Tests\TestBase;
use League\CLImate\Util\System\Linux;
use Mockery;

class LinuxTest extends TestBase
{
/** @test */
public function it_can_get_the_width()
{
$system = Mockery::mock('League\CLImate\Util\System\Linux')
$system = Mockery::mock(Linux::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

Expand All @@ -22,7 +23,7 @@ public function it_can_get_the_width()
/** @test */
public function it_will_return_null_if_width_is_not_numeric()
{
$system = Mockery::mock('League\CLImate\Util\System\Linux')
$system = Mockery::mock(Linux::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

Expand All @@ -34,7 +35,7 @@ public function it_will_return_null_if_width_is_not_numeric()
/** @test */
public function it_can_get_the_height()
{
$system = Mockery::mock('League\CLImate\Util\System\Linux')
$system = Mockery::mock(Linux::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

Expand All @@ -46,7 +47,7 @@ public function it_can_get_the_height()
/** @test */
public function it_will_return_null_if_height_is_not_numeric()
{
$system = Mockery::mock('League\CLImate\Util\System\Linux')
$system = Mockery::mock(Linux::class)
->makePartial()
->shouldAllowMockingProtectedMethods();

Expand Down
Loading

0 comments on commit bd01721

Please sign in to comment.