Skip to content

Commit

Permalink
Merge pull request #26 from doppynl/TestTagOnClass
Browse files Browse the repository at this point in the history
Configure TestTag to be allowed on a class.
  • Loading branch information
DaveLiddament authored Aug 12, 2024
2 parents 99e3149 + f73456c commit 17ff755
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class AnotherClass extends Result {}

## TestTag

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

E.g.

Expand Down Expand Up @@ -503,7 +503,8 @@ class PersonTest
```

NOTES:
- Methods with the`#[TestTag]` MUST have public visibility.
- Classes with the `#[TestTag]` will have an error when any interaction with the class is done.
- Methods with the `#[TestTag]` MUST have public visibility.
- 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:
- 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).
- 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).
Expand Down
31 changes: 31 additions & 0 deletions examples/testTag/testTagOnClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace TestTagOnConstructor;

use DaveLiddament\PhpLanguageExtensions\TestTag;

#[TestTag]
class Person
{
public function __construct()
{
}

public static function create(): Person // No Error, class can interact with itself
{
return new Person(); // No Error, class can interact with itself
}
}

class AnotherClass
{
public function newInstance(): Person
{
return new Person(); // ERROR
}

public function buildPerson(): Person
{
return Person::create(); // ERROR
}
}
31 changes: 31 additions & 0 deletions examples/testTag/testTagOnClassIgnoredInTestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace TestTagOnConstructor;

use DaveLiddament\PhpLanguageExtensions\TestTag;

#[TestTag]
class Person
{
public function __construct()
{
}

public static function create(): Person // No Error, class can interact with itself
{
return new Person(); // No Error, class can interact with itself
}
}

class PersonTest
{
public function newInstance(): Person
{
return new Person(); // No Error
}

public function buildPerson(): Person
{
return Person::create(); // No error
}
}
6 changes: 3 additions & 3 deletions src/TestTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace DaveLiddament\PhpLanguageExtensions;

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

0 comments on commit 17ff755

Please sign in to comment.