Skip to content

Commit c38dc02

Browse files
committed
Add phpspecs as spec for better tests 🚀
1 parent 0f42bca commit c38dc02

File tree

170 files changed

+12615
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+12615
-226
lines changed

.php_cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ return PhpCsFixer\Config::create()
2626
->setFinder(
2727
PhpCsFixer\Finder::create()
2828
->in(__DIR__ . '/src')
29-
->in(__DIR__ . '/tests')
3029
->in(__DIR__ . '/spec')
3130
)
3231
->setRiskyAllowed(true)
3332
->setUsingCache(false)
34-
;
33+
;

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
},
2323
"autoload-dev": {
2424
"psr-4": {
25-
"Proget\\Tests\\PHPStan\\PhpSpec\\": "tests/",
26-
"spec\\Proget\\": "spec/Proget/"
25+
"spec\\PhpSpec\\": "spec/PhpSpec/"
2726
}
2827
},
2928
"license": "MIT",
@@ -37,7 +36,7 @@
3736
"check-cs": "php-cs-fixer fix --dry-run --diff",
3837
"fix-cs": "php-cs-fixer fix",
3938
"tests": "phpspec run",
40-
"stan": "phpstan analyse -l max -c ./phpstan.neon ./src ./tests ./spec",
39+
"stan": "phpstan analyse -l max -c ./phpstan.neon ./src ./spec",
4140
"check": [
4241
"@check-cs",
4342
"@stan",

extension.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ services:
22
-
33
class: Proget\PHPStan\PhpSpec\Reflection\ObjectBehaviorMethodsClassReflectionExtension
44
tags: [phpstan.broker.methodsClassReflectionExtension]
5+
-
6+
class: Proget\PHPStan\PhpSpec\Reflection\ObjectBehaviorPropertiesClassReflectionExtension
7+
tags: [phpstan.broker.propertiesClassReflectionExtension]
8+
-
9+
class: Proget\PHPStan\PhpSpec\Type\ObjectBehaviorDynamicMethodReturnTypeExtension
10+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
511

612
extensions:
713
phpspec: Proget\PHPStan\PhpSpec\DependencyInjection\CollaboratorExtension
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\PhpSpec\CodeAnalysis;
6+
7+
use PhpSpec\CodeAnalysis\AccessInspector;
8+
use PhpSpec\ObjectBehavior;
9+
10+
class MagicAwareAccessInspectorSpec extends ObjectBehavior
11+
{
12+
public function let(AccessInspector $accessInspector)
13+
{
14+
$this->beConstructedWith($accessInspector);
15+
}
16+
17+
public function it_should_be_an_access_inspector()
18+
{
19+
$this->shouldImplement('PhpSpec\CodeAnalysis\AccessInspector');
20+
}
21+
22+
public function it_should_detect_a_magic_getter_if_no_value_is_given()
23+
{
24+
$this->isPropertyReadable(new ObjectWithMagicGet, 'property')->shouldReturn(true);
25+
}
26+
27+
public function it_should_detect_a_magic_setter_if_a_value_is_given()
28+
{
29+
$this->isPropertyWritable(new ObjectWithMagicSet, 'property', true)->shouldReturn(true);
30+
}
31+
32+
public function it_should_detect_a_magic_call_method()
33+
{
34+
$this->isMethodCallable(new ObjectWithMagicCall, 'method')->shouldreturn(true);
35+
}
36+
37+
public function it_should_not_detect_a_getter_if_there_is_no_magic_getter_and_wrapped_inspector_finds_none(AccessInspector $accessInspector)
38+
{
39+
$accessInspector->isPropertyReadable(new \stdClass(), 'foo')->willReturn(false);
40+
41+
$this->isPropertyReadable(new \stdClass(), 'foo')->shouldReturn(false);
42+
}
43+
44+
public function it_should_detect_a_getter_if_there_is_no_magic_getter_but_wrapped_inspector_finds_one(AccessInspector $accessInspector)
45+
{
46+
$accessInspector->isPropertyReadable(new \stdClass(), 'foo')->willReturn(true);
47+
48+
$this->isPropertyReadable(new \stdClass(), 'foo')->shouldReturn(true);
49+
}
50+
51+
public function it_should_not_detect_a_setter_if_there_is_no_magic_setter_and_wrapped_inspector_finds_none(AccessInspector $accessInspector)
52+
{
53+
$accessInspector->isPropertyWritable(new \stdClass(), 'foo')->willReturn(false);
54+
55+
$this->isPropertyWritable(new \stdClass(), 'foo')->shouldReturn(false);
56+
}
57+
58+
public function it_should_detect_a_setter_if_there_is_no_magic_setter_but_wrapped_inspector_finds_one(AccessInspector $accessInspector)
59+
{
60+
$accessInspector->isPropertyWritable(new \stdClass(), 'foo')->willReturn(true);
61+
62+
$this->isPropertyWritable(new \stdClass(), 'foo')->shouldReturn(true);
63+
}
64+
65+
public function it_should_detect_a_method_if_there_is_no_magic_caller_and_wrapped_inspector_finds_none(AccessInspector $accessInspector)
66+
{
67+
$accessInspector->isMethodCallable(new \stdClass(), 'foo')->willReturn(false);
68+
69+
$this->isMethodCallable(new \stdClass(), 'foo')->shouldReturn(false);
70+
}
71+
72+
public function it_should_detect_a_method_if_there_is_no_magic_caller_but_wrapped_inspector_finds_one(AccessInspector $accessInspector)
73+
{
74+
$accessInspector->isMethodCallable(new \stdClass(), 'foo')->willReturn(true);
75+
76+
$this->isMethodCallable(new \stdClass(), 'foo')->shouldReturn(true);
77+
}
78+
}
79+
80+
class ObjectWithMagicGet
81+
{
82+
public function __get($name)
83+
{
84+
}
85+
}
86+
87+
class ObjectWithMagicSet
88+
{
89+
public function __set($name, $value)
90+
{
91+
}
92+
}
93+
94+
class ObjectWithMagicCall
95+
{
96+
public function __call($name, $args)
97+
{
98+
}
99+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\PhpSpec\CodeAnalysis;
6+
7+
use PhpSpec\CodeAnalysis\DisallowedNonObjectTypehintException;
8+
use PhpSpec\CodeAnalysis\NamespaceResolver;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class StaticRejectingNamespaceResolverSpec extends ObjectBehavior
12+
{
13+
public function let(NamespaceResolver $namespaceResolver)
14+
{
15+
$this->beConstructedWith($namespaceResolver);
16+
}
17+
18+
public function it_is_initializable()
19+
{
20+
$this->shouldHaveType('PhpSpec\CodeAnalysis\NamespaceResolver');
21+
}
22+
23+
public function it_delegates_analysis_to_wrapped_resolver(NamespaceResolver $namespaceResolver)
24+
{
25+
$this->analyse('foo');
26+
27+
$namespaceResolver->analyse('foo')->shouldhaveBeenCalled();
28+
}
29+
30+
public function it_delegates_resolution_to_wrapped_resolver(NamespaceResolver $namespaceResolver)
31+
{
32+
$namespaceResolver->resolve('Bar')->willReturn('Foo\Bar');
33+
34+
$this->resolve('Bar')->shouldReturn('Foo\Bar');
35+
}
36+
37+
public function it_does_not_allow_resolution_of_non_object_types()
38+
{
39+
$this->shouldThrow(DisallowedNonObjectTypehintException::class)->duringResolve('int');
40+
$this->shouldThrow(DisallowedNonObjectTypehintException::class)->duringResolve('float');
41+
$this->shouldThrow(DisallowedNonObjectTypehintException::class)->duringResolve('string');
42+
$this->shouldThrow(DisallowedNonObjectTypehintException::class)->duringResolve('bool');
43+
$this->shouldThrow(DisallowedNonObjectTypehintException::class)->duringResolve('iterable');
44+
}
45+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\PhpSpec\CodeAnalysis;
6+
7+
use PhpSpec\ObjectBehavior;
8+
9+
class TokenizedNamespaceResolverSpec extends ObjectBehavior
10+
{
11+
public function it_is_initializable()
12+
{
13+
$this->shouldHaveType('PhpSpec\CodeAnalysis\NamespaceResolver');
14+
}
15+
16+
public function it_resolves_types_outside_of_namespaces()
17+
{
18+
$this->analyse('
19+
<?php
20+
21+
class Foo
22+
{
23+
}
24+
');
25+
26+
$this->resolve('Bar')->shouldReturn('Bar');
27+
$this->resolve('Bar')->shouldReturn('Bar');
28+
}
29+
30+
public function it_resolves_types_from_current_namespace()
31+
{
32+
$this->analyse('
33+
<?php
34+
35+
namespace Baz;
36+
37+
class Foo
38+
{
39+
}
40+
41+
');
42+
43+
$this->resolve('Foo')->shouldReturn('Baz\Foo');
44+
$this->resolve('Bar')->shouldReturn('Baz\Bar');
45+
}
46+
47+
public function it_resolves_types_with_use_statements()
48+
{
49+
$this->analyse('
50+
<?php
51+
52+
namespace Baz;
53+
54+
use Boz\Bar;
55+
56+
class Foo
57+
{
58+
}
59+
60+
');
61+
62+
$this->resolve('Foo')->shouldReturn('Baz\Foo');
63+
$this->resolve('Bar')->shouldReturn('Boz\Bar');
64+
}
65+
66+
public function it_resolves_types_with_use_aliases()
67+
{
68+
$this->analyse('
69+
<?php
70+
71+
namespace Baz;
72+
73+
use Boz\Bar as Biz;
74+
75+
class Foo
76+
{
77+
}
78+
79+
');
80+
81+
$this->resolve('Foo')->shouldReturn('Baz\Foo');
82+
$this->resolve('Biz')->shouldReturn('Boz\Bar');
83+
}
84+
85+
public function it_resolves_types_with_partial_use_statements()
86+
{
87+
$this->analyse('
88+
<?php
89+
90+
namespace Baz;
91+
92+
use Boz\Bar;
93+
94+
class Foo
95+
{
96+
function it_something(Bar\Baz $boz)
97+
{
98+
}
99+
}
100+
101+
');
102+
103+
$this->resolve('Foo')->shouldReturn('Baz\Foo');
104+
$this->resolve('Bar\Baz')->shouldReturn('Boz\Bar\Baz');
105+
}
106+
107+
public function it_resolves_types_from_grouped_use_statements()
108+
{
109+
$this->analyse('
110+
<?php
111+
112+
namespace Baz;
113+
114+
use Boz\{Fiz, Buz};
115+
116+
class Foo
117+
{
118+
function it_something(Fiz $fiz, Buz $buz)
119+
{
120+
}
121+
}
122+
123+
');
124+
125+
$this->resolve('Fiz')->shouldReturn('Boz\Fiz');
126+
$this->resolve('Buz')->shouldReturn('Boz\Buz');
127+
}
128+
}

0 commit comments

Comments
 (0)