|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Stefna\PhpCodeBuilder\Tests\CodeHelper; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Stefna\PhpCodeBuilder\CodeHelper\ForeachCode; |
| 7 | +use Stefna\PhpCodeBuilder\CodeHelper\IfHelper\IfCollection; |
| 8 | +use Stefna\PhpCodeBuilder\CodeHelper\IfHelper\IfFactory; |
| 9 | +use Stefna\PhpCodeBuilder\CodeHelper\ReturnCode; |
| 10 | +use Stefna\PhpCodeBuilder\CodeHelper\VariableReference; |
| 11 | +use Stefna\PhpCodeBuilder\FlattenSource; |
| 12 | +use Stefna\PhpCodeBuilder\ValueObject\Identifier; |
| 13 | + |
| 14 | +final class IfCollectionTest extends TestCase |
| 15 | +{ |
| 16 | + public function testIfElseIf(): void |
| 17 | + { |
| 18 | + $collection = new IfCollection(); |
| 19 | + $var = new VariableReference('test'); |
| 20 | + $collection[] = IfFactory::instanceOf( |
| 21 | + $var, |
| 22 | + Identifier::fromString(WriteableServerConfigurationInterface::class), |
| 23 | + [ |
| 24 | + new ReturnCode($var), |
| 25 | + ] |
| 26 | + ); |
| 27 | + $collection[] = IfFactory::nullCheck($var, [ |
| 28 | + new ReturnCode($var), |
| 29 | + ]); |
| 30 | + |
| 31 | + $this->assertEquals( |
| 32 | + 'if ($test instanceof WriteableServerConfigurationInterface) { |
| 33 | + return $test; |
| 34 | +} |
| 35 | +elseif ($test === null) { |
| 36 | + return $test; |
| 37 | +} |
| 38 | +', |
| 39 | + FlattenSource::source($collection->getSourceArray()), |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testElse(): void |
| 44 | + { |
| 45 | + $collection = new IfCollection(); |
| 46 | + $var = new VariableReference('test'); |
| 47 | + $collection[] = IfFactory::methodExist($var, 'toString', [ |
| 48 | + new ReturnCode($var), |
| 49 | + ]); |
| 50 | + $collection[] = IfFactory::nullCheck($var, [ |
| 51 | + new ReturnCode($var), |
| 52 | + ]); |
| 53 | + |
| 54 | + $collection->addElse([ |
| 55 | + new ReturnCode($var), |
| 56 | + ]); |
| 57 | + |
| 58 | + $this->assertEquals( |
| 59 | + 'if (is_object($test) && method_exists($test, \'toString\')) { |
| 60 | + return $test; |
| 61 | +} |
| 62 | +elseif ($test === null) { |
| 63 | + return $test; |
| 64 | +} |
| 65 | +else { |
| 66 | + return $test; |
| 67 | +} |
| 68 | +', |
| 69 | + FlattenSource::source($collection->getSourceArray()), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function testLoopWithIfCollection(): void |
| 74 | + { |
| 75 | + $loop = new ForeachCode(new VariableReference('test'), function (VariableReference $key, VariableReference $value) { |
| 76 | + $collection = new IfCollection(); |
| 77 | + $collection[] = IfFactory::instanceOf($value, RequestBody::class, [ |
| 78 | + '$return[] = ' . $value->toString() . ';', |
| 79 | + ]); |
| 80 | + $collection[] = IfFactory::methodExist($value, 'toString', [ |
| 81 | + '$return[] = ' . $value->toString() . '->toString();', |
| 82 | + ]); |
| 83 | + |
| 84 | + return [ |
| 85 | + $collection, |
| 86 | + ]; |
| 87 | + }); |
| 88 | + |
| 89 | + $this->assertSame( |
| 90 | + 'foreach ($test as $key => $value) { |
| 91 | + if ($value instanceof RequestBody) { |
| 92 | + $return[] = $value; |
| 93 | + } |
| 94 | + elseif (is_object($value) && method_exists($value, \'toString\')) { |
| 95 | + $return[] = $value->toString(); |
| 96 | + } |
| 97 | +} |
| 98 | +', |
| 99 | + FlattenSource::source($loop->getSourceArray()) |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments