Skip to content

Instantly share code, notes, and snippets.

@delineas

delineas/Parser.php

Last active Aug 29, 2015
Embed
What would you like to do?
<?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]);
}
}
<?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