diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..da528d2 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,53 @@ +name: Tests + +on: + push: + pull_request: + schedule: + - cron: '0 1 * * *' + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + php: [ '8.1', '8.2', '8.3' ] + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with tools + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer + - run: composer install + - run: composer test + + format: + runs-on: ubuntu-latest + strategy: + matrix: + php: [ '8.1' ] + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with tools + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer + - run: composer install + - run: composer format + + analyse: + runs-on: ubuntu-latest + strategy: + matrix: + php: [ '8.1' ] + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with tools + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer + - run: composer install + - run: composer analyse diff --git a/composer.json b/composer.json index ea18c0c..2d7bcf5 100644 --- a/composer.json +++ b/composer.json @@ -20,17 +20,23 @@ "require": { "php": "^7.4|^8.0", "ext-json": "*", - "phpstan/phpstan": "^1.0" + "phpstan/phpstan": "^1.0|^2.0" }, "require-dev": { "fakerphp/faker": "^1.21", - "laravel/pint": "^1.2" + "laravel/pint": "^1.2", + "phpunit/phpunit": "^9.6" }, "autoload": { "psr-4": { "Swis\\PHPStan\\Reflection\\": "src" } }, + "autoload-dev": { + "psr-4": { + "Swis\\PHPStan\\Reflection\\Tests\\": "tests" + } + }, "extra": { "phpstan": { "includes": [ @@ -40,7 +46,8 @@ }, "scripts": { "analyse": "vendor/bin/phpstan analyse", - "format": "vendor/bin/pint" + "format": "vendor/bin/pint", + "test": "vendor/bin/phpunit" }, "config": { "sort-packages": true diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..fc10755 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,19 @@ + + + + + tests + + + + + + src + + + diff --git a/src/FakerProviderReflectionExtension.php b/src/FakerProviderReflectionExtension.php index 76dec5f..34d41ae 100644 --- a/src/FakerProviderReflectionExtension.php +++ b/src/FakerProviderReflectionExtension.php @@ -11,7 +11,7 @@ use PHPStan\Reflection\PropertyReflection; use PHPStan\Reflection\ReflectionProvider; -class FakerProviderReflectionExtension implements PropertiesClassReflectionExtension, MethodsClassReflectionExtension +class FakerProviderReflectionExtension implements MethodsClassReflectionExtension, PropertiesClassReflectionExtension { protected ReflectionProvider $reflectionProvider; @@ -51,7 +51,7 @@ public function getMethod(ClassReflection $classReflection, string $methodName): if ($method === null) { // This should never happen, because hasMethod() should always be // called first. - throw new \PHPStan\ShouldNotHappenException(); + throw new \PHPStan\ShouldNotHappenException; } return $method; @@ -69,7 +69,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa if ($property === null) { // This should never happen, because hasProperty() should always be // called first. - throw new \PHPStan\ShouldNotHappenException(); + throw new \PHPStan\ShouldNotHappenException; } return $property; @@ -102,8 +102,8 @@ protected function findMethod(ClassReflection $classReflection, string $methodNa foreach ($this->providerClasses as $providerClass) { $providerReflection = $this->reflectionProvider->getClass($providerClass); try { - $methodReflection = $providerReflection->getMethod($methodName, new OutOfClassScope()); - if (!$methodReflection->isStatic() && $methodReflection->isPublic()) { + $methodReflection = $providerReflection->getMethod($methodName, new OutOfClassScope); + if (! $methodReflection->isStatic() && $methodReflection->isPublic()) { return $methodReflection; } } catch (MissingMethodFromReflectionException $e) { diff --git a/tests/BookProvider.php b/tests/BookProvider.php new file mode 100644 index 0000000..0315be8 --- /dev/null +++ b/tests/BookProvider.php @@ -0,0 +1,13 @@ +generator->ean13(); + } +} diff --git a/tests/FakerProviderReflectionExtensionTest.php b/tests/FakerProviderReflectionExtensionTest.php new file mode 100644 index 0000000..321426b --- /dev/null +++ b/tests/FakerProviderReflectionExtensionTest.php @@ -0,0 +1,35 @@ +addProvider(new BookProvider($faker)); + + $reflectionProvider = self::createReflectionProvider(); + $sut = new FakerProviderReflectionExtension($reflectionProvider, [BookProvider::class]); + + $classReflection = $reflectionProvider->getClass(Faker\Generator::class); + + $this->assertTrue($sut->hasMethod($classReflection, 'customBookIsbn')); + $this->assertTrue($sut->hasProperty($classReflection, 'customBookIsbn')); + } + + public function testProviderCustomDataIsNotAvailabledOutsideOfFakerGenerator(): void + { + $reflectionProvider = self::createReflectionProvider(); + $sut = new FakerProviderReflectionExtension($reflectionProvider, [BookProvider::class]); + + $classReflection = $reflectionProvider->getClass(BookProvider::class); + + $this->assertFalse($sut->hasMethod($classReflection, 'customBookIsbn')); + $this->assertFalse($sut->hasProperty($classReflection, 'customBookIsbn')); + } +}