<?php
namespace A2PLab\Component\JSONBuilder\Tests;
use A2PLab\Component\JSONBuilder\JSONBuilder;
use A2PLab\Component\JSONBuilder\JSONFunction;
use PHPUnit_Framework_TestCase;
class JSONBuilderTest extends PHPUnit_Framework_TestCase {
protected $builder;
protected $data;
public function __construct($name = NULL, array $data = array(), $dataName = '') {
parent::__construct($name, $data, $dataName);
}
public function getMarkerTests() {
return array(
array('#__#__#'),
array('<<>>'),
array('______'),
array("\n\n"),
array('F'),
array(' '),
);
}
public function getBuildTests() {
$data = $this->getMarkerTests();
$data = array_map(function($value) {
return array($value, array(
'prova1' => 'Prova',
'prova2' => $value,
'prova3' => $value.$value,
'prova4' => $value.'Prova'.$value,
'prova5' => 'Prova'.$value,
'prova6' => $value.$value.$value,
'provaFunc' => new JSONFunction('Funzione'),
'provaFunc2' => new JSONFunction('function(event) { alert(\'ok\'); alert("ok"); }'),
'provaFunc3' => new JSONFunction('function(event) { alert("'.$value.'"); }')
),
"{\"prova1\":\"Prova\",\"prova2\":\"{$value}\",\"prova3\":\"{$value}{$value}\",\"prova4\":\"{$value}Prova{$value}\",".
"\"prova5\":\"Prova{$value}\",\"prova6\":\"{$value}{$value}{$value}\",\"provaFunc\":Funzione,".
"\"provaFunc2\":function(event) { alert('ok'); alert(\"ok\"); },\"provaFunc3\":function(event) { alert(\"{$value}\"); }}");
}, $data[0]);
return $data;
}
/**
* @dataProvider getMarkerTests
*/
public function testCreate($marker) {
$builder = new JSONBuilder($marker);
$this->assertEquals($marker, $builder->getFunctionMarker(), 'Error creating the JSONBuilder with marker ' . $marker);
}
/**
* @dataProvider getBuildTests
*/
public function testBuild($marker, $data, $expectedJSON) {
$builder = new JSONBuilder($marker);
$json = $builder->buildJSON($data);
$this->assertEquals($expectedJSON, $json, 'Error building json with builder');
}
}
|