mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-17 17:47:13 +00:00
* phpcs: enable also for config, resources and tests
* composer fix-style
* vendor/bin/phpunit -d --update-snapshots
* gha: instead of checking the style, fix and auto-commit it 💪
112 lines
3.6 KiB
PHP
112 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
|
|
|
use Barryvdh\LaravelIdeHelper\Method;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ExampleTest extends TestCase
|
|
{
|
|
/**
|
|
* Test that we can actually instantiate the class
|
|
*/
|
|
public function testCanInstantiate()
|
|
{
|
|
$reflectionClass = new \ReflectionClass(ExampleClass::class);
|
|
$reflectionMethod = $reflectionClass->getMethod('setName');
|
|
|
|
$method = new Method($reflectionMethod, 'Example', $reflectionClass);
|
|
|
|
$this->assertInstanceOf(Method::class, $method);
|
|
}
|
|
|
|
/**
|
|
* Test the output of a class
|
|
*/
|
|
public function testOutput()
|
|
{
|
|
$reflectionClass = new \ReflectionClass(ExampleClass::class);
|
|
$reflectionMethod = $reflectionClass->getMethod('setName');
|
|
|
|
$method = new Method($reflectionMethod, 'Example', $reflectionClass);
|
|
|
|
$output = '/**
|
|
*
|
|
*
|
|
* @param string $last
|
|
* @param string $first
|
|
* @param string $middle
|
|
* @static
|
|
*/';
|
|
$this->assertSame($output, $method->getDocComment(''));
|
|
$this->assertSame('setName', $method->getName());
|
|
$this->assertSame('\\' . ExampleClass::class, $method->getDeclaringClass());
|
|
$this->assertSame('$last, $first, ...$middle', $method->getParams(true));
|
|
$this->assertSame(['$last', '$first', '...$middle'], $method->getParams(false));
|
|
$this->assertSame('$last, $first = \'Barry\', ...$middle', $method->getParamsWithDefault(true));
|
|
$this->assertSame(['$last', '$first = \'Barry\'', '...$middle'], $method->getParamsWithDefault(false));
|
|
$this->assertSame(true, $method->shouldReturn());
|
|
}
|
|
|
|
/**
|
|
* Test the output of a class
|
|
*/
|
|
public function testEloquentBuilderOutput()
|
|
{
|
|
$reflectionClass = new \ReflectionClass(Builder::class);
|
|
$reflectionMethod = $reflectionClass->getMethod('with');
|
|
|
|
$method = new Method($reflectionMethod, 'Builder', $reflectionClass);
|
|
|
|
$output = '/**
|
|
* Set the relationships that should be eager loaded.
|
|
*
|
|
* @param mixed $relations
|
|
* @return \Illuminate\Database\Eloquent\Builder|static
|
|
* @static
|
|
*/';
|
|
$this->assertSame($output, $method->getDocComment(''));
|
|
$this->assertSame('with', $method->getName());
|
|
$this->assertSame('\\' . Builder::class, $method->getDeclaringClass());
|
|
$this->assertSame('$relations', $method->getParams(true));
|
|
$this->assertSame(['$relations'], $method->getParams(false));
|
|
$this->assertSame('$relations', $method->getParamsWithDefault(true));
|
|
$this->assertSame(['$relations'], $method->getParamsWithDefault(false));
|
|
$this->assertSame(true, $method->shouldReturn());
|
|
}
|
|
|
|
/**
|
|
* Test special characters in methods default values
|
|
*/
|
|
public function testDefaultSpecialChars()
|
|
{
|
|
$reflectionClass = new \ReflectionClass(ExampleClass::class);
|
|
$reflectionMethod = $reflectionClass->getMethod('setSpecialChars');
|
|
|
|
$method = new Method($reflectionMethod, 'Example', $reflectionClass);
|
|
$this->assertSame('$chars', $method->getParams(true));
|
|
$this->assertSame(['$chars'], $method->getParams(false));
|
|
$this->assertSame('$chars = \'$\\\'\\\\\'', $method->getParamsWithDefault(true));
|
|
$this->assertSame(['$chars = \'$\\\'\\\\\''], $method->getParamsWithDefault(false));
|
|
}
|
|
}
|
|
|
|
class ExampleClass
|
|
{
|
|
/**
|
|
* @param string $last
|
|
* @param string $first
|
|
* @param string $middle
|
|
*/
|
|
public function setName($last, $first = 'Barry', ...$middle)
|
|
{
|
|
return;
|
|
}
|
|
|
|
public function setSpecialChars($chars = "\$'\\")
|
|
{
|
|
return;
|
|
}
|
|
}
|