Skip to content

Commit

Permalink
Add tests for mixed regime
Browse files Browse the repository at this point in the history
  • Loading branch information
ebln committed Jun 4, 2024
1 parent 87664e2 commit 490a5f8
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ Intended to follow [«Keep a Changelog»](https://keepachangelog.com/en/)

## Upcoming

### Added
- Support for attributes
* missing stand-alone attribute ???

### Removed
* Support for PHP < 7.4
* Support for PHPStan < 1.11

----

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parameters:
analyseAndScan:
- tests/data/*
- tests/dataAttrib/*
- tests/dataMixed/*

rules:
- \Ebln\PHPStan\EnforceFactory\ForceFactoryRule
42 changes: 42 additions & 0 deletions tests/MixedForceFactoryRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Test\Ebln\PHPStan\EnforceFactory;

use Ebln\PHPStan\EnforceFactory\ForceFactoryRule;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @requires PHP >= 8.0
* @extends RuleTestCase<ForceFactoryRule>
*/
class MixedForceFactoryRuleTest extends RuleTestCase
{
public function testAttributeFactory(): void
{
$this->analyse(
[__DIR__ . '/dataMixed/AttributeFactory.php'],
[
['Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\MismatchedProduct has either no factories defined or a conflict between interface and attribute!', 25],
]
);
}

public function testRogueAttributeFactory(): void
{
$this->analyse([__DIR__ . '/dataMixed/RogueAttributeFactory.php'], [
['Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\AttributeProduct must be instantiated by Test\Ebln\PHPStan\EnforceFactory\dataMixed\AttributeFactory!', 14],
['Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\MixedProduct must be instantiated by Test\Ebln\PHPStan\EnforceFactory\dataMixed\AttributeFactory or Test\Ebln\PHPStan\EnforceFactory\dataMixed\ForcedFactory!', 19],

]);
}

protected function getRule(): Rule
{
return new ForceFactoryRule(self::getContainer()->getByType(ReflectionProvider::class));
}
}

27 changes: 27 additions & 0 deletions tests/dataMixed/AttributeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Test\Ebln\PHPStan\EnforceFactory\dataMixed;

use Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\AttributeProduct;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\MismatchedProduct;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\MixedProduct;

class AttributeFactory
{
public function allowedAttribute(): void
{
$test = new AttributeProduct();
}

public function allowedMix(): void
{
$test = new MixedProduct();
}

public function failingMismatch(): void
{
$test = new MismatchedProduct();
}
}
10 changes: 10 additions & 0 deletions tests/dataMixed/ForcedFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Test\Ebln\PHPStan\EnforceFactory\dataMixed;

class ForcedFactory
{

}
21 changes: 21 additions & 0 deletions tests/dataMixed/RogueAttributeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Test\Ebln\PHPStan\EnforceFactory\dataMixed;

use Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\AttributeProduct;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\code\MixedProduct;

class RogueAttributeFactory
{
public function forbiddenAttribute(): void
{
$test = new AttributeProduct();
}

public function forbiddenMix(): void
{
$test = new MixedProduct();
}
}
13 changes: 13 additions & 0 deletions tests/dataMixed/code/AttributeProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Test\Ebln\PHPStan\EnforceFactory\dataMixed\code;

use Ebln\PHPStan\EnforceFactory\ForceFactory;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\AttributeFactory;

#[ForceFactory(AttributeFactory::class)]
class AttributeProduct
{
}
19 changes: 19 additions & 0 deletions tests/dataMixed/code/MismatchedProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Test\Ebln\PHPStan\EnforceFactory\dataMixed\code;

use Ebln\PHPStan\EnforceFactory\ForceFactory;
use Ebln\PHPStan\EnforceFactory\ForceFactoryInterface;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\AttributeFactory;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\ForcedFactory;

#[ForceFactory(AttributeFactory::class)]
class MismatchedProduct implements ForceFactoryInterface
{
public static function getFactories(): array
{
return [ForcedFactory::class, AttributeFactory::class];
}
}
20 changes: 20 additions & 0 deletions tests/dataMixed/code/MixedProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Test\Ebln\PHPStan\EnforceFactory\dataMixed\code;

use Ebln\PHPStan\EnforceFactory\ForceFactory;
use Ebln\PHPStan\EnforceFactory\ForceFactoryInterface;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\AttributeFactory;
use Test\Ebln\PHPStan\EnforceFactory\dataMixed\ForcedFactory;

#[ForceFactory(ForcedFactory::class, AttributeFactory::class)]
class MixedProduct implements ForceFactoryInterface
{
public static function getFactories(): array
{
return [ForcedFactory::class, AttributeFactory::class];
}
}

0 comments on commit 490a5f8

Please sign in to comment.