Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primeira versão do Repositorio #16

Merged
merged 5 commits into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
vendor/
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"require-dev": {
"phpunit/phpunit": "^5.0"
},
"autoload": {
"psr-4": {
"PhpBa\\PhpBusinessBa\\": "src/php_business_ba/"
}
}
}
93 changes: 93 additions & 0 deletions src/php_business_ba/Repository/CSV/RepositoryCSV.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
namespace PhpBa\PhpBusinessBa\Repository\CSV;

use PhpBa\PhpBusinessBa\Repository\RepositoryInterface;

/**
* responsible class process the csv data
*
* Class RepositoryCSV
* @package PhpBa\PhpBusinessBa\Repository\CSV
* @author edyonil <[email protected]>
*/
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;
}
}
21 changes: 21 additions & 0 deletions src/php_business_ba/Repository/RepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace PhpBa\PhpBusinessBa\Repository;

/**
* Interface that defines the return of the rules of API data
*
* Interface RepositoryInterface
* @package PhpBa\PhpBusinessBa\Repository
* @author edyonil <[email protected]>
*/
interface RepositoryInterface
{
/**
* Method that returns the API data
*
* @return array
*/
public function getData() : array;
}
81 changes: 81 additions & 0 deletions test/php_business_ba/Repository/RepositoryCSVTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
namespace PhpBa\PhpBusinessBa\Repository;

use PhpBa\PhpBusinessBa\Repository\CSV\RepositoryCSV;

/**
* Class RepositoryCSVTest
* @package PhpBa\PhpBusinessBa\Repository
* @author edyonil <[email protected]>
*/
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.");
}
}

}