PHP Classes

File: tests/Element/PhpFunctionTest.php

Recommend this page to a friend!
  Packages of WsdlToPhp   PHP Code Generator   tests/Element/PhpFunctionTest.php   Download  
File: tests/Element/PhpFunctionTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: PHP Code Generator
Generate PHP code elements programatically
Author: By
Last change: Refactor docker container to be able to run any PHP version from 7.4 (#14)

Thanks AI for 😄:
- update Dockerfile
- update docker-compose
- create Makefile

Update code with:
- CS Fixer
- Rector

#Refactor docker container to be able to run any PHP version from 7.4
Date: 5 months ago
Size: 5,435 bytes
 

 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace WsdlToPhp\PhpGenerator\Tests\Element;

use WsdlToPhp\PhpGenerator\Element\PhpFunction;
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
use WsdlToPhp\PhpGenerator\Element\PhpVariable;
use WsdlToPhp\PhpGenerator\Tests\TestCase;

/**
 * @internal
 *
 * @coversDefaultClass
 */
class PhpFunctionTest extends TestCase
{
    public function testGetPhpDeclaration()
    {
        $function = new PhpFunction('foo', [
            'bar',
            [
                'name' => 'demo',
                'value' => 1,
            ],
            [
                'name' => 'sample',
                'value' => null,
            ],
            new PhpFunctionParameter('deamon', true),
        ]);

        $this->assertSame('function foo($bar, $demo = 1, $sample = null, $deamon = true)', $function->getPhpDeclaration());
    }

    public function testGetPhpDeclarationWithReturnType()
    {
        $function = new PhpFunction('foo', [
            'bar',
            [
                'name' => 'demo',
                'value' => 1,
            ],
            [
                'name' => 'sample',
                'value' => null,
            ],
            new PhpFunctionParameter('deamon', true),
        ], 'void');

        $this->assertSame('function foo($bar, $demo = 1, $sample = null, $deamon = true): void', $function->getPhpDeclaration());
    }

    public function testAddChild()
    {
        $this->expectException(\InvalidArgumentException::class);

        $function = new PhpFunction('foo', []);

        $function->addChild(new PhpProperty('Bar'));
    }

    public function testSetParameters()
    {
        $this->expectException(\InvalidArgumentException::class);

        $function = new PhpFunction('foo', []);

        $function->setParameters([
            new PhpFunction('bar'),
        ]);
    }

    public function testSetName()
    {
        $function = new PhpFunction('foo', []);

        $function->setName($name = 'Partagé');

        $this->assertSame($name, $function->getName());
    }

    public function testAddChildVariable()
    {
        $function = new PhpFunction('foo', []);

        $function->addChild(new PhpVariable('bar'));

        $this->assertCount(1, $function->getChildren());
    }

    public function testSetReturnType()
    {
        $function = new PhpFunction('foo', []);

        $function->setReturnType($returnType = 'int');

        $this->assertSame($returnType, $function->getReturnType());
    }

    public function testAddChildString()
    {
        $function = new PhpFunction('foo', []);

        $function->addChild('bar');

        $this->assertCount(1, $function->getChildren());
    }

    public function testToStringEmptyBody()
    {
        $function = new PhpFunction('foo', [
            'bar',
            [
                'name' => 'demo',
                'value' => 1,
            ],
            [
                'name' => 'sample',
                'value' => null,
            ],
            new PhpFunctionParameter('deamon', true),
        ]);

        $this->assertSame("function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true)\n{\n}", $function->toString());
    }

    public function testToStringWithBody()
    {
        $function = new PhpFunction('foo', [
            'bar',
            [
                'name' => 'demo',
                'value' => 1,
            ],
            [
                'name' => 'sample',
                'value' => null,
            ],
            new PhpFunctionParameter('deamon', true),
        ]);

        $function
            ->addChild(new PhpVariable('bar', 1))
            ->addChild('return $bar;')
        ;

        $this->assertSame("function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true)\n{\n    \$bar = 1;\n    return \$bar;\n}", $function->toString());
    }

    public function testToStringWithBodyWithReturnType()
    {
        $function = new PhpFunction('foo', [
            'bar',
            [
                'name' => 'demo',
                'value' => 1,
            ],
            [
                'name' => 'sample',
                'value' => null,
            ],
            new PhpFunctionParameter('deamon', true),
        ], 'int');

        $function
            ->addChild(new PhpVariable('bar', 1))
            ->addChild('return $bar;')
        ;

        $this->assertSame("function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true): int\n{\n    \$bar = 1;\n    return \$bar;\n}", $function->toString());
    }

    public function testToStringWithBodyWithNullableReturnType()
    {
        $function = new PhpFunction('foo', [
            'bar',
            [
                'name' => 'demo',
                'value' => 1,
            ],
            [
                'name' => 'sample',
                'value' => null,
            ],
            new PhpFunctionParameter('deamon', true),
        ], '?int');

        $function
            ->addChild(new PhpVariable('bar', 1))
            ->addChild('return $bar;')
        ;

        $this->assertSame("function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true): ?int\n{\n    \$bar = 1;\n    return \$bar;\n}", $function->toString());
    }

    public function testExceptionMessageOnName()
    {
        $this->expectException(\TypeError::class);

        new PhpFunction(0);
    }
}