From 562a63d83694c84738742624a486e9defa5a13d2 Mon Sep 17 00:00:00 2001 From: Diego Silva Date: Tue, 21 May 2019 18:24:29 -0300 Subject: [PATCH 1/5] Fix error on memoryCollection function count() - was added + 1 on count of array, and remove method on interface that tells to remove --- src/CollectionInterface.php | 9 --------- src/MemoryCollection.php | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) mode change 100644 => 100755 src/CollectionInterface.php mode change 100644 => 100755 src/MemoryCollection.php diff --git a/src/CollectionInterface.php b/src/CollectionInterface.php old mode 100644 new mode 100755 index 16480df..b714d5e --- a/src/CollectionInterface.php +++ b/src/CollectionInterface.php @@ -41,13 +41,4 @@ public function has(string $index); * @return integer */ public function count(): int; - - /** - * Cleans the collection - * - * Estou aqui para testar sua atenção. Remova-me. - * - * @return void - */ - public function clean(); } diff --git a/src/MemoryCollection.php b/src/MemoryCollection.php old mode 100644 new mode 100755 index b71984a..c55f0dd --- a/src/MemoryCollection.php +++ b/src/MemoryCollection.php @@ -57,7 +57,7 @@ public function has(string $index) */ public function count(): int { - return count($this->data) + 1; + return count($this->data); } /** From 24496820a3c385547537b1053f8ae57537c56d97 Mon Sep 17 00:00:00 2001 From: Diego Silva Date: Tue, 21 May 2019 18:29:34 -0300 Subject: [PATCH 2/5] create complete FileCollection( expires to) with tests --- src/FileCollection.php | 149 +++++++++++++++++++++++++++++++ tests/src/FileCollectionTest.php | 121 +++++++++++++++++++++++++ 2 files changed, 270 insertions(+) create mode 100755 src/FileCollection.php create mode 100755 tests/src/FileCollectionTest.php diff --git a/src/FileCollection.php b/src/FileCollection.php new file mode 100755 index 0000000..feb68a0 --- /dev/null +++ b/src/FileCollection.php @@ -0,0 +1,149 @@ +files[$index] = ['value'=> $completePath, 'expires'=> time() + $secondsToExpire]; + } else { + throw new Exception('File Not Exists : '); + } + } + + /** + * Open file when specifies the index of collection + * @param string $index + * @param string $mode the same mode using fopen + * @return resource If $index is not found, + * open will throw Exception. + * @throws Exception if not found index + */ + public function open(string $index, string $mode = 'r') + { + if ($this->has($index)) { + $fileResource = $this->get($index); + return is_string($fileResource) ? fopen($fileResource, $mode) : $fileResource; + } + throw new Exception('FileAccessException : do not have permission to open this file, check permissions'); + } + + /** + * Read a file in collection + * @param string $index + * @param string $mode the same mode using fopen + * @return string contains all contents of file + * @throws Exception if not found index or file is not readable + */ + public function read($index) : string + { + $content= null; + try { + $fileResource = $this->open($index, 'r'); + if (is_readable($this->get($index))) { + $allFileContent= ''; + while (($line = fgets($fileResource)) !== false) { + $allFileContent.= $line; + } + $content = $allFileContent; + } else { + throw new Exception('FileAccessException : do not have permission to read this file'); + } + } catch (Exception $ex) { + } + return $content; + } + + /** + * Read a file in collection + * @param string $index + * @param string $mode the same mode using fopen + * @return string contains all contents of file + * @throws Exception if not found index or file is not readable + */ + public function write($index, $stringToWrite) : bool + { + try { + $fileResource = $this->open($index, 'w'); + if (is_writable($this->get($index))) { + fwrite($fileResource, $stringToWrite); + fclose($fileResource); + return true; + } + throw new Exception('FileAccessException : do not have permission to write in this file.'); + } catch (Exception $e) { + return false; + } + } + + /** + * {@inheritDoc} + */ + public function get(string $index, $defaultValue = null) + { + if (!$this->has($index)) { + return $defaultValue; + } + + return $this->files[$index]['value']; + } + + /** + * {@inheritDoc} + */ + public function getAll() : array + { + return $this->files; + } + + /** + * {@inheritDoc} + */ + public function set(string $completePath, $index, $secondsToExpire = 5) + { + $this->files[$index] = ['value'=> $completePath, 'expires'=> $secondsToExpire ]; + } + + /** + * {@inheritDoc} + */ + public function has(string $index) + { + return array_key_exists($index, $this->files) && time() <= $this->files[$index]['expires']; + } + + /** + * {@inheritDoc} + */ + public function count(): int + { + return count($this->files); + } + + /** + * {@inheritDoc} + */ + public function clean() + { + $this->files = []; + } +} diff --git a/tests/src/FileCollectionTest.php b/tests/src/FileCollectionTest.php new file mode 100755 index 0000000..9184635 --- /dev/null +++ b/tests/src/FileCollectionTest.php @@ -0,0 +1,121 @@ +assertFalse(false); + } + + $collection = new FileCollection('files/test.txt', 'test'); + $collection->set('files/content_not_exists.txt', 'test2', 5); + try { + $collection->open('test2'); + } catch (\Exception $e) { + $this->assertFalse(false); + } + } + + /** + * @test + * @depends objectCanBeConstructed + * @doesNotPerformAssertions + */ + public function fileCanBeAdded() + { + $collection = new FileCollection('files/test.txt', 'test', 5); + $collection->set('files/test2.txt', 'test2', 5); + } + + /** + * @test + * @depends fileCanBeAdded + * @doesNotPerformAssertions + */ + public function fileCanBeOpen() + { + $collection = new FileCollection('files/test.txt', 'test'); + $collection->open('test'); + } + + /** + * @test + * @depends fileCanBeOpen + */ + public function fileCannotBeUseAfterExpires() + { + $collection = new FileCollection('files/test.txt', 'test', 3); + sleep(4); + $this->assertFalse($collection->has('test')); + } + + /** + * @test + * @depends fileCanBeOpen + */ + public function dataCanBeWrite() + { + $collection = new FileCollection('files/test.txt', 'test'); + $collection->open('test'); + $this->assertTrue($collection->write('test', 'qualquercoisa')); + } + + /** + * @test + * @depends fileCanBeOpen + */ + public function fileCanBeRead() + { + $collection = new FileCollection('files/test.txt', 'test'); + $collection->write('test', 'NewStringTest'); + $this->assertEquals('NewStringTest', $collection->read('test', 'NewStringTest')); + } + + /** + * @test + * @depends fileCanBeAdded + */ + public function collectionWithItemsShouldReturnValidCount() + { + $collection = new FileCollection('files/test.txt', 'test'); + $collection->set('files/test2.txt', 'test2', 5); + $collection->set('files/test3.txt', 'test3', 5); + + + $this->assertEquals(3, $collection->count()); + } + + /** + * @test + * @depends collectionWithItemsShouldReturnValidCount + */ + public function collectionCanBeCleaned() + { + $collection = new FileCollection('files/test.txt', 'test'); + $this->assertEquals(1, $collection->count()); + + $collection->clean(); + $this->assertEquals(0, $collection->count()); + } +} From 38c13688ccc40f89680af7cf40c0bfbd036d91c1 Mon Sep 17 00:00:00 2001 From: Diego Silva Date: Tue, 21 May 2019 18:40:05 -0300 Subject: [PATCH 3/5] add files to test dev code in docker and update .gitignore --- .gitignore | 3 ++- Dockerfile | 46 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 9 +++++++++ files/test.txt | 1 + files/test2.txt | 0 files/test3.txt | 0 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 Dockerfile create mode 100755 docker-compose.yml create mode 100755 files/test.txt create mode 100755 files/test2.txt create mode 100755 files/test3.txt diff --git a/.gitignore b/.gitignore index 88ffe7c..1435708 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ /build/ -.phpunit* \ No newline at end of file +.phpunit* +.idea/ diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..4aa3be4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +FROM php:7.3-apache + +RUN buildDeps=" \ + libbz2-dev \ + libmemcached-dev \ + default-libmysqlclient-dev \ + libsasl2-dev \ + " \ + runtimeDeps=" \ + libzip-dev \ + zip \ + curl \ + git \ + libfreetype6-dev \ + libicu-dev \ + libjpeg-dev \ + libmcrypt-dev \ + libmemcachedutil2 \ + libpng-dev \ + libpq-dev \ + libxml2-dev \ + libc-client-dev \ + libkrb5-dev \ + libcurl3-gnutls \ + apt-transport-https \ + ca-certificates \ + " \ + && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y $buildDeps $runtimeDeps \ + && docker-php-ext-configure zip --with-libzip \ + && docker-php-ext-install zip \ + && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ + && docker-php-ext-install gd \ + && pecl install xdebug \ + && docker-php-ext-enable xdebug \ + && docker-php-ext-install bcmath json ftp bz2 calendar iconv intl mbstring mysqli opcache pdo_mysql pdo_pgsql pgsql soap fileinfo sockets \ + && apt-get purge -y --auto-remove $buildDeps \ + && a2enmod rewrite \ + && apt-get install -y --no-install-recommends libfontconfig1-dev + +#correcao data e hora SP / Brasil +RUN echo "America/Sao_Paulo" > /etc/timezone +RUN dpkg-reconfigure -f noninteractive tzdata + +ENV COMPOSER_HOME /root/composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer +ENV PATH $COMPOSER_HOME/vendor/bin:$PATH diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..bb89fb8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '2' +services: + phpTrade: + image: diegosilva19/phptest + container_name: phptest + ports: + - "80:80" + volumes: + - ".:/var/www/html" diff --git a/files/test.txt b/files/test.txt new file mode 100755 index 0000000..0862591 --- /dev/null +++ b/files/test.txt @@ -0,0 +1 @@ +NewStringTest \ No newline at end of file diff --git a/files/test2.txt b/files/test2.txt new file mode 100755 index 0000000..e69de29 diff --git a/files/test3.txt b/files/test3.txt new file mode 100755 index 0000000..e69de29 From b9f8cbbf7de69da4cfd0487d2870a3ab9d8a246b Mon Sep 17 00:00:00 2001 From: Diego Silva Date: Tue, 21 May 2019 19:22:15 -0300 Subject: [PATCH 4/5] remove unused function getAll --- README.md | 8 ++++---- src/FileCollection.php | 10 +--------- 2 files changed, 5 insertions(+), 13 deletions(-) mode change 100644 => 100755 README.md diff --git a/README.md b/README.md old mode 100644 new mode 100755 index b5105c3..8d57112 --- a/README.md +++ b/README.md @@ -78,8 +78,8 @@ Para executar os testes, utilize o comando, na raiz do projeto: $ composer test ``` -[icon-travisci]: https://img.shields.io/travis/liveecommerce/php-test.svg?style=flat-square -[icon-codecov]: https://img.shields.io/codecov/c/github/liveecommerce/php-test.svg?style=flat-square +[icon-travisci]: https://img.shields.io/travis/diegosilva19/php-test.svg?style=flat-square +[icon-codecov]: https://img.shields.io/codecov/c/github/diegosilva19/php-test.svg?style=flat-square -[link-travisci]: https://travis-ci.org/liveecommerce/php-test -[link-codecov]: https://codecov.io/gh/liveecommerce/php-test \ No newline at end of file +[link-travisci]: https://travis-ci.org/diegosilva19/php-test.svg?branch=master +[link-codecov]: https://codecov.io/gh/diegosilva19/php-test \ No newline at end of file diff --git a/src/FileCollection.php b/src/FileCollection.php index feb68a0..67ddbee 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -106,15 +106,7 @@ public function get(string $index, $defaultValue = null) return $this->files[$index]['value']; } - - /** - * {@inheritDoc} - */ - public function getAll() : array - { - return $this->files; - } - + /** * {@inheritDoc} */ From 498d3937b862450b23b223a615d06b3838238911 Mon Sep 17 00:00:00 2001 From: Diego Silva Date: Tue, 21 May 2019 20:31:02 -0300 Subject: [PATCH 5/5] reduce code to correct coverage, optimized logic when open files to avoid exceptions --- src/FileCollection.php | 36 +++++++++----------------------- tests/src/FileCollectionTest.php | 12 +++++++++++ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/FileCollection.php b/src/FileCollection.php index 67ddbee..6e871ba 100755 --- a/src/FileCollection.php +++ b/src/FileCollection.php @@ -23,7 +23,7 @@ class FileCollection implements CollectionInterface */ public function __construct(string $completePath, string $index, int $secondsToExpire = 5) { - if (file_exists($completePath) !== false) { + if (file_exists($completePath) !== false && is_writable($completePath) && is_readable($completePath)) { $this->files[$index] = ['value'=> $completePath, 'expires'=> time() + $secondsToExpire]; } else { throw new Exception('File Not Exists : '); @@ -56,21 +56,12 @@ public function open(string $index, string $mode = 'r') */ public function read($index) : string { - $content= null; - try { - $fileResource = $this->open($index, 'r'); - if (is_readable($this->get($index))) { - $allFileContent= ''; - while (($line = fgets($fileResource)) !== false) { - $allFileContent.= $line; - } - $content = $allFileContent; - } else { - throw new Exception('FileAccessException : do not have permission to read this file'); - } - } catch (Exception $ex) { + $fileResource = $this->open($index, 'r'); + $allFileContent= ''; + while (($line = fgets($fileResource)) !== false) { + $allFileContent.= $line; } - return $content; + return $allFileContent; } /** @@ -82,17 +73,10 @@ public function read($index) : string */ public function write($index, $stringToWrite) : bool { - try { - $fileResource = $this->open($index, 'w'); - if (is_writable($this->get($index))) { - fwrite($fileResource, $stringToWrite); - fclose($fileResource); - return true; - } - throw new Exception('FileAccessException : do not have permission to write in this file.'); - } catch (Exception $e) { - return false; - } + $fileResource = $this->open($index, 'w'); + fwrite($fileResource, $stringToWrite); + fclose($fileResource); + return true; } /** diff --git a/tests/src/FileCollectionTest.php b/tests/src/FileCollectionTest.php index 9184635..6d15f39 100755 --- a/tests/src/FileCollectionTest.php +++ b/tests/src/FileCollectionTest.php @@ -106,6 +106,18 @@ public function collectionWithItemsShouldReturnValidCount() $this->assertEquals(3, $collection->count()); } + /** + * @test + * @depends objectCanBeConstructed + */ + public function inexistentIndexShouldReturnDefaultValue() + { + $collection = new FileCollection('files/test.txt', 'test'); + + $this->assertNull($collection->get('test1')); + $this->assertEquals('defaultValue', $collection->get('test1', 'defaultValue')); + } + /** * @test * @depends collectionWithItemsShouldReturnValidCount