├── .gitignore ├── LICENSE ├── README.MD ├── composer.json ├── infection.json.dist ├── phpbench.json.dist ├── phpunit.xml.dist ├── psalm.xml ├── src ├── Collection │ ├── LinesCollection.php │ └── LinesCollectionInterface.php ├── Exception │ ├── ClassNotFoundException.php │ ├── MalformedPrintROutputException.php │ ├── NotImplementedException.php │ └── ReversePrintRException.php ├── Handler │ ├── ArrayHandler.php │ ├── CompoundHandlerAbstract.php │ ├── CompoundHandlerInterface.php │ ├── FloatHandler.php │ ├── HandlerInterface.php │ ├── IntegerHandler.php │ ├── NullHandler.php │ └── ObjectHandler.php ├── HandlerRunner.php ├── HandlerRunnerInterface.php ├── ReversePrintR.php └── Service │ ├── StringEndsWithService.php │ └── StringToLinesService.php └── tests ├── Benchmark ├── Benchmarks.md └── Collection │ └── CollectionGettingAndAddingLines.php ├── Integration ├── Fixtures │ ├── ClassWithAllTypes.php │ ├── ClassWithParent.php │ └── SimpleClass.php ├── Handler │ ├── ArrayHandlerIntegrationTest.php │ ├── NullHandlerIntegrationTest.php │ └── ObjectHandlerIntegrationTest.php └── ReversePrintRIntegrationTest.php └── Unit ├── Collection └── LinesCollectionUnitTest.php ├── DataProviders └── HandlersDataProvider.php ├── Handler ├── ArrayHandlerUnitTest.php ├── FloatHandlerUnitTest.php ├── IntegerHandlerUnitTest.php ├── NullHandlerUnitTest.php └── ObjectHandlerUnitTest.php └── Service ├── StringEndsWithServiceUnitTest.php └── StringToLinesServiceUnitTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | vendor/ 3 | infection.log -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 simivar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # Reverse print_r 2 | This library provides six different handlers for reversing output of PHP's `print_r` function back to original variables. 3 | If there's no handler available for a type it's returned as `string`. 4 | 5 | ## Assumptions 6 | - all values should be type-casted, not returned as `string` 7 | - empty string (`""`) is treated as `null` (see `NullHandler`) 8 | - integers are treated as integers (no `boolean` support) 9 | - multi-level `array` MUST be supported with type-casting 10 | - `public`, `protected` and `private` properties of Objects MUST be supported with type-casting 11 | 12 | ## Known issues 13 | - no support for Object Inheritance 14 | 15 | # Installation 16 | Package is available via [Composer](https://getcomposer.org/). 17 | 18 | ``` 19 | composer require simivar/reverse-print-r 20 | ``` 21 | 22 | # Usage 23 | ```php 24 | 'some text', 28 | 'integer' => 1, 29 | 'float' => 2.3, 30 | 'subArray' => [ 31 | 'Hello World.', 32 | ], 33 | ], true); 34 | 35 | $reverser = new \ReversePrintR\ReversePrintR($print_r_output); 36 | echo $reverser->reverse()['float']; 37 | // outputs "2.3" 38 | ``` 39 | 40 | ## Changing behavior of Handlers 41 | All handlers are defined as `final`, but thanks to Dependency Injection it's easy to change the behavior of library 42 | and it's type-casting. Let's say you want to keep all the empty strings `""` as string, not `null`. All you have to do 43 | is create your own `HandlerRunner` without `NullHandler`. 44 | 45 | ```php 46 | '', 50 | 'null' => null, 51 | ], true); 52 | 53 | $handlerRunner = new \ReversePrintR\HandlerRunner( 54 | new \ReversePrintR\Handler\FloatHandler(), 55 | new \ReversePrintR\Handler\IntegerHandler(), 56 | new \ReversePrintR\Handler\ArrayHandler(), 57 | new \ReversePrintR\Handler\ObjectHandler() 58 | ); 59 | 60 | $reverser = new \ReversePrintR\ReversePrintR($print_r_output, $handlerRunner); 61 | var_dump($reverser->reverse()['null']); 62 | // outputs "" 63 | ``` 64 | 65 | ## Own Handlers 66 | The same way to removed `NullHandler` you can add your own handlers. All you have to do is make sure that it implements 67 | `\ReversePrintR\Handler\HandlerInterface` and you are good to go. 68 | 69 | # Versioning 70 | Library is following [Semver](http://semver.org/). All minor and patch updates are backwards compatible. 71 | 72 | # License 73 | Please see the [license file](https://github.com/simivar/reverse-print-r/blob/master/LICENSE) for more information. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simivar/reverse-print-r", 3 | "description": "Library to reverse print_r output to PHP objects, arrays and scalar values.", 4 | "type": "library", 5 | "require-dev": { 6 | "phpunit/phpunit": "^9.0", 7 | "friendsofphp/php-cs-fixer": "^2.16", 8 | "vimeo/psalm": "^3.9", 9 | "infection/infection": "^0.15.3", 10 | "ergebnis/composer-normalize": "^2.2", 11 | "phpbench/phpbench": "@dev" 12 | }, 13 | "autoload": { 14 | "psr-4": { 15 | "ReversePrintR\\": "src/" 16 | } 17 | }, 18 | "autoload-dev": { 19 | "psr-4": { 20 | "Tests\\Unit\\ReversePrintR\\": "tests/Unit", 21 | "Tests\\Integration\\ReversePrintR\\": "tests/Integration" 22 | } 23 | }, 24 | "license": "MIT", 25 | "authors": [ 26 | { 27 | "name": "simivar", 28 | "email": "simivar@gmail.com" 29 | } 30 | ], 31 | "minimum-stability": "stable", 32 | "require": { 33 | "php": "^7.1" 34 | }, 35 | "scripts": { 36 | "psalm": "vendor/bin/psalm", 37 | "infection": "vendor/bin/infection", 38 | "test": "vendor/bin/phpunit", 39 | "test-unit": "vendor/bin/phpunit --testsuite Unit", 40 | "test-integration": "vendor/bin/phpunit --testsuite Integration", 41 | "bench": [ 42 | "Composer\\Config::disableProcessTimeout", 43 | "vendor/bin/phpbench run --report=my_report --output=my_output" 44 | ], 45 | "qa": [ 46 | "@psalm", 47 | "@test", 48 | "@infection" 49 | ] 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /infection.json.dist: -------------------------------------------------------------------------------- 1 | { 2 | "timeout": 10, 3 | "source": { 4 | "directories": [ 5 | "src" 6 | ] 7 | }, 8 | "phpUnit": { 9 | "configDir": "." 10 | }, 11 | "logs": { 12 | "text": "infection.log" 13 | }, 14 | "mutators": { 15 | "@default": true 16 | } 17 | } -------------------------------------------------------------------------------- /phpbench.json.dist: -------------------------------------------------------------------------------- 1 | { 2 | "bootstrap": "vendor/autoload.php", 3 | "path": "tests/Benchmark", 4 | "progress": "dots", 5 | "retry_threshold": 5, 6 | "time_unit": "seconds", 7 | "reports": { 8 | "my_report": { 9 | "generator": "table" 10 | } 11 | }, 12 | "outputs": { 13 | "my_output": { 14 | "extends": "markdown", 15 | "file": "tests/Benchmark/Benchmarks.md" 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | tests/Unit 16 | 17 | 18 | tests/Integration 19 | 20 | 21 | 22 | 23 | 24 | src 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /psalm.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/Collection/LinesCollection.php: -------------------------------------------------------------------------------- 1 | lines = array_reverse($lines); 29 | $this->currentLine = 1; 30 | } 31 | 32 | public function addNextLine(string $line): void 33 | { 34 | $this->lines[] = $line; 35 | } 36 | 37 | public function getNextLine(): ?string 38 | { 39 | if (!$this->hasNextLine()) { 40 | return null; 41 | } 42 | 43 | $this->currentLine++; 44 | return trim(array_pop($this->lines)); 45 | } 46 | 47 | public function lookupNextLine(): ?string 48 | { 49 | if (!$this->hasNextLine()) { 50 | return null; 51 | } 52 | 53 | return trim($this->lines[count($this->lines) - 1]); 54 | } 55 | 56 | public function hasNextLine(): bool 57 | { 58 | return count($this->lines) !== 0; 59 | } 60 | 61 | public function getCurrentLineNumber(): int 62 | { 63 | return $this->currentLine; 64 | } 65 | 66 | public function getLines(): array 67 | { 68 | return $this->lines; 69 | } 70 | 71 | public function isOneLineOutput(): bool 72 | { 73 | return count($this->lines) === 1; 74 | } 75 | } -------------------------------------------------------------------------------- /src/Collection/LinesCollectionInterface.php: -------------------------------------------------------------------------------- 1 | validateOpeningToken($linesCollection); 16 | $this->validateOpeningBracket($linesCollection); 17 | 18 | $resultArray = []; 19 | $foundClosingBracket = false; 20 | while ($lineData = $linesCollection->getNextLine()) { 21 | if ($lineData === ')') { 22 | $this->validateClosingBracket($linesCollection); 23 | 24 | $foundClosingBracket = true; 25 | break; 26 | } 27 | 28 | $parsedLine = $this->parseLine($lineData); 29 | $linesCollection->addNextLine($parsedLine['value']); 30 | $resultArray[$parsedLine['key']] = $this->getHandlerRunner()->run($linesCollection); 31 | } 32 | 33 | if (!$foundClosingBracket) { 34 | throw new MalformedPrintROutputException(sprintf( 35 | 'Could NOT find closing bracket of "Array" on line %d.', 36 | $linesCollection->getCurrentLineNumber() 37 | )); 38 | } 39 | 40 | return $resultArray; 41 | } 42 | 43 | private function parseLine(string $value): array 44 | { 45 | $pregResult = preg_match('#^\[(.*)] =>(.*)$#', $value, $matches); 46 | if ($pregResult === 1) { 47 | return [ 48 | 'key' => $matches[1], 49 | 'value' => $matches[2], 50 | ]; 51 | } 52 | 53 | throw new MalformedPrintROutputException(sprintf( 54 | 'Expected array\'s "[key] => value" line, not "%s".', 55 | $value 56 | )); 57 | } 58 | 59 | public function shouldBeUsed(string $line): bool 60 | { 61 | return $line === 'Array'; 62 | } 63 | 64 | protected function validateOpeningToken(LinesCollectionInterface $linesCollection): string 65 | { 66 | $line = $linesCollection->getNextLine(); 67 | if ($line !== 'Array') { 68 | throw new MalformedPrintROutputException(sprintf( 69 | 'Expected opening token "Array", got "%s" on line %d.', 70 | $line, 71 | $linesCollection->getCurrentLineNumber() 72 | )); 73 | } 74 | 75 | return 'Array'; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Handler/CompoundHandlerAbstract.php: -------------------------------------------------------------------------------- 1 | handlerRunner === null) { 18 | $this->handlerRunner = $handlerRunner; 19 | } 20 | } 21 | 22 | public function getHandlerRunner(): HandlerRunnerInterface 23 | { 24 | return $this->handlerRunner; 25 | } 26 | 27 | abstract protected function validateOpeningToken(LinesCollectionInterface $linesCollection): string; 28 | 29 | protected function validateOpeningBracket(LinesCollectionInterface $linesCollection): void 30 | { 31 | if ($linesCollection->getNextLine() !== '(') { 32 | throw new MalformedPrintROutputException(sprintf( 33 | 'Expected opening bracket after compound type keyword on line %d.', 34 | $linesCollection->getCurrentLineNumber() 35 | )); 36 | } 37 | } 38 | 39 | protected function validateClosingBracket(LinesCollectionInterface $linesCollection): void 40 | { 41 | $nextLine = $linesCollection->getNextLine(); 42 | if ($nextLine !== '' && $linesCollection->hasNextLine()) { 43 | throw new MalformedPrintROutputException(sprintf( 44 | 'Closing bracket of compound type MUST be followed by empty line on line %d.', 45 | $linesCollection->getCurrentLineNumber() 46 | )); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Handler/CompoundHandlerInterface.php: -------------------------------------------------------------------------------- 1 | getNextLine(); 13 | } 14 | 15 | public function shouldBeUsed(string $line): bool 16 | { 17 | $integerHandler = new IntegerHandler(); 18 | 19 | $asFloat = (float)$line; 20 | return ((string)$asFloat) === $line && !$integerHandler->shouldBeUsed($line); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Handler/HandlerInterface.php: -------------------------------------------------------------------------------- 1 | getNextLine(); 13 | } 14 | 15 | public function shouldBeUsed(string $line): bool 16 | { 17 | $asInt = (int)$line; 18 | return ((string)$asInt) === $line; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Handler/NullHandler.php: -------------------------------------------------------------------------------- 1 | getNextLine(); 13 | 14 | return null; 15 | } 16 | 17 | public function shouldBeUsed(string $line): bool 18 | { 19 | return $line === ''; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Handler/ObjectHandler.php: -------------------------------------------------------------------------------- 1 | validateOpeningToken($linesCollection); 20 | $this->validateOpeningBracket($linesCollection); 21 | $reflectionClass = $this->createClassReflection($className); 22 | 23 | $resultObject = $reflectionClass->newInstanceWithoutConstructor(); 24 | $foundClosingBracket = false; 25 | while ($lineData = $linesCollection->getNextLine()) { 26 | if ($lineData === ')') { 27 | $this->validateClosingBracket($linesCollection); 28 | 29 | $foundClosingBracket = true; 30 | break; 31 | } 32 | 33 | $parsedLine = $this->parseLine($lineData); 34 | $linesCollection->addNextLine($parsedLine['value']); 35 | $value = $this->getHandlerRunner()->run($linesCollection); 36 | 37 | if ($className === 'stdClass') { 38 | $resultObject->{$parsedLine['property']} = $value; 39 | } else { 40 | $property = $reflectionClass->getProperty($parsedLine['property']); 41 | switch ($parsedLine['visibility']) { 42 | case ReflectionProperty::IS_PRIVATE: 43 | case ReflectionProperty::IS_PROTECTED: 44 | $property->setAccessible(true); 45 | $property->setValue($resultObject, $value); 46 | $property->setAccessible(false); 47 | break; 48 | case ReflectionProperty::IS_PUBLIC: 49 | $property->setValue($resultObject, $value); 50 | } 51 | } 52 | } 53 | 54 | if (!$foundClosingBracket) { 55 | throw new MalformedPrintROutputException(sprintf( 56 | 'Could NOT find closing bracket of "Object" on line %d.', 57 | $linesCollection->getCurrentLineNumber() 58 | )); 59 | } 60 | 61 | return $resultObject; 62 | } 63 | 64 | private function parseLine(string $line) 65 | { 66 | $pregResult = preg_match('#^\[(.*)] =>(.*)$#', $line, $matches); 67 | if ($pregResult === 1) { 68 | $visibility = ReflectionProperty::IS_PUBLIC; 69 | if (StringEndsWithService::endsWith($matches[1], ':private')) { 70 | $visibility = ReflectionProperty::IS_PRIVATE; 71 | } 72 | 73 | if (StringEndsWithService::endsWith($matches[1], ':protected')) { 74 | $visibility = ReflectionProperty::IS_PROTECTED; 75 | } 76 | 77 | return [ 78 | 'property' => explode(':', $matches[1])[0], 79 | 'visibility' => $visibility, 80 | 'value' => $matches[2], 81 | ]; 82 | } 83 | 84 | throw new MalformedPrintROutputException(sprintf( 85 | 'Expected object\'s "[key] => value" line, not "%s".', 86 | $line 87 | )); 88 | } 89 | 90 | private function createClassReflection(string $className): ReflectionClass 91 | { 92 | if (!class_exists($className)) { 93 | throw new ClassNotFoundException(sprintf( 94 | 'Could NOT find class "%s".', 95 | $className 96 | )); 97 | } 98 | 99 | return new ReflectionClass($className); 100 | } 101 | 102 | public function shouldBeUsed(string $line): bool 103 | { 104 | return preg_match('/^\S+? Object$/', $line) === 1; 105 | } 106 | 107 | protected function validateOpeningToken(LinesCollectionInterface $linesCollection): string 108 | { 109 | $line = $linesCollection->getNextLine(); 110 | $result = preg_match('/^(\S+?) Object$/', $line, $matches); 111 | if ($result !== 1) { 112 | throw new MalformedPrintROutputException(sprintf( 113 | 'Opening token of Object has to be in "ClassName Object" format, got "%s" on line %d.', 114 | $line, 115 | $linesCollection->getCurrentLineNumber() 116 | )); 117 | } 118 | 119 | return $matches[1]; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/HandlerRunner.php: -------------------------------------------------------------------------------- 1 | handlers = $handlers; 17 | } 18 | 19 | /** 20 | * @inheritDoc 21 | */ 22 | public function run(LinesCollectionInterface $linesCollection) 23 | { 24 | $returnValue = ''; 25 | $usedHandler = false; 26 | 27 | $nextLine = $linesCollection->lookupNextLine(); 28 | foreach ($this->getHandlers() as $handler) { 29 | if (!$handler->shouldBeUsed($nextLine)) { 30 | continue; 31 | } 32 | 33 | if ($handler instanceof CompoundHandlerInterface) { 34 | $handler->setHandlerRunner($this); 35 | } 36 | 37 | $returnValue = $handler->handle($linesCollection); 38 | $usedHandler = true; 39 | } 40 | 41 | if (!$usedHandler) { 42 | $linesCollection->getNextLine(); 43 | return $nextLine; 44 | } 45 | 46 | return $returnValue; 47 | } 48 | 49 | /** 50 | * @inheritDoc 51 | */ 52 | public function getHandlers(): array 53 | { 54 | return $this->handlers; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/HandlerRunnerInterface.php: -------------------------------------------------------------------------------- 1 | linesCollection = new LinesCollection(StringToLinesService::split($printROutput)); 25 | $this->handlerRunner = $handlerRunner; 26 | } 27 | 28 | public function reverse() 29 | { 30 | return $this->getHandlerRunner()->run($this->linesCollection); 31 | } 32 | 33 | private function getHandlerRunner(): HandlerRunnerInterface 34 | { 35 | if ($this->handlerRunner === null) { 36 | $this->handlerRunner = new HandlerRunner( 37 | new FloatHandler(), 38 | new IntegerHandler(), 39 | new NullHandler(), 40 | new ArrayHandler(), 41 | new ObjectHandler() 42 | ); 43 | } 44 | 45 | return $this->handlerRunner; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Service/StringEndsWithService.php: -------------------------------------------------------------------------------- 1 | array = array_fill(0, self::ARRAY_LENGTH, 'line'); 32 | $this->array[0] = 'first line'; 33 | $this->array[999] = 'last line'; 34 | } 35 | 36 | public function benchArrayShift(): void 37 | { 38 | for ($i = 0; $i < self::ARRAY_LENGTH; $i++) { 39 | array_shift($this->array); 40 | array_unshift($this->array, 'new first line'); 41 | } 42 | } 43 | 44 | public function benchReverseAndPop(): void 45 | { 46 | array_reverse($this->array); 47 | for ($i = 0; $i < self::ARRAY_LENGTH; $i++) { 48 | array_pop($this->array); 49 | $this->array[] = 'new first line'; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/Integration/Fixtures/ClassWithAllTypes.php: -------------------------------------------------------------------------------- 1 | 4, 16 | 'f' => 5.6, 17 | 's' => 'in array', 18 | 'n' => null, 19 | ]; 20 | 21 | private $null = null; 22 | 23 | private $class; 24 | 25 | public function __construct() 26 | { 27 | $this->class = new SimpleClass(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Integration/Fixtures/ClassWithParent.php: -------------------------------------------------------------------------------- 1 | handler = new HandlerRunner( 24 | new IntegerHandler(), 25 | new FloatHandler(), 26 | new NullHandler(), 27 | new ArrayHandler(), 28 | new ObjectHandler() 29 | ); 30 | } 31 | 32 | public function testReturnsProperValuesWithSubarray(): void 33 | { 34 | $arrayAsString = [ 35 | 'Array', 36 | '(', 37 | ' [subArray] => Array', 38 | ' (', 39 | ' [0] => value', 40 | ' [1] => 1', 41 | ' [2] => 2.3', 42 | ' [3] => ', 43 | ' )', 44 | '', 45 | ')', 46 | ]; 47 | $handler = new ArrayHandler(); 48 | $handler->setHandlerRunner($this->handler); 49 | $values = $handler->handle(new LinesCollection($arrayAsString)); 50 | $this->assertSame([ 51 | 'subArray' => [ 52 | 'value', 53 | 1, 54 | 2.3, 55 | null, 56 | ], 57 | ], $values); 58 | } 59 | 60 | public function testReturnsProperStringValue(): void 61 | { 62 | $handler = new ArrayHandler(); 63 | $handler->setHandlerRunner($this->handler); 64 | $values = $handler->handle(new LinesCollection([ 65 | 'Array', 66 | '(', 67 | ' [0] => value', 68 | ')', 69 | ])); 70 | 71 | $this->assertSame(['value'], $values); 72 | } 73 | 74 | public function testReturnsProperIntegerValue(): void 75 | { 76 | $handler = new ArrayHandler(); 77 | $handler->setHandlerRunner($this->handler); 78 | $values = $handler->handle(new LinesCollection([ 79 | 'Array', 80 | '(', 81 | ' [0] => 1', 82 | ')', 83 | ])); 84 | 85 | $this->assertSame([1], $values); 86 | } 87 | 88 | public function testReturnsProperFloatValue(): void 89 | { 90 | $handler = new ArrayHandler(); 91 | $handler->setHandlerRunner($this->handler); 92 | $values = $handler->handle(new LinesCollection([ 93 | 'Array', 94 | '(', 95 | ' [0] => 2.3', 96 | ')', 97 | ])); 98 | 99 | $this->assertSame([2.3], $values); 100 | } 101 | 102 | public function testReturnsProperNullValue(): void 103 | { 104 | $handler = new ArrayHandler(); 105 | $handler->setHandlerRunner($this->handler); 106 | $values = $handler->handle(new LinesCollection([ 107 | 'Array', 108 | '(', 109 | ' [0] => ', 110 | ')', 111 | ])); 112 | 113 | $this->assertSame([null], $values); 114 | } 115 | 116 | public function testThrowsExceptionWhenMissingNewlineAfterSubarrayClosingBracket(): void 117 | { 118 | $this->expectException(MalformedPrintROutputException::class); 119 | $this->expectExceptionMessageMatches( 120 | '/Could NOT find closing bracket of "Array" on line [0-9]{1,}\./' 121 | ); 122 | 123 | $arrayAsString = [ 124 | 'Array', 125 | '(', 126 | ' [subArray] => Array', 127 | ' (', 128 | ' [0] => value', 129 | ' [1] => 1', 130 | ' [2] => 2.3', 131 | ' [3] => ', 132 | ' )', 133 | ')', 134 | ]; 135 | $handler = new ArrayHandler(); 136 | $handler->setHandlerRunner($this->handler); 137 | $handler->handle(new LinesCollection($arrayAsString)); 138 | } 139 | } -------------------------------------------------------------------------------- /tests/Integration/Handler/NullHandlerIntegrationTest.php: -------------------------------------------------------------------------------- 1 | assertNull($handler->handle($collection)); 18 | } 19 | } -------------------------------------------------------------------------------- /tests/Integration/Handler/ObjectHandlerIntegrationTest.php: -------------------------------------------------------------------------------- 1 | handler = new HandlerRunner( 24 | new IntegerHandler(), 25 | new FloatHandler(), 26 | new NullHandler(), 27 | new ArrayHandler(), 28 | new ObjectHandler() 29 | ); 30 | } 31 | 32 | public function testReturnsProperValuesWithSubarray(): void 33 | { 34 | $arrayAsString = [ 35 | 'Array', 36 | '(', 37 | ' [subArray] => Array', 38 | ' (', 39 | ' [0] => value', 40 | ' [1] => 1', 41 | ' [2] => 2.3', 42 | ' [3] => ', 43 | ' )', 44 | '', 45 | ')', 46 | ]; 47 | $handler = new ArrayHandler(); 48 | $handler->setHandlerRunner($this->handler); 49 | $values = $handler->handle(new LinesCollection($arrayAsString)); 50 | $this->assertSame([ 51 | 'subArray' => [ 52 | 'value', 53 | 1, 54 | 2.3, 55 | null, 56 | ], 57 | ], $values); 58 | } 59 | 60 | public function testReturnsProperStringValue(): void 61 | { 62 | $handler = new ArrayHandler(); 63 | $handler->setHandlerRunner($this->handler); 64 | $values = $handler->handle(new LinesCollection([ 65 | 'Array', 66 | '(', 67 | ' [0] => value', 68 | ')', 69 | ])); 70 | 71 | $this->assertSame(['value'], $values); 72 | } 73 | 74 | public function testReturnsProperIntegerValue(): void 75 | { 76 | $handler = new ArrayHandler(); 77 | $handler->setHandlerRunner($this->handler); 78 | $values = $handler->handle(new LinesCollection([ 79 | 'Array', 80 | '(', 81 | ' [0] => 1', 82 | ')', 83 | ])); 84 | 85 | $this->assertSame([1], $values); 86 | } 87 | 88 | public function testReturnsProperFloatValue(): void 89 | { 90 | $handler = new ArrayHandler(); 91 | $handler->setHandlerRunner($this->handler); 92 | $values = $handler->handle(new LinesCollection([ 93 | 'Array', 94 | '(', 95 | ' [0] => 2.3', 96 | ')', 97 | ])); 98 | 99 | $this->assertSame([2.3], $values); 100 | } 101 | 102 | public function testReturnsProperNullValue(): void 103 | { 104 | $handler = new ArrayHandler(); 105 | $handler->setHandlerRunner($this->handler); 106 | $values = $handler->handle(new LinesCollection([ 107 | 'Array', 108 | '(', 109 | ' [0] => ', 110 | ')', 111 | ])); 112 | 113 | $this->assertSame([null], $values); 114 | } 115 | 116 | public function testThrowsExceptionWhenMissingNewlineAfterSubarrayClosingBracket(): void 117 | { 118 | $this->expectException(MalformedPrintROutputException::class); 119 | $this->expectExceptionMessageMatches( 120 | '/Could NOT find closing bracket of "Array" on line [0-9]{1,}\./' 121 | ); 122 | 123 | $arrayAsString = [ 124 | 'Array', 125 | '(', 126 | ' [subArray] => Array', 127 | ' (', 128 | ' [0] => value', 129 | ' [1] => 1', 130 | ' [2] => 2.3', 131 | ' [3] => ', 132 | ' )', 133 | ')', 134 | ]; 135 | $handler = new ArrayHandler(); 136 | $handler->setHandlerRunner($this->handler); 137 | $handler->handle(new LinesCollection($arrayAsString)); 138 | } 139 | } -------------------------------------------------------------------------------- /tests/Integration/ReversePrintRIntegrationTest.php: -------------------------------------------------------------------------------- 1 | assertNull($reverser->reverse()); 17 | } 18 | 19 | public function testIntegerIsInteger(): void 20 | { 21 | $reverser = new ReversePrintR(print_r(1, true)); 22 | $this->assertSame(1, $reverser->reverse()); 23 | } 24 | 25 | public function testIntegerAsStringIsInteger(): void 26 | { 27 | $reverser = new ReversePrintR(print_r('1', true)); 28 | $this->assertSame(1, $reverser->reverse()); 29 | } 30 | 31 | public function testFloatIsFloat(): void 32 | { 33 | $reverser = new ReversePrintR(print_r(2.3, true)); 34 | $this->assertSame(2.3, $reverser->reverse()); 35 | } 36 | 37 | public function testFloatAsStringIsFloat(): void 38 | { 39 | $reverser = new ReversePrintR(print_r('4.5', true)); 40 | $this->assertSame(4.5, $reverser->reverse()); 41 | } 42 | 43 | public function testStringIsString(): void 44 | { 45 | $reverser = new ReversePrintR(print_r('simple string', true)); 46 | $this->assertSame('simple string', $reverser->reverse()); 47 | } 48 | 49 | public function testEmptyStringIsNull(): void 50 | { 51 | $reverser = new ReversePrintR(print_r('', true)); 52 | $this->assertNull($reverser->reverse()); 53 | } 54 | 55 | public function testEmptyArray(): void 56 | { 57 | $array = []; 58 | $reverser = new ReversePrintR(print_r($array, true)); 59 | $this->assertSame($array, $reverser->reverse()); 60 | } 61 | 62 | public function testArrayWithoutKeys(): void 63 | { 64 | $array = [ 65 | 'string', 66 | 1, 67 | 2.3, 68 | null 69 | ]; 70 | $reverser = new ReversePrintR(print_r($array, true)); 71 | $this->assertSame($array, $reverser->reverse()); 72 | } 73 | 74 | public function testArrayWithAllValueTypes(): void 75 | { 76 | $array = [ 77 | 'firstInt' => 1, 78 | 'firstString' => 'string', 79 | 'firstFloat' => 2.3, 80 | 'firstSubArray' => [ 81 | 'subKey' => 'subValue', 82 | ], 83 | ]; 84 | $reverser = new ReversePrintR(print_r($array, true)); 85 | $this->assertSame($array, $reverser->reverse()); 86 | } 87 | 88 | public function testOneLineArrayThrowsException(): void 89 | { 90 | $reverser = new ReversePrintR('Array'); 91 | 92 | $this->expectException(MalformedPrintROutputException::class); 93 | 94 | $reverser->reverse(); 95 | } 96 | 97 | public function testObjectWithAllValueTypes(): void 98 | { 99 | $class = new ClassWithAllTypes(); 100 | $reverser = new ReversePrintR(print_r($class, true)); 101 | 102 | $this->assertEquals($class, $reverser->reverse()); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tests/Unit/Collection/LinesCollectionUnitTest.php: -------------------------------------------------------------------------------- 1 | expectException(MalformedPrintROutputException::class); 15 | $this->expectExceptionMessage('Collection must contain at least one line.'); 16 | 17 | new LinesCollection([]); 18 | } 19 | 20 | public function testAddNextLineAddsLineAtEnd(): void 21 | { 22 | $collection = new LinesCollection(['first line']); 23 | $collection->addNextLine('second line'); 24 | 25 | $this->assertSame('second line', $collection->getNextLine()); 26 | $this->assertSame('first line', $collection->getNextLine()); 27 | } 28 | 29 | public function testGetNextLineReturnsLine(): void 30 | { 31 | $collection = new LinesCollection(['first line', 'second line']); 32 | 33 | $this->assertSame('first line', $collection->getNextLine()); 34 | $this->assertSame('second line', $collection->getNextLine()); 35 | } 36 | 37 | public function testGetNextLineRemovesLine(): void 38 | { 39 | $collection = new LinesCollection(['first line', 'second line']); 40 | 41 | $collection->getNextLine(); 42 | $this->assertSame([0 => 'second line'], $collection->getLines()); 43 | } 44 | 45 | public function testGetNextLineTrimsLines(): void 46 | { 47 | $array = [' first line ']; 48 | $collection = new LinesCollection($array); 49 | 50 | $this->assertSame('first line', $collection->getNextLine()); 51 | } 52 | 53 | public function testLookupNextLineDoesntRemoveLine(): void 54 | { 55 | $collection = new LinesCollection(['first line', 'second line']); 56 | 57 | $collection->lookupNextLine(); 58 | $this->assertSame(['second line', 'first line'], $collection->getLines()); 59 | } 60 | 61 | public function testLookupNextLineTrimsLines(): void 62 | { 63 | $array = [' first line ']; 64 | $collection = new LinesCollection($array); 65 | 66 | $this->assertSame('first line', $collection->lookupNextLine()); 67 | } 68 | 69 | public function testGetNextLineTrimsLine(): void 70 | { 71 | $collection = new LinesCollection([' first line ']); 72 | $this->assertSame('first line', $collection->getNextLine()); 73 | } 74 | 75 | public function testGetNextLineIncreasesCounter(): void 76 | { 77 | $collection = new LinesCollection(['first line', 'second line']); 78 | 79 | $collection->getNextLine(); 80 | $this->assertSame(2, $collection->getCurrentLineNumber()); 81 | } 82 | 83 | public function testIsOneLineReturnsTrueOnSingleValueArray(): void 84 | { 85 | $collection = new LinesCollection(['']); 86 | $this->assertTrue($collection->isOneLineOutput()); 87 | } 88 | 89 | public function testIsOneLineReturnsFalseOnMultilineValueArray(): void 90 | { 91 | $collection = new LinesCollection(['first line', 'second line']); 92 | $this->assertFalse($collection->isOneLineOutput()); 93 | } 94 | } -------------------------------------------------------------------------------- /tests/Unit/DataProviders/HandlersDataProvider.php: -------------------------------------------------------------------------------- 1 | ['Array'], 12 | ]; 13 | } 14 | 15 | public function provideObjectValues(): array 16 | { 17 | return [ 18 | 'object string' => ['stdClass Object'], 19 | ]; 20 | } 21 | 22 | public static function provideIntegerValues(): array 23 | { 24 | return [ 25 | 'integer as string' => ['1'], 26 | ]; 27 | } 28 | 29 | public static function provideNullValues(): array 30 | { 31 | return [ 32 | 'empty string' => [''], 33 | ]; 34 | } 35 | 36 | public static function provideStringValues(): array 37 | { 38 | return [ 39 | 'string' => ['test string'], 40 | 'string beginning with int' => ['1 and only'], 41 | 'string beginning with float' => ['1.52 and only'], 42 | 'string ending with int' => ['we are the 1'], 43 | 'string with int inside' => ['only 1 int'], 44 | ]; 45 | } 46 | 47 | public static function provideFloatValues(): array 48 | { 49 | return [ 50 | 'float as string' => ['1.23'], 51 | ]; 52 | } 53 | } -------------------------------------------------------------------------------- /tests/Unit/Handler/ArrayHandlerUnitTest.php: -------------------------------------------------------------------------------- 1 | expectException(MalformedPrintROutputException::class); 17 | $this->expectExceptionMessageMatches( 18 | '/Expected opening token "Array", got "(.*)" on line [0-9]{1,}\./' 19 | ); 20 | 21 | $mock = $this->createMock(LinesCollectionInterface::class); 22 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls( 23 | '(', 24 | '[0] => value', 25 | ')' 26 | ); 27 | 28 | $handler = new ArrayHandler(); 29 | $handler->handle($mock); 30 | } 31 | 32 | public function testThrowsExceptionWhenArrayMissingOpeningBracket(): void 33 | { 34 | $this->expectException(MalformedPrintROutputException::class); 35 | $this->expectExceptionMessageMatches( 36 | '/Expected opening bracket after compound type keyword on line [0-9]{1,}\./' 37 | ); 38 | 39 | $mock = $this->createMock(LinesCollectionInterface::class); 40 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls( 41 | 'Array', 42 | '[0] => value', 43 | ')' 44 | ); 45 | 46 | $handler = new ArrayHandler(); 47 | $handler->handle($mock); 48 | } 49 | 50 | public function testThrowsExceptionWhenMalformedArrayValue(): void 51 | { 52 | $mock = $this->createMock(LinesCollectionInterface::class); 53 | $return = [ 54 | 'Array', 55 | '(', 56 | 'test => value', 57 | ')', 58 | ]; 59 | $mock->method('getLines')->willReturn($return); 60 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls( 61 | 'Array', 62 | '(', 63 | 'test => value', 64 | ')' 65 | ); 66 | 67 | $this->expectException(MalformedPrintROutputException::class); 68 | $this->expectExceptionMessageMatches( 69 | '/Expected array\'s "\[key] => value" line, not (.*)\./' 70 | ); 71 | 72 | $handler = new ArrayHandler(); 73 | $handler->handle($mock); 74 | } 75 | 76 | public function testThrowsExceptionWhenArrayMissingClosingBracket(): void 77 | { 78 | $this->expectException(MalformedPrintROutputException::class); 79 | $this->expectExceptionMessageMatches( 80 | '/Could NOT find closing bracket of "Array" on line [0-9]{1,}\./' 81 | ); 82 | 83 | $mock = $this->createMock(LinesCollectionInterface::class); 84 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls( 85 | 'Array', 86 | '(', 87 | '[0] => value' 88 | ); 89 | 90 | $handlerRunnerMock = $this->createMock(HandlerRunnerInterface::class); 91 | $handlerRunnerMock->method('run')->willReturn('value'); 92 | 93 | $handler = new ArrayHandler(); 94 | $handler->setHandlerRunner($handlerRunnerMock); 95 | $handler->handle($mock); 96 | } 97 | 98 | /** 99 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideNullValues 100 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideStringValues 101 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideIntegerValues 102 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideFloatValues 103 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideObjectValues 104 | */ 105 | public function testValidateReturnsFalseOnNotArrayLikeValue(string $value): void 106 | { 107 | $handler = new ArrayHandler(); 108 | $this->assertFalse($handler->shouldBeUsed($value)); 109 | } 110 | 111 | /** 112 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideArrayValues 113 | */ 114 | public function testValidateReturnsTrueOnArrayLikeValues(string $value): void 115 | { 116 | $handler = new ArrayHandler(); 117 | $this->assertTrue($handler->shouldBeUsed($value)); 118 | } 119 | } -------------------------------------------------------------------------------- /tests/Unit/Handler/FloatHandlerUnitTest.php: -------------------------------------------------------------------------------- 1 | createMock(LinesCollectionInterface::class); 15 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls('1.2', '1.2'); 16 | 17 | $handler = new FloatHandler(); 18 | $this->assertSame(1.2, $handler->handle($mock)); 19 | } 20 | 21 | /** 22 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideNullValues 23 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideStringValues 24 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideIntegerValues 25 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideArrayValues 26 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideObjectValues 27 | */ 28 | public function testValidateReturnsFalseOnNotFloatLikeValue(string $value): void 29 | { 30 | $handler = new FloatHandler(); 31 | $this->assertFalse($handler->shouldBeUsed($value)); 32 | } 33 | 34 | /** 35 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideFloatValues 36 | */ 37 | public function testValidateReturnsTrueOnFloatLikeValue(string $value): void 38 | { 39 | $handler = new FloatHandler(); 40 | $this->assertTrue($handler->shouldBeUsed($value)); 41 | } 42 | } -------------------------------------------------------------------------------- /tests/Unit/Handler/IntegerHandlerUnitTest.php: -------------------------------------------------------------------------------- 1 | createMock(LinesCollectionInterface::class); 15 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls('3', '3'); 16 | 17 | $handler = new IntegerHandler(); 18 | $this->assertSame(3, $handler->handle($mock)); 19 | } 20 | 21 | /** 22 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideNullValues 23 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideStringValues 24 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideFloatValues 25 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideArrayValues 26 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideObjectValues 27 | */ 28 | public function testValidateReturnsFalseOnNotIntegerLikeValue(string $value): void 29 | { 30 | $handler = new IntegerHandler(); 31 | $this->assertFalse($handler->shouldBeUsed($value)); 32 | } 33 | 34 | /** 35 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideIntegerValues 36 | */ 37 | public function testValidateReturnsTrueOnIntegerLikeValue(string $value): void 38 | { 39 | $handler = new IntegerHandler(); 40 | $this->assertTrue($handler->shouldBeUsed($value)); 41 | } 42 | } -------------------------------------------------------------------------------- /tests/Unit/Handler/NullHandlerUnitTest.php: -------------------------------------------------------------------------------- 1 | createMock(LinesCollectionInterface::class); 15 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls(''); 16 | $mock->method('lookupNextLine')->willReturnOnConsecutiveCalls(''); 17 | 18 | $handler = new NullHandler(); 19 | $this->assertNull($handler->handle($mock)); 20 | } 21 | 22 | /** 23 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideFloatValues 24 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideStringValues 25 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideIntegerValues 26 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideArrayValues 27 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideObjectValues 28 | */ 29 | public function testValidateReturnsFalseOnNotNullLikeValue(string $value): void 30 | { 31 | $handler = new NullHandler(); 32 | $this->assertFalse($handler->shouldBeUsed($value)); 33 | } 34 | 35 | /** 36 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideNullValues 37 | */ 38 | public function testValidateReturnsTrueOnNullLikeValue(string $value): void 39 | { 40 | $handler = new NullHandler(); 41 | $this->assertTrue($handler->shouldBeUsed($value)); 42 | } 43 | } -------------------------------------------------------------------------------- /tests/Unit/Handler/ObjectHandlerUnitTest.php: -------------------------------------------------------------------------------- 1 | expectException(MalformedPrintROutputException::class); 17 | $this->expectExceptionMessage( 18 | 'Opening token of Object has to be in "ClassName Object" format, got "Wrong Class Name Object" on line 0.' 19 | ); 20 | 21 | $mock = $this->createMock(LinesCollectionInterface::class); 22 | $return = [ 23 | 'Wrong Class Name Object', 24 | '(', 25 | ')', 26 | ]; 27 | $mock->method('getLines')->willReturn($return); 28 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls( 29 | 'Wrong Class Name Object', 30 | '(', 31 | ')' 32 | ); 33 | 34 | $handler = new ObjectHandler(); 35 | $handler->handle($mock); 36 | } 37 | 38 | public function testThrowsExceptionOnNotLoadedClass(): void 39 | { 40 | $this->expectException(ClassNotFoundException::class); 41 | $this->expectExceptionMessage('Could NOT find class "Some\Random\Namespace\ClassName".'); 42 | 43 | $mock = $this->createMock(LinesCollectionInterface::class); 44 | $return = [ 45 | 'Some\\Random\\Namespace\\ClassName Object', 46 | '(', 47 | ')', 48 | ]; 49 | $mock->method('getLines')->willReturn($return); 50 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls( 51 | 'Some\\Random\\Namespace\\ClassName Object', 52 | '(', 53 | ')' 54 | ); 55 | 56 | $handler = new ObjectHandler(); 57 | $handler->handle($mock); 58 | } 59 | 60 | public function testThrowsExceptionWhenMissingOpeningBracket(): void 61 | { 62 | $this->expectException(MalformedPrintROutputException::class); 63 | $this->expectExceptionMessageMatches( 64 | '/Expected opening bracket after compound type keyword on line [0-9]{1,}\./' 65 | ); 66 | 67 | $mock = $this->createMock(LinesCollectionInterface::class); 68 | $mock->method('getNextLine')->willReturnOnConsecutiveCalls( 69 | 'stdClass Object', 70 | '[0] => value', 71 | ')' 72 | ); 73 | 74 | $handler = new ObjectHandler(); 75 | $handler->handle($mock); 76 | } 77 | 78 | /** 79 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideNullValues 80 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideStringValues 81 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideFloatValues 82 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideArrayValues 83 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideIntegerValues 84 | */ 85 | public function testValidateReturnsFalseOnNotObjectLikeValue(string $value): void 86 | { 87 | $handler = new ObjectHandler(); 88 | $this->assertFalse($handler->shouldBeUsed($value)); 89 | } 90 | 91 | /** 92 | * @dataProvider \Tests\Unit\ReversePrintR\DataProviders\HandlersDataProvider::provideObjectValues 93 | */ 94 | public function testValidateReturnsTrueOnObjectLikeValue(string $value): void 95 | { 96 | $handler = new ObjectHandler(); 97 | $this->assertTrue($handler->shouldBeUsed($value)); 98 | } 99 | } -------------------------------------------------------------------------------- /tests/Unit/Service/StringEndsWithServiceUnitTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(StringEndsWithService::endsWith('string', '')); 13 | } 14 | 15 | public function testReturnsTrueOnSameEnding(): void 16 | { 17 | $this->assertTrue(StringEndsWithService::endsWith('property:visibility', ':visibility')); 18 | } 19 | 20 | public function testReturnsFalseOnDifferentEnding(): void 21 | { 22 | $this->assertFalse(StringEndsWithService::endsWith('aa', 'b')); 23 | } 24 | 25 | public function testDoesntIgnoreWhitespaces(): void 26 | { 27 | $this->assertFalse(StringEndsWithService::endsWith('1', ' 1')); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Unit/Service/StringToLinesServiceUnitTest.php: -------------------------------------------------------------------------------- 1 | assertEquals([''], StringToLinesService::split('')); 14 | $this->assertEquals([''], StringToLinesService::split(' ')); 15 | $this->assertEquals(['0'], StringToLinesService::split('0')); 16 | } 17 | 18 | public function testReturnsMultiValueArrayOnMultiLineString(): void 19 | { 20 | $this->assertEquals(['line 1', 'line 2'], StringToLinesService::split('line 1' . PHP_EOL . 'line 2')); 21 | } 22 | } --------------------------------------------------------------------------------