From 2f62a5eb59af4c1480a15704b99d233fdea55884 Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Mon, 6 Feb 2017 23:26:20 -0200 Subject: [PATCH 1/8] composer initial config --- .gitignore | 1 + composer.json | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..fd8a305 --- /dev/null +++ b/composer.json @@ -0,0 +1,8 @@ +{ + "name": "phpol/string", + "description": "PHP Object Library String Component", + "type": "library", + "license": "MIT", + "minimum-stability": "stable", + "require": {} +} From 6ccf86995c8505feabf0010ed08b2baa77117c09 Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Mon, 6 Feb 2017 23:32:05 -0200 Subject: [PATCH 2/8] phpunit added as dependency --- .gitignore | 1 + composer.json | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 57872d0..3a9875b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +composer.lock diff --git a/composer.json b/composer.json index fd8a305..2c1a3bc 100644 --- a/composer.json +++ b/composer.json @@ -4,5 +4,13 @@ "type": "library", "license": "MIT", "minimum-stability": "stable", - "require": {} + "require": {}, + "autoload": { + "psr-4": { + "OL\\String\\": "src/" + } + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + } } From 0cb16b7a1537fd6a8e41bf4ac5350973ca622806 Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Mon, 6 Feb 2017 23:37:26 -0200 Subject: [PATCH 3/8] initial phpunit configuration file (dist) --- .gitignore | 2 ++ phpunit.xml.dist | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 phpunit.xml.dist diff --git a/.gitignore b/.gitignore index 3a9875b..5193bf3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /vendor/ composer.lock +phpunit.xml +/coverage/ \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..c5cce58 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,24 @@ + + + + + ./tests + + + + + ./src + + + + + + + \ No newline at end of file From caf2a04f166a5fe707fa7d5fe8d38e8e6243e7a5 Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Mon, 6 Feb 2017 23:49:40 -0200 Subject: [PATCH 4/8] php-cs-fixer config file --- .gitignore | 3 ++- .php_cs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 .php_cs diff --git a/.gitignore b/.gitignore index 5193bf3..6ddd33f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor/ composer.lock phpunit.xml -/coverage/ \ No newline at end of file +/coverage/ +.php_cs.cache diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..f003be0 --- /dev/null +++ b/.php_cs @@ -0,0 +1,18 @@ +setRiskyAllowed(true) + ->setRules(array( + '@PSR2' => true, + 'psr4' => true, + 'ordered_class_elements' => true, + 'ordered_imports' => true, + 'phpdoc_add_missing_param_annotation' => true, + 'phpdoc_order' => true, + )) + ->setFinder( + PhpCsFixer\Finder::create() + ->exclude('tests/Fixtures') + ->exclude('vendor') + ->in(__DIR__) + ); \ No newline at end of file From 306f462800eaa9433f025851e070a415e3246b61 Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Mon, 6 Feb 2017 23:50:23 -0200 Subject: [PATCH 5/8] php-cs-fixer as dev dependency and initial test --- composer.json | 3 ++- src/Str.php | 8 ++++++++ tests/StrTest.php | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/Str.php create mode 100644 tests/StrTest.php diff --git a/composer.json b/composer.json index 2c1a3bc..2a1394c 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^6.0" + "phpunit/phpunit": "^6.0", + "friendsofphp/php-cs-fixer": "^2.0" } } diff --git a/src/Str.php b/src/Str.php new file mode 100644 index 0000000..da2b229 --- /dev/null +++ b/src/Str.php @@ -0,0 +1,8 @@ +assertInstanceOf(Str::class, $str); + } +} From 80f0b09189ff440fe2c016c3235a828382a8894e Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Tue, 7 Feb 2017 00:20:06 -0200 Subject: [PATCH 6/8] initial method replace / 100% coverage --- src/Contracts/Str.php | 46 +++++++++++++++++++++++++++ src/Str.php | 73 +++++++++++++++++++++++++++++++++++++++++-- tests/StrTest.php | 64 +++++++++++++++++++++++++++++++++++-- 3 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 src/Contracts/Str.php diff --git a/src/Contracts/Str.php b/src/Contracts/Str.php new file mode 100644 index 0000000..9bfa983 --- /dev/null +++ b/src/Contracts/Str.php @@ -0,0 +1,46 @@ +payload = (string) $payload; + } + + /** + * Str objects should should be able to be cast as string. + * + * @return string The actual string + */ + public function __toString() + { + return $this->contents(); + } + + /** + * Public method to retrieve the raw string contents of an object. + * + * @return string + */ + public function contents() + { + return $this->payload; + } + + /** + * Replace one or more entries from the given object with a given value + * + * @param array|string $search What to search for + * @param string $replace Replace with? + * + * @return StrContract Object instance with the replaced text. + */ + public function replace($search, string $replace) + { + return $this->factory(str_replace($search, $replace, $this)); + } + + /** + * Factories a new StrContract object. + * + * @param string $str Instance to be factory from + * + * @return StrContract + */ + protected function factory(string $str) + { + return new self($str); + } } diff --git a/tests/StrTest.php b/tests/StrTest.php index b52c24f..6f3bb2f 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -2,14 +2,74 @@ namespace OL\String; +use OL\String\Contracts\Str as StrContract; use PHPUnit\Framework\TestCase; class StrTest extends TestCase { - public function test_instantiable() + /** + * This test should instantiate with success from a string + */ + public function test_instantiable_with_string() { + // create a new instance $str = new Str('foo value'); - $this->assertInstanceOf(Str::class, $str); + // make sure it is implementing the main contract + $this->assertInstanceOf(StrContract::class, $str); + } + + /** + * This test should determine if an object of Str can be cast as a string. + */ + public function test_object_can_be_cast_as_string() + { + // create a new instance + $str = new Str('foo bar baz'); + + // assert the cast value of the object is equals the initially given string. + $this->assertEquals('foo bar baz', (string) $str); + } + + /** + * This test should prove an Str object can be created from another + * Str object. + */ + public function test_instantiable_with_str_object() + { + // create an initial instance + $initialStr = new Str('foo bar'); + + // create an instance passing not directly a string but an StrContract object. + $str = new Str($initialStr); + + // assets contract implementation. + $this->assertInstanceOf(StrContract::class, $str); + // assets the contents are correctly passed between instances. + $this->assertEquals('foo bar', $str->contents()); + } + + /** + * Test replacement of parts of text, using a single string + */ + public function test_replace_single_string() + { + $str = new Str('foo bar'); + + $replaced = $str->replace('bar', 'baz'); + + $this->assertEquals('foo baz', $replaced); + } + + /** + * Test replacement of parts of text, using a multiple search strings + */ + public function test_replace_multiple_string() + { + $str = new Str('foo bar baz'); + + $replaced = $str->replace(['bar', 'baz'], 'foo'); + + $this->assertEquals('foo foo foo', $replaced); } } From 273e0d5b8da0a8562e994ee83269ac92e5cfbf72 Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Tue, 7 Feb 2017 00:20:54 -0200 Subject: [PATCH 7/8] cs-fixer --- src/Contracts/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contracts/Str.php b/src/Contracts/Str.php index 9bfa983..b273eb6 100644 --- a/src/Contracts/Str.php +++ b/src/Contracts/Str.php @@ -43,4 +43,4 @@ public function contents(); * @return Str */ public function replace($search, string $replace); -} \ No newline at end of file +} From 76ee32b7395503d53eaddb57ce47fa3db2e49eba Mon Sep 17 00:00:00 2001 From: Diego Hernandes Date: Tue, 7 Feb 2017 00:23:48 -0200 Subject: [PATCH 8/8] new test passing object --- tests/StrTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/StrTest.php b/tests/StrTest.php index 6f3bb2f..bfb358e 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -72,4 +72,16 @@ public function test_replace_multiple_string() $this->assertEquals('foo foo foo', $replaced); } + + /** + * Test replacement of parts of text, using a multiple search strings and objects + */ + public function test_replace_multiple_string_with_objects() + { + $str = new Str('foo bar baz'); + + $replaced = $str->replace([new Str('bar'), 'baz'], 'foo'); + + $this->assertEquals('foo foo foo', $replaced); + } }