Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stricter argument, property, and return types #1463

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions features/configuration/developer_adds_extensions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Developer enables extensions

class Extension2 implements \PhpSpec\Extension
{
public function load(\PhpSpec\ServiceContainer $container, array $params)
public function load(\PhpSpec\ServiceContainer $container, array $params) : void
{
throw new \Exception(get_class($this).' enabled'. print_r($params, true));
}
Expand All @@ -38,7 +38,7 @@ Feature: Developer enables extensions

class Extension3 implements \PhpSpec\Extension
{
public function load(\PhpSpec\ServiceContainer $container, array $params)
public function load(\PhpSpec\ServiceContainer $container, array $params) : void
{
throw new \Exception(get_class($this).' enabled'. print_r($params, true));
}
Expand All @@ -62,7 +62,7 @@ Feature: Developer enables extensions

class NOPE implements \PhpSpec\Extension
{
public function load(\PhpSpec\ServiceContainer $container, array $params)
public function load(\PhpSpec\ServiceContainer $container, array $params) : void
{
throw new \Exception(get_class($this).' enabled'. print_r($params, true));
}
Expand All @@ -86,7 +86,7 @@ Feature: Developer enables extensions

class Extension4 implements \PhpSpec\Extension
{
public function load(\PhpSpec\ServiceContainer $container, array $params)
public function load(\PhpSpec\ServiceContainer $container, array $params) : void
{
throw new \Exception(get_class($this).' enabled'. print_r($params, true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Feature: Developer uses bootstrap config key in any place

class Extension implements PhpSpecExtension
{
public function load(ServiceContainer $container, array $params)
public function load(ServiceContainer $container, array $params) : void
{
$container->get('console.io');
}
Expand Down
4 changes: 2 additions & 2 deletions features/extensions/developer_uses_extension.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Feature: Developer uses extension

class Extension implements PhpSpecExtension
{
public function load(ServiceContainer $container, array $params)
public function load(ServiceContainer $container, array $params) : void
{
$container->define('matchers.seven', function (ServiceContainer $c) {
return new BeSevenMatcher($c->get('formatter.presenter'));
Expand Down Expand Up @@ -173,7 +173,7 @@ Feature: Developer uses extension

class EventSubscriberExtension implements PhpSpecExtension
{
public function load(ServiceContainer $compositeContainer, array $params)
public function load(ServiceContainer $compositeContainer, array $params) : void
{
$io = $compositeContainer->get('console.io');
$eventDispatcher = $compositeContainer->get('event_dispatcher');
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MissingParamType><errorLevel type="error"/></MissingParamType>
<MissingReturnType><errorLevel type="error"/></MissingReturnType>
<MissingPropertyType><errorLevel type="error"/></MissingPropertyType>
<DuplicateClass>
<errorLevel type="suppress">
<!-- duplicate classes are inside a conditional so not an error -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function it_creates_folder_for_class_if_needed($io, TemplateRenderer $tpl, $fs,
$fs->pathExists('/project/src/Acme/App.php')->willReturn(false);
$fs->isDirectory('/project/src/Acme')->willReturn(false);
$fs->makeDirectory('/project/src/Acme')->shouldBeCalled();
$fs->putFileContents('/project/src/Acme/App.php', Argument::any())->willReturn(null);
$fs->putFileContents('/project/src/Acme/App.php', Argument::any())->shouldBeCalled();

$this->generate($resource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function it_creates_folder_for_spec_if_needed($io, TemplateRenderer $tpl, $fs, R
$fs->pathExists('/project/spec/Acme/AppSpec.php')->willReturn(false);
$fs->isDirectory('/project/spec/Acme')->willReturn(false);
$fs->makeDirectory('/project/spec/Acme')->shouldBeCalled();
$fs->putFileContents('/project/spec/Acme/AppSpec.php', Argument::any())->willReturn(null);
$fs->putFileContents('/project/spec/Acme/AppSpec.php', Argument::any())->shouldBeCalled();

$this->generate($resource);
}
Expand Down
2 changes: 1 addition & 1 deletion spec/PhpSpec/Config/OptionsConfigSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function it_says_bootstrap_path_is_false_when_setting_is_false()
{
$this->beConstructedWith(false, false, false, false, false, false);

$this->getBootstrapPath()->shouldReturn(false);
$this->getBootstrapPath()->shouldReturn(null);
}

function it_returns_bootstrap_path_when_one_is_specified()
Expand Down
5 changes: 3 additions & 2 deletions spec/PhpSpec/Event/ExampleEventSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\PhpSpec\Event;

use PhpSpec\Event\ExampleEvent;
use PhpSpec\ObjectBehavior;

use PhpSpec\Loader\Node\ExampleNode;
Expand Down Expand Up @@ -48,7 +49,7 @@ function it_provides_a_link_to_time()

function it_provides_a_link_to_result()
{
$this->getResult()->shouldReturn($this->FAILED);
$this->getResult()->shouldReturn(ExampleEvent::FAILED);
}

function it_provides_a_link_to_exception($exception)
Expand All @@ -60,7 +61,7 @@ function it_initializes_a_default_result(ExampleNode $example)
{
$this->beConstructedWith($example);

$this->getResult()->shouldReturn($this->PASSED);
$this->getResult()->shouldReturn(ExampleEvent::PASSED);
}

function it_initializes_a_default_time(ExampleNode $example)
Expand Down
5 changes: 3 additions & 2 deletions spec/PhpSpec/Event/ExpectationEventSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\PhpSpec\Event;

use PhpSpec\Event\ExpectationEvent;
use PhpSpec\ObjectBehavior;
use PhpSpec\Loader\Suite;
use PhpSpec\Loader\Node\SpecificationNode;
Expand Down Expand Up @@ -66,7 +67,7 @@ function it_provides_a_link_to_arguments()

function it_provides_a_link_to_result()
{
$this->getResult()->shouldReturn($this->FAILED);
$this->getResult()->shouldReturn(ExpectationEvent::FAILED);
}

function it_provides_a_link_to_exception($exception)
Expand All @@ -81,6 +82,6 @@ function it_initializes_a_default_result(ExampleNode $example, Matcher $matcher,

$this->beConstructedWith($example, $matcher, $subject, $method, $arguments);

$this->getResult()->shouldReturn($this->PASSED);
$this->getResult()->shouldReturn(ExpectationEvent::PASSED);
}
}
4 changes: 2 additions & 2 deletions spec/PhpSpec/Formatter/HtmlFormatterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace spec\PhpSpec\Formatter;

use PhpSpec\Formatter\Html\ReportPassedItem;
use PhpSpec\IO\IO;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

use PhpSpec\Event\ExampleEvent;
use PhpSpec\Formatter\Html\ReportItem;
use PhpSpec\Formatter\Html\ReportItemFactory;
use PhpSpec\Formatter\Presenter\Presenter;
use PhpSpec\Listener\StatisticsCollector;
Expand All @@ -27,7 +27,7 @@ function it_is_an_event_subscriber()
}

function it_delegates_the_reporting_to_the_event_type_line_reporter(
ExampleEvent $event, ReportItem $item, ReportItemFactory $factory,
ExampleEvent $event, ReportPassedItem $item, ReportItemFactory $factory,
Presenter $presenter)
{
$factory->create($event, $presenter)->willReturn($item);
Expand Down
61 changes: 22 additions & 39 deletions spec/PhpSpec/Formatter/JUnitFormatterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\PhpSpec\Formatter;

use Exception;
use PhpSpec\ObjectBehavior;
use PhpSpec\Formatter\Presenter\Presenter;
use PhpSpec\IO\IO;
Expand Down Expand Up @@ -55,24 +56,26 @@ function it_stores_a_testcase_node_after_broken_example_run(
$event->getTitle()->willReturn('example title');
$event->getTime()->willReturn(1337);

$event->getException()->willReturn(new ExceptionStub('Something went wrong', 'Exception trace'));
$event->getException()->willReturn(new Exception('Something went wrong'));

$event->getSpecification()->willReturn($specification);
$specification->getClassReflection()->willReturn($refClass);
$refClass->getName()->willReturn('Acme\Foo\Bar');

$this->afterExample($event);

$this->getTestCaseNodes()->shouldReturn(array(
$testCaseNode = $this->getTestCaseNodes()[0];

$testCaseNode->shouldStartWith(
'<testcase name="example title" time="1337.000000" classname="Acme\Foo\Bar" status="broken">'."\n".
'<error type="spec\PhpSpec\Formatter\ExceptionStub" message="Something went wrong" />'."\n".
'<system-err>'."\n".
'<![CDATA['."\n".
'Exception trace'."\n".
']]>'."\n".
'</system-err>'."\n".
'<error type="Exception" message="Something went wrong" />'."\n".
'<system-err>'
);

$testCaseNode->shouldEndWith(
'</system-err>'."\n".
'</testcase>'
));
);
}

function it_stores_a_testcase_node_after_failed_example_run(
Expand All @@ -84,24 +87,26 @@ function it_stores_a_testcase_node_after_failed_example_run(
$event->getTitle()->willReturn('example title');
$event->getTime()->willReturn(1337);

$event->getException()->willReturn(new ExceptionStub('Something went wrong', 'Exception trace'));
$event->getException()->willReturn(new Exception('Something went wrong'));

$event->getSpecification()->willReturn($specification);
$specification->getClassReflection()->willReturn($refClass);
$refClass->getName()->willReturn('Acme\Foo\Bar');

$this->afterExample($event);

$this->getTestCaseNodes()->shouldReturn(array(
$testCaseNode = $this->getTestCaseNodes()[0];

$testCaseNode->shouldStartWith(
'<testcase name="example title" time="1337.000000" classname="Acme\Foo\Bar" status="failed">'."\n".
'<failure type="spec\PhpSpec\Formatter\ExceptionStub" message="Something went wrong" />'."\n".
'<system-err>'."\n".
'<![CDATA['."\n".
'Exception trace'."\n".
']]>'."\n".
'<failure type="Exception" message="Something went wrong" />'."\n".
'<system-err>'
);

$testCaseNode->shouldEndWith(
'</system-err>'."\n".
'</testcase>'
));
);
}

function it_stores_a_testcase_node_after_skipped_example_run(
Expand Down Expand Up @@ -201,25 +206,3 @@ function it_aggregates_testsuite_nodes_and_display_them_after_suite_run(SuiteEve
)->shouldBeCalled();
}
}

class ExceptionStub
{
protected $trace;
protected $message;

public function __construct($message, $trace)
{
$this->message = $message;
$this->trace = $trace;
}

public function getMessage()
{
return $this->message;
}

public function getTraceAsString()
{
return $this->trace;
}
}
19 changes: 10 additions & 9 deletions spec/PhpSpec/Listener/MethodReturnedNullListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpSpec\Util\MethodAnalyser;
use Prophecy\Argument;
use PhpSpec\Exception\Example\MethodFailureException;
use stdClass;

class MethodReturnedNullListenerSpec extends ObjectBehavior
{
Expand All @@ -33,10 +34,10 @@ function let(
$methodFailureException->getActual()->willReturn(null);
$methodFailureException->getExpected()->willReturn(100);
$methodFailureException->getSubject()->willReturn(null);
$methodFailureException->getMethod()->willReturn(null);
$methodFailureException->getMethod()->willReturn('');

$methodCallEvent->getMethod()->willReturn('foo');
$methodCallEvent->getSubject()->willReturn(new \stdClass);
$methodCallEvent->getSubject()->willReturn(new stdClass);

$io->isCodeGenerationEnabled()->willReturn(true);

Expand Down Expand Up @@ -123,7 +124,7 @@ function it_does_not_prompt_when_there_is_a_problem_creating_the_resource(
MethodCallEvent $methodCallEvent, ExampleEvent $exampleEvent, ConsoleIO $io, ResourceManager $resourceManager, SuiteEvent $event
) {
$resourceManager->createResource(Argument::any())->willThrow(new \RuntimeException());
$methodCallEvent->getSubject()->willReturn(new \stdClass());
$methodCallEvent->getSubject()->willReturn(new stdClass());
$methodCallEvent->getMethod()->willReturn('');

$this->afterMethodCall($methodCallEvent);
Expand All @@ -137,7 +138,7 @@ function it_does_not_prompt_when_input_is_not_interactive(
MethodCallEvent $methodCallEvent, ExampleEvent $exampleEvent, ConsoleIO $io, SuiteEvent $event
) {
$io->isCodeGenerationEnabled()->willReturn(false);
$methodCallEvent->getSubject()->willReturn(new \stdClass());
$methodCallEvent->getSubject()->willReturn(new stdClass());
$methodCallEvent->getMethod()->willReturn('');

$this->afterMethodCall($methodCallEvent);
Expand Down Expand Up @@ -175,7 +176,7 @@ function it_does_not_prompt_when_multiple_contradictory_examples_are_found(
$notEqualException->getExpected()->willReturn('foo');
$notEqualException2->getExpected()->willReturn('bar');

$methodCallEvent->getSubject()->willReturn(new \stdClass());
$methodCallEvent->getSubject()->willReturn(new stdClass());
$methodCallEvent->getMethod()->willReturn('');

$this->afterMethodCall($methodCallEvent);
Expand All @@ -193,7 +194,7 @@ function it_does_not_prompt_when_io_has_faking_disabled(
MethodCallEvent $methodCallEvent, ExampleEvent $exampleEvent, ConsoleIO $io, SuiteEvent $event
) {
$io->isFakingEnabled()->willReturn(false);
$methodCallEvent->getSubject()->willReturn(new \stdClass());
$methodCallEvent->getSubject()->willReturn(new stdClass());
$methodCallEvent->getMethod()->willReturn('');

$this->afterMethodCall($methodCallEvent);
Expand All @@ -206,7 +207,7 @@ function it_does_not_prompt_when_io_has_faking_disabled(
function it_prompts_when_correct_type_of_exception_is_thrown(
MethodCallEvent $methodCallEvent, ExampleEvent $exampleEvent, ConsoleIO $io, SuiteEvent $event
) {
$methodCallEvent->getSubject()->willReturn(new \stdClass());
$methodCallEvent->getSubject()->willReturn(new stdClass());
$methodCallEvent->getMethod()->willReturn('');

$this->afterMethodCall($methodCallEvent);
Expand All @@ -219,7 +220,7 @@ function it_prompts_when_correct_type_of_exception_is_thrown(
function it_prompts_if_no_method_was_called_beforehand_but_subject_and_method_are_set_on_the_exception(
ExampleEvent $exampleEvent, ConsoleIO $io, SuiteEvent $event, MethodFailureException $methodFailureException
) {
$methodFailureException->getSubject()->willReturn(new \stdClass());
$methodFailureException->getSubject()->willReturn(new stdClass());
$methodFailureException->getMethod()->willReturn('myMethod');

$this->afterExample($exampleEvent);
Expand All @@ -235,7 +236,7 @@ function it_invokes_method_body_generation_when_prompt_is_answered_yes(
$io->askConfirmation(Argument::any())->willReturn(true);
$resourceManager->createResource(Argument::any())->willReturn($resource);

$methodCallEvent->getSubject()->willReturn(new \StdClass());
$methodCallEvent->getSubject()->willReturn(new stdClass());
$methodCallEvent->getMethod()->willReturn('myMethod');

$this->afterMethodCall($methodCallEvent);
Expand Down
Loading