Skip to content

Commit 5f18381

Browse files
Fixed incomplete tests & added improvement
1 parent 9d3ec7e commit 5f18381

File tree

6 files changed

+140
-10
lines changed

6 files changed

+140
-10
lines changed

tests/AnnotationLoaderTest.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PHPUnit\Framework\TestCase;
2929
use Spiral\Attributes\AnnotationReader;
3030
use Spiral\Attributes\AttributeReader;
31+
use Spiral\Attributes\Composite\MergeReader;
3132

3233
/**
3334
* AnnotationLoaderTest
@@ -36,6 +37,8 @@ class AnnotationLoaderTest extends TestCase
3637
{
3738
protected function setUp(): void
3839
{
40+
require_once __DIR__ . '/Fixtures/Annotation/function.php';
41+
3942
// doctrine/annotations ^1.0 compatibility.
4043
if (\method_exists(AnnotationRegistry::class, 'registerLoader')) {
4144
AnnotationRegistry::registerLoader('\\class_exists');
@@ -46,18 +49,18 @@ protected function setUp(): void
4649
* @dataProvider provideAnnotationLoader
4750
* @runInSeparateProcess
4851
*/
49-
public function testAttach($loader): void
52+
public function testAnnotationListenerWithResource($loader): void
5053
{
5154
$annotation = new AnnotationLoader(new AnnotationReader(), $loader);
5255
$result = $names = [];
5356

54-
$annotation->attachListener(new Fixtures\SampleListener());
55-
$annotation->attach(...[
57+
$annotation->listener(new Fixtures\SampleListener());
58+
$annotation->resource(...[
5659
__DIR__ . '/Fixtures/Annotation/Valid',
5760
'non-existing-file.php',
5861
]);
5962

60-
$this->assertCount(1, $founds = \iterator_to_array($annotation->load()));
63+
$this->assertCount(1, $founds = $annotation->load());
6164

6265
/** @var Fixtures\SampleCollector $found */
6366
foreach ($founds as $found) {
@@ -115,23 +118,23 @@ public function testAttach($loader): void
115118
['handler' => \ReflectionMethod::class, 'priority' => 323],
116119
['handler' => \ReflectionProperty::class, 'priority' => 0],
117120
['handler' => \ReflectionProperty::class, 'priority' => 0],
118-
['handler' => Fixtures\Annotation\Valid\SingleClass::class, 'priority' => 0],
121+
['handler' => \ReflectionClass::class, 'priority' => 0],
119122
], $result);
120123
}
121124

122125
/**
123126
* @dataProvider provideAnnotationLoader
124127
* @runInSeparateProcess
125128
*/
126-
public function testAttachAttribute($loader): void
129+
public function testAnnotationLoaderWithAttribute($loader): void
127130
{
128131
$annotation = new AnnotationLoader(new AttributeReader(), $loader);
129132
$result = [];
130133

131-
$annotation->attachListener(new Fixtures\SampleListener());
132-
$annotation->attach(__DIR__ . '/Fixtures/Annotation/Attribute');
134+
$annotation->listener(new Fixtures\SampleListener());
135+
$annotation->resource(__DIR__ . '/Fixtures/Annotation/Attribute');
133136

134-
$this->assertCount(1, $founds = \iterator_to_array($annotation->load()));
137+
$this->assertCount(1, $founds = $annotation->load());
135138

136139
/** @var Fixtures\SampleCollector $found */
137140
foreach ($founds as $found) {
@@ -159,6 +162,33 @@ public function testAttachAttribute($loader): void
159162
], $result);
160163
}
161164

165+
/**
166+
* @runInSeparateProcess
167+
*/
168+
public function testAnnotationLoaderWithFunction(): void
169+
{
170+
$annotation = new AnnotationLoader(new MergeReader([new AnnotationReader(), new AttributeReader()]));
171+
$result = $names = [];
172+
173+
$annotation->listener(new Fixtures\SampleListener());
174+
$annotation->resource('Biurad\\Annotations\\Tests\\Fixtures\\Valid\\annotated_function');
175+
176+
/** @var Fixtures\SampleCollector $found */
177+
foreach ($annotation->load() as $found) {
178+
$this->assertInstanceOf(Fixtures\SampleCollector::class, $found);
179+
180+
$collected = $found->getCollected();
181+
$collected->ksort();
182+
183+
foreach ($collected as $name => $sample) {
184+
$names[] = $name;
185+
$result[] = $sample;
186+
}
187+
}
188+
189+
$this->assertEquals(['attributed_function', 'function_property'], $names);
190+
}
191+
162192
public function provideAnnotationLoader(): array
163193
{
164194
return [
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Biurad opensource projects.
7+
*
8+
* PHP version 7.2 and above required
9+
*
10+
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
11+
* @copyright 2019 Biurad Group (https://biurad.com/)
12+
* @license https://opensource.org/licenses/BSD-3-Clause License
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
18+
namespace Biurad\Annotations\Tests\Fixtures\Annotation\Valid;
19+
20+
abstract class AbstractClass
21+
{
22+
abstract public function method(): void;
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Biurad opensource projects.
7+
*
8+
* PHP version 7.2 and above required
9+
*
10+
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
11+
* @copyright 2019 Biurad Group (https://biurad.com/)
12+
* @license https://opensource.org/licenses/BSD-3-Clause License
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
18+
namespace Biurad\Annotations\Tests\Fixtures\Annotation\Valid;
19+
20+
class EmptyClass
21+
{
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Biurad opensource projects.
7+
*
8+
* PHP version 7.2 and above required
9+
*
10+
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
11+
* @copyright 2019 Biurad Group (https://biurad.com/)
12+
* @license https://opensource.org/licenses/BSD-3-Clause License
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
18+
namespace Biurad\Annotations\Tests\Fixtures\Annotation\Valid;
19+
20+
class NoAnnotationClass
21+
{
22+
protected $prop = null;
23+
24+
public function method(): void
25+
{
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Biurad opensource projects.
7+
*
8+
* PHP version 7.2 and above required
9+
*
10+
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
11+
* @copyright 2019 Biurad Group (https://biurad.com/)
12+
* @license https://opensource.org/licenses/BSD-3-Clause License
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
18+
namespace Biurad\Annotations\Tests\Fixtures\Valid;
19+
20+
use Biurad\Annotations\Tests\Fixtures\Sample;
21+
22+
/** @Sample(name="annotated_function") */
23+
#[Sample(name: 'attributed_function')]
24+
function annotated_function(
25+
#[Sample('function_property', priority: 4)]
26+
string $parameter
27+
): void {
28+
}

tests/Fixtures/Sample.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Annotation class for @Listener().
2222
*
2323
* @Annotation
24-
* @Target({"CLASS", "METHOD", "PROPERTY"})
24+
* @Target({"CLASS", "METHOD", "FUNCTION", "PROPERTY"})
2525
*/
2626

2727
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::TARGET_PARAMETER)]

0 commit comments

Comments
 (0)