mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
* Improve PHPUnit assertions * composer fix-style Co-authored-by: laravel-ide-helper <[email protected]>
124 lines
3.9 KiB
PHP
124 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
|
|
|
use Barryvdh\LaravelIdeHelper\Method;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Foundation\Application;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class MethodTest 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 = <<<'DOC'
|
|
/**
|
|
*
|
|
*
|
|
* @param string $last
|
|
* @param string $first
|
|
* @param string $middle
|
|
* @static
|
|
*/
|
|
DOC;
|
|
$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->assertTrue($method->shouldReturn());
|
|
}
|
|
|
|
/**
|
|
* Test the output of a class
|
|
*/
|
|
public function testEloquentBuilderOutput()
|
|
{
|
|
if ((int) Application::VERSION < 8) {
|
|
$this->markTestSkipped('This test requires Laravel 8.0 or higher');
|
|
}
|
|
|
|
$reflectionClass = new \ReflectionClass(Builder::class);
|
|
$reflectionMethod = $reflectionClass->getMethod('with');
|
|
|
|
$method = new Method($reflectionMethod, 'Builder', $reflectionClass);
|
|
|
|
$output = <<<'DOC'
|
|
/**
|
|
* Set the relationships that should be eager loaded.
|
|
*
|
|
* @param string|array $relations
|
|
* @param string|\Closure|null $callback
|
|
* @return \Illuminate\Database\Eloquent\Builder|static
|
|
* @static
|
|
*/
|
|
DOC;
|
|
$this->assertSame($output, $method->getDocComment(''));
|
|
$this->assertSame('with', $method->getName());
|
|
$this->assertSame('\\' . Builder::class, $method->getDeclaringClass());
|
|
$this->assertSame('$relations, $callback', $method->getParams(true));
|
|
$this->assertSame(['$relations', '$callback'], $method->getParams(false));
|
|
$this->assertSame('$relations, $callback = null', $method->getParamsWithDefault(true));
|
|
$this->assertSame(['$relations', '$callback = null'], $method->getParamsWithDefault(false));
|
|
$this->assertTrue($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;
|
|
}
|
|
}
|