diff --git a/.gitignore b/.gitignore index 48b8bf9..c1759d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +/.idea vendor/ diff --git a/composer.json b/composer.json index 2a0d423..78ef1c1 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,10 @@ { "require-dev": { "phpunit/phpunit": "^5.0" + }, + "autoload": { + "psr-4": { + "PhpBa\\PhpBusinessBa\\": "src/php_business_ba/" + } } } diff --git a/src/php_business_ba/Repository/CSV/RepositoryCSV.php b/src/php_business_ba/Repository/CSV/RepositoryCSV.php new file mode 100644 index 0000000..8a79e8e --- /dev/null +++ b/src/php_business_ba/Repository/CSV/RepositoryCSV.php @@ -0,0 +1,93 @@ + + */ +class RepositoryCSV implements RepositoryInterface +{ + /** + * Key google spreadsheet + * + * @var string + */ + private $key; + + /** + * RepositoryCSV constructor. + * + * @param string $key + */ + public function __construct(string $key) + { + $this->key = $key; + } + + /** + * returns the worksheet data + * + * @return array + */ + public function getData() : array + { + $spreadsheetData = []; + $fileRows = $this->processData(); + + foreach ($fileRows as $key => $file) { + if ($key > 0) { + $data = str_getcsv($file, ','); + $spreadsheetData[] = + [ + 'key' => $key, + 'name' => $data[1], + 'city' => $data[2], + 'employees' => $data[3], + 'website' => $data[4], + 'years_using_php' => $data[5], + 'frameworks' => (!empty($data[6])) ? explode(',', $data[6]) : [], + 'tests' => (!empty($data[7])) ? explode(',', $data[7]) : [] + ]; + } + } + + return $spreadsheetData; + } + + /** + * returns the worksheet url with the specified id + * + * @return string + */ + protected function getUrl() + { + return "https://docs.google.com/spreadsheets/d/{$this->key}/export?&format=csv&id={$this->key}"; + } + + /** + * It makes the request and processing spreadsheet data + * + * @return array + */ + protected function processData() : array + { + $url = $this->getUrl(); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $fileRows = explode("\n", curl_exec($ch)); + //Gets bool if an error is found 404 + $is404 = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404; + curl_close($ch); + + if ($is404) { + throw new \RuntimeException("File CSV not found"); + } + + return $fileRows; + } +} diff --git a/src/php_business_ba/Repository/RepositoryInterface.php b/src/php_business_ba/Repository/RepositoryInterface.php new file mode 100644 index 0000000..bd09449 --- /dev/null +++ b/src/php_business_ba/Repository/RepositoryInterface.php @@ -0,0 +1,21 @@ + + */ +interface RepositoryInterface +{ + /** + * Method that returns the API data + * + * @return array + */ + public function getData() : array; +} diff --git a/test/php_business_ba/Repository/RepositoryCSVTest.php b/test/php_business_ba/Repository/RepositoryCSVTest.php new file mode 100644 index 0000000..c4847ff --- /dev/null +++ b/test/php_business_ba/Repository/RepositoryCSVTest.php @@ -0,0 +1,81 @@ + + */ +class RepositoryCSVTest extends \PHPUnit_Framework_TestCase +{ + protected function builderRepositoryCsv() + { + $key = '19ri9qD--XVlTZREolIQ5IA9lJODNeqU2elG9gLN06p0'; + $repository = new RepositoryCSV($key); + return $repository->getData(); + } + + public function testVerificaSeClassRepositorioInstanciaUmaInterfaceValida() + { + $key = '19ri9qD--XVlTZREolIQ5IA9lJODNeqU2elG9gLN06p0'; + $repository = new CSV\RepositoryCSV($key); + $className = get_class($repository); + $this->assertInstanceOf('PhpBa\PhpBusinessBa\Repository\RepositoryInterface', $repository, + "A classe {$className} não é uma instância de RepositoryInterface"); + } + + public function testDeveVerificarSeContemItensNaArray() + { + $arrayData = $this->builderRepositoryCsv(); + $this->assertTrue(is_array($arrayData), 'Os dados do repositório não é uma array'); + } + + public function testDeveVerificarConsistenciaDasColunasNaArray() + { + $arrayData = $this->builderRepositoryCsv(); + $keys = []; + + foreach ($arrayData as $entry) { + $this->assertArrayHasKey('key', $entry, 'Coluna key não encontrada'); + $this->assertArrayHasKey('name', $entry, 'Coluna name não encontrada'); + $this->assertArrayHasKey('city', $entry, 'Coluna city não encontrada'); + $this->assertArrayHasKey('employees', $entry, 'Coluna employees não encontrada'); + $this->assertArrayHasKey('website', $entry, 'Coluna website não encontrada'); + $this->assertArrayHasKey('years_using_php', $entry, 'Coluna years_using_php não encontrada'); + $this->assertArrayHasKey('frameworks', $entry, 'Coluna frameworks não encontrada'); + $this->assertArrayHasKey('tests', $entry, 'Coluna tests não encontrada'); + $this->assertNotContains($entry['key'], $keys, 'A key precisa ser unica na lista.'); + $keys[] = $entry['key']; + } + } + + /** + * @expectedException \RuntimeException + */ + public function testDeveRetornarExceptionCasoNaoEncontrePlanilhaOnline() + { + $repository = new RepositoryCSV('testedechave'); + $repository->getData(); + } + + public function testDeveRetonarErroQuandoFramewoksETestsNaoForArray() + { + $arrayData = $this->builderRepositoryCsv(); + + foreach ($arrayData as $entry) { + + $this->assertArrayHasKey('frameworks', $entry, 'Coluna framework não encontrada'); + $this->assertArrayHasKey('tests', $entry, 'Coluna use_tests não encontrada'); + + //Testar consistência das novas colunas + $typeVarFrameworks = gettype($entry['frameworks']); + $typeVarTests = gettype($entry['frameworks']); + $this->assertTrue(is_array($entry['frameworks']), "A coluna frameworks não é um array. Uma {$typeVarFrameworks} foi passado."); + $this->assertTrue(is_array($entry['tests']), "A coluna tests não é um array. Uma {$typeVarTests} foi passado."); + } + } + +} +