File: tests/Element/PhpFileTest.php

Recommend this page to a friend!
  Classes of WsdlToPhp  >  PHP Code Generator  >  tests/Element/PhpFileTest.php  >  Download  
File: tests/Element/PhpFileTest.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: Implement declare statement,
Code requires PHP 7.4,
Code cleaning and various tests
Date: 1 month ago
Size: 2,814 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace
WsdlToPhp\PhpGenerator\Tests\Element;

use
InvalidArgumentException;
use
TypeError;
use
WsdlToPhp\PhpGenerator\Element\PhpClass;
use
WsdlToPhp\PhpGenerator\Element\PhpDeclare;
use
WsdlToPhp\PhpGenerator\Element\PhpFunction;
use
WsdlToPhp\PhpGenerator\Element\PhpVariable;
use
WsdlToPhp\PhpGenerator\Element\PhpMethod;
use
WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
use
WsdlToPhp\PhpGenerator\Element\PhpConstant;
use
WsdlToPhp\PhpGenerator\Element\PhpFile;
use
WsdlToPhp\PhpGenerator\Tests\TestCase;

class
PhpFileTest extends TestCase
{
    public function
testException()
    {
       
$this->expectException(InvalidArgumentException::class);

       
$file = new PhpFile('foo');

       
$file->addChild(new PhpMethod('Foo'));
    }

    public function
testVariableToString()
    {
       
$file = new PhpFile('foo');

       
$file->addChild(new PhpVariable('foo', 1));

       
$this->assertSame("<?php\n\$foo = 1;\n", $file->toString());
    }

    public function
testConstantToString()
    {
       
$file = new PhpFile('foo');

       
$file->addChild(new PhpConstant('foo', 1));

       
$this->assertSame("<?php\ndefine('foo', 1);\n", $file->toString());
    }

    public function
testFunctionToString()
    {
       
$file = new PhpFile('foo');

       
$file->addChild(new PhpFunction('foo', [
           
'foo',
           
'sample',
           
'demo',
        ]));

       
$this->assertSame("<?php\nfunction foo(\$foo, \$sample, \$demo)\n{\n}\n", $file->toString());
    }

    public function
testAnnotationBlockToString()
    {
       
$file = new PhpFile('foo');

       
$file->addChild(new PhpAnnotationBlock([
           
'date is the key',
           
'time is the core key',
        ]));

       
$this->assertSame("<?php\n/**\n * date is the key\n * time is the core key\n */\n", $file->toString());
    }

    public function
testAddDeclareToString()
    {
       
$file = new PhpFile('foo');

       
$file->addChild(new PhpDeclare(PhpDeclare::DIRECTIVE_STRICT_TYPES, 1));

       
$this->assertSame("<?php\ndeclare(strict_types=1);\n", $file->toString());
    }

    public function
testAnnotationClassMethodBlockToString()
    {
       
$file = new PhpFile('foo');

       
$file->addChild(new PhpAnnotationBlock([
           
'date is the key',
           
'time is the core key',
        ]));

       
$class = new PhpClass('Foo');
       
$class->addChild(new PhpMethod('Bar'));
       
$file->addChild($class);

       
$this->assertSame("<?php\n/**\n * date is the key\n * time is the core key\n */\nclass Foo\n{\n public function Bar()\n {\n }\n}\n", $file->toString());
    }

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

        new
PhpFile(0);
    }
}


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