PHP Classes

File: Tests/JSONBuilderTest.php

Recommend this page to a friend!
  Packages of Aldo Stracquadanio   JSON Builder   Tests/JSONBuilderTest.php   Download  
File: Tests/JSONBuilderTest.php
Role: Unit test script
Content type: text/plain
Description: Unit tests for the JSONBuilder class
Class: JSON Builder
Encode associative array as object in JSON format
Author: By
Last change:
Date: 15 years ago
Size: 2,513 bytes
 

 

Contents

Class file image Download
<?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');
    }
    
}