Skip to content

Commit

Permalink
tests: improved trait test
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 26, 2020
1 parent 4ffec80 commit ea2c8e8
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 33 deletions.
39 changes: 12 additions & 27 deletions tests/PhpGenerator/ClassType.from.trait.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,23 @@ use Nette\PhpGenerator\ClassType;


require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/traits.php';


/**
* Trait1
*/
trait Trait1
{
public function func1()
{
}
}

trait Trait2
{
protected function func2()
{
}
}

abstract class Class1
{
use Trait1;
use Trait2;
}

class Class2 extends Class1
{
}

$res[] = ClassType::from('Trait1');
$res[] = ClassType::from('Trait2');
$res[] = ClassType::from('Class1');
$res[] = ClassType::from('Class2');
$res[] = ClassType::from('Class3');

sameFile(__DIR__ . '/expected/ClassType.from.trait.expect', implode("\n", $res));


$res = [];
$res[] = ClassType::withBodiesFrom('Trait1');
$res[] = ClassType::withBodiesFrom('Trait2');
$res[] = ClassType::withBodiesFrom('Class1');
$res[] = ClassType::withBodiesFrom('Class2');
$res[] = ClassType::withBodiesFrom('Class3');

sameFile(__DIR__ . '/expected/ClassType.from.trait.bodies.expect', implode("\n", $res));
84 changes: 84 additions & 0 deletions tests/PhpGenerator/expected/ClassType.from.trait.bodies.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Trait1
*/
trait Trait1
{
public $x1;


public function f1()
{
echo 'Trait1::f1';
}
}

trait Trait2
{
protected $x2;
public $x1;


public function f2()
{
echo 'Trait2::f2';
}


public function f1()
{
}
}

class Class1 extends ParentClass
{
protected $x2;


public function f1()
{
}


public function f2()
{
}
}

class Class2 extends ParentClass
{
public $x1;
protected $x2;


public function f1()
{
echo 'Class2::f1';
}


public function f2()
{
}
}

class Class3 extends ParentClass
{
public $x1;
protected $x2;


public function f1()
{
echo 'Class3::f1';
}


public function f2()
{
}


public function aliased()
{
}
}
60 changes: 54 additions & 6 deletions tests/PhpGenerator/expected/ClassType.from.trait.expect
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,78 @@
*/
trait Trait1
{
public function func1()
public $x1;


public function f1()
{
}
}

trait Trait2
{
protected function func2()
protected $x2;
public $x1;


public function f2()
{
}


public function f1()
{
}
}

abstract class Class1
class Class1 extends ParentClass
{
public function func1()
protected $x2;


public function f1()
{
}


protected function func2()
public function f2()
{
}
}

class Class2 extends Class1
class Class2 extends ParentClass
{
public $x1;
protected $x2;


public function f1()
{
}


public function f2()
{
}
}

class Class3 extends ParentClass
{
public $x1;
protected $x2;


public function f1()
{
}


public function f2()
{
}


public function aliased()
{
}
}
79 changes: 79 additions & 0 deletions tests/PhpGenerator/fixtures/traits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/**
* Trait1
*/
trait Trait1
{
public $x1;


public function f1()
{
echo 'Trait1::f1';
}
}


trait Trait2
{
use Trait1;

protected $x2;


public function f2()
{
echo 'Trait2::f2';
}
}


class ParentClass
{
public $x1;


public function f1()
{
echo 'ParentClass::f1';
}
}


class Class1 extends ParentClass
{
use Trait2;
}


class Class2 extends ParentClass
{
use Trait2;

public $x1;


public function f1()
{
echo 'Class2::f1';
}
}


class Class3 extends ParentClass
{
use Trait2 {
Trait2::f1 as aliased;
}

public $x1;


public function f1()
{
echo 'Class3::f1';
}
}

0 comments on commit ea2c8e8

Please sign in to comment.