File: tests/Element/PhpMethodTest.php

Recommend this page to a friend!
  Classes of WsdlToPhp  >  PHP Code Generator  >  tests/Element/PhpMethodTest.php  >  Download  
File: tests/Element/PhpMethodTest.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: upgrade source in order to use most of PHP 7.2 features
refactor sources and tests
add feature to PHP CS settings
Date: 1 year ago
Size: 5,403 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace
WsdlToPhp\PhpGenerator\Tests\Element;

use
WsdlToPhp\PhpGenerator\Element\PhpVariable;
use
WsdlToPhp\PhpGenerator\Element\PhpInterface;
use
WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
use
WsdlToPhp\PhpGenerator\Element\PhpMethod;
use
WsdlToPhp\PhpGenerator\Tests\TestCase;

class
PhpMethodTest extends TestCase
{
    public function
testPublicGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
            [
               
'name' => 'demo',
               
'value' => 1,
            ],
            [
               
'name' => 'sample',
               
'value' => null,
            ],
            new
PhpFunctionParameter('deamon', true),
        ]);

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

    public function
testProtectedGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
        ],
PhpMethod::ACCESS_PROTECTED);

       
$this->assertSame('protected function foo($bar)', $method->getPhpDeclaration());
    }

    public function
testPrivateGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
        ],
PhpMethod::ACCESS_PRIVATE);

       
$this->assertSame('private function foo($bar)', $method->getPhpDeclaration());
    }

    public function
testPublicStaticGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
        ],
PhpMethod::ACCESS_PUBLIC, false, true);

       
$this->assertSame('public static function foo($bar)', $method->getPhpDeclaration());
    }

    public function
testProtectedStaticGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
        ],
PhpMethod::ACCESS_PROTECTED, false, true);

       
$this->assertSame('protected static function foo($bar)', $method->getPhpDeclaration());
    }

    public function
testPublicFinalGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
        ],
PhpMethod::ACCESS_PUBLIC, false, false, true);

       
$this->assertSame('final public function foo($bar)', $method->getPhpDeclaration());
    }

    public function
testAbstractPublicGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
        ],
PhpMethod::ACCESS_PUBLIC, true);

       
$this->assertSame('abstract public function foo($bar);', $method->getPhpDeclaration());
    }

    public function
testEmptyBodyPublicGetPhpDeclaration()
    {
       
$method = new PhpMethod('foo', [
           
'bar',
        ],
PhpMethod::ACCESS_PUBLIC, false, false, false, false);

       
$this->assertSame('public function foo($bar);', $method->getPhpDeclaration());
    }

   
/**
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage Element of type "object:WsdlToPhp\PhpGenerator\Element\PhpInterface" is not authorized, please provide one of these types: string, WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock, WsdlToPhp\PhpGenerator\Element\PhpVariable
     */
   
public function testAddChildWithException()
    {
       
$method = new PhpMethod('Foo');

       
$method->addChild(new PhpInterface('Bar'));
    }

    public function
testGetLineBeforeChildren()
    {
       
$method = new PhpMethod('Foo');
       
$method->setHasBody(true);

       
$line = $method->getLineBeforeChildren();

       
$this->assertSame('', $line);
    }

    public function
testGetLineAfterChildren()
    {
       
$method = new PhpMethod('Foo');
       
$method->setHasBody(true);

       
$line = $method->getLineAfterChildren();

       
$this->assertSame('', $line);
    }

    public function
testAddChildString()
    {
       
$method = new PhpMethod('Foo');

       
$method->addChild("\n");

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

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

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

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

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

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

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

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

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

   
/**
     * @expectedException \TypeError
     */
   
public function testExceptionMessageOnName()
    {
        new
PhpMethod(0);
    }
}


For more information send a message to info at phpclasses dot org.