Skip to content

Commit 17ff755

Browse files
Merge pull request #26 from doppynl/TestTagOnClass
Configure TestTag to be allowed on a class.
2 parents 99e3149 + f73456c commit 17ff755

4 files changed

Lines changed: 68 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class AnotherClass extends Result {}
471471

472472
## TestTag
473473

474-
The `#[TestTag]` attribute is an idea borrowed from hardware testing. Methods marked with this attribute are only available to test code.
474+
The `#[TestTag]` attribute is an idea borrowed from hardware testing. Classes or methods marked with this attribute are only available to test code.
475475

476476
E.g.
477477

@@ -503,7 +503,8 @@ class PersonTest
503503
```
504504

505505
NOTES:
506-
- Methods with the`#[TestTag]` MUST have public visibility.
506+
- Classes with the `#[TestTag]` will have an error when any interaction with the class is done.
507+
- Methods with the `#[TestTag]` MUST have public visibility.
507508
- For determining what is "test code" see the relevant plugin. E.g. the [PHPStan extension](https://github.com/DaveLiddament/phpstan-php-language-extensions) can be setup to either:
508509
- Assume all classes that end `Test` is test code. See [className config option](https://github.com/DaveLiddament/phpstan-php-language-extensions#exclude-checks-on-class-names-ending-with-test).
509510
- Assume all classes within a given namespace is test code. See [namespace config option](https://github.com/DaveLiddament/phpstan-php-language-extensions#exclude-checks-based-on-test-namespace).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace TestTagOnConstructor;
4+
5+
use DaveLiddament\PhpLanguageExtensions\TestTag;
6+
7+
#[TestTag]
8+
class Person
9+
{
10+
public function __construct()
11+
{
12+
}
13+
14+
public static function create(): Person // No Error, class can interact with itself
15+
{
16+
return new Person(); // No Error, class can interact with itself
17+
}
18+
}
19+
20+
class AnotherClass
21+
{
22+
public function newInstance(): Person
23+
{
24+
return new Person(); // ERROR
25+
}
26+
27+
public function buildPerson(): Person
28+
{
29+
return Person::create(); // ERROR
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace TestTagOnConstructor;
4+
5+
use DaveLiddament\PhpLanguageExtensions\TestTag;
6+
7+
#[TestTag]
8+
class Person
9+
{
10+
public function __construct()
11+
{
12+
}
13+
14+
public static function create(): Person // No Error, class can interact with itself
15+
{
16+
return new Person(); // No Error, class can interact with itself
17+
}
18+
}
19+
20+
class PersonTest
21+
{
22+
public function newInstance(): Person
23+
{
24+
return new Person(); // No Error
25+
}
26+
27+
public function buildPerson(): Person
28+
{
29+
return Person::create(); // No error
30+
}
31+
}

src/TestTag.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace DaveLiddament\PhpLanguageExtensions;
66

77
/**
8-
* Add the TestTag attribute to a method that should only be called by test code.
9-
* Attempts to call from non-test code will raise an issue.
8+
* Add the TestTag attribute to a class or a method that should only be called by test code.
9+
* Attempts to use or call from non-test code will raise an issue.
1010
*/
11-
#[\Attribute(\Attribute::TARGET_METHOD)]
11+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
1212
final class TestTag
1313
{
1414
}

0 commit comments

Comments
 (0)