Last active
August 29, 2015 14:10
-
-
Save delineas/d30616e8b656b47253bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme; | |
/** | |
* Dummy class | |
* | |
*/ | |
class Parser | |
{ | |
public function parse($data) { | |
$values = $this->splitValues($data); | |
$this->validateMail($values[2]); | |
$values[4] = $this->extractGroups($values[4]); | |
return $values; | |
} | |
private function splitValues($data) { | |
$values = explode(';', $data); | |
foreach($values as &$value) { | |
$value = trim($value); | |
} | |
return $values; | |
} | |
private function validateMail($mail){ | |
if(!filter_var($mail,FILTER_VALIDATE_EMAIL)) { | |
throw new \InvalidArgumentException(); | |
} | |
} | |
private function extractGroups($component) { | |
$matches = array(); | |
preg_match('/\[(.*)\]/', $component, $matches); | |
return explode(',',$matches[1]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme; | |
/** | |
* Description of DummyTest | |
* | |
*/ | |
class ParserTest extends \PHPUnit_Framework_TestCase | |
{ | |
public $data; | |
public function setUp() | |
{ | |
$this->data = 'juan; pérez peréz; juanperez@ejemplo.com;juan;[contabilidad,dirección]'; | |
} | |
public function testNumeroDeElementosCorrecto() { | |
$expected = 5; | |
$parser = new \Acme\Parser(); | |
$values = $parser->parse($this->data); | |
$this->assertEquals($expected,count($values)); | |
} | |
public function testComprobarCampos() { | |
$expected = array( | |
'juan', | |
'pérez peréz', | |
'juanperez@ejemplo.com', | |
'juan', | |
array( | |
'contabilidad', | |
'dirección' | |
) | |
); | |
$parser = new \Acme\Parser(); | |
$values = $parser->parse($this->data); | |
$this->assertEquals($expected,$values); | |
} | |
/** | |
@expectedException \InvalidArgumentException | |
*/ | |
public function testValidarTercerCampoCorreoElectronico() { | |
$data = 'juan; pérez peréz; juanperezejemplo.com;juan;[contabilidad,dirección]'; | |
$parser = new \Acme\Parser(); | |
$parser->parse($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment