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/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/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 diff --git a/src/CollectionInterface.php b/src/CollectionInterface.php old mode 100644 new mode 100755 index 16480df..5a7676f --- a/src/CollectionInterface.php +++ b/src/CollectionInterface.php @@ -23,9 +23,10 @@ public function get(string $index, $defaultValue = null); * * @param string $index * @param mixed $value + * @param int $secondsToExpire * @return void */ - public function set(string $index, $value); + public function set(string $index, $value, int $secondsToExpire = 5); /** * Checks whether the collection has the given index @@ -41,13 +42,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/FileCollection.php b/src/FileCollection.php new file mode 100755 index 0000000..6e871ba --- /dev/null +++ b/src/FileCollection.php @@ -0,0 +1,125 @@ +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 + { + $fileResource = $this->open($index, 'r'); + $allFileContent= ''; + while (($line = fgets($fileResource)) !== false) { + $allFileContent.= $line; + } + return $allFileContent; + } + + /** + * 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 + { + $fileResource = $this->open($index, 'w'); + fwrite($fileResource, $stringToWrite); + fclose($fileResource); + return true; + } + + /** + * {@inheritDoc} + */ + public function get(string $index, $defaultValue = null) + { + if (!$this->has($index)) { + return $defaultValue; + } + + return $this->files[$index]['value']; + } + + /** + * {@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/src/MemoryCollection.php b/src/MemoryCollection.php old mode 100644 new mode 100755 index b71984a..44a20d1 --- a/src/MemoryCollection.php +++ b/src/MemoryCollection.php @@ -33,15 +33,16 @@ public function get(string $index, $defaultValue = null) return $defaultValue; } - return $this->data[$index]; + return $this->data[$index]['value']; } /** * {@inheritDoc} */ - public function set(string $index, $value) + public function set(string $index, $value, $secondsToExpire = 5) { - $this->data[$index] = $value; + + $this->data[$index] = ['value'=> $value, 'expires'=> time() + $secondsToExpire ]; } /** @@ -49,7 +50,7 @@ public function set(string $index, $value) */ public function has(string $index) { - return array_key_exists($index, $this->data); + return array_key_exists($index, $this->data) && time() <= $this->data[$index]['expires']; } /** @@ -57,7 +58,7 @@ public function has(string $index) */ public function count(): int { - return count($this->data) + 1; + return count($this->data); } /** diff --git a/tests/src/FileCollectionTest.php b/tests/src/FileCollectionTest.php new file mode 100755 index 0000000..6d15f39 --- /dev/null +++ b/tests/src/FileCollectionTest.php @@ -0,0 +1,133 @@ +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 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 + */ + public function collectionCanBeCleaned() + { + $collection = new FileCollection('files/test.txt', 'test'); + $this->assertEquals(1, $collection->count()); + + $collection->clean(); + $this->assertEquals(0, $collection->count()); + } +} diff --git a/tests/src/MemoryCollectionTest.php b/tests/src/MemoryCollectionTest.php old mode 100644 new mode 100755 index 7f8f1c9..6ea96c8 --- a/tests/src/MemoryCollectionTest.php +++ b/tests/src/MemoryCollectionTest.php @@ -43,6 +43,18 @@ public function dataCanBeRetrieved() $this->assertEquals('value', $collection->get('index1')); } + /** + * @test + * @depends dataCanBeAdded + */ + public function dataCannotBeRetrievedAfterExpires() + { + $collection = new MemoryCollection(); + $collection->set('index1', 'value', 2); + sleep(3); + $this->assertFalse($collection->has('index1')); + } + /** * @test * @depends objectCanBeConstructed