view vendor/consolidation/output-formatters/tests/testIncompatibleData.php @ 9:1fc0ff908d1f

Add another data file
author Chris Cannam
date Mon, 05 Feb 2018 12:34:32 +0000
parents 4c8ae668cc8c
children
line wrap: on
line source
<?php
namespace Consolidation\OutputFormatters;

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Consolidation\OutputFormatters\Exception\IncompatibleDataException;

class IncompatibleDataTests extends \PHPUnit_Framework_TestCase
{
    protected $formatterManager;

    function setup() {
        $this->formatterManager = new FormatterManager();
    }

    protected function assertIncompatibleDataMessage($expected, $formatter, $data)
    {
        $e = new IncompatibleDataException($formatter, $data, $formatter->validDataTypes());
        $this->assertEquals($expected, $e->getMessage());
    }

    public function testIncompatibleData()
    {
        $tableFormatter = $this->formatterManager->getFormatter('table');

        $this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, a string was provided.', $tableFormatter, 'a string');
        $this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, an instance of Consolidation\OutputFormatters\FormatterManager was provided.', $tableFormatter, $this->formatterManager);
        $this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, an array was provided.', $tableFormatter, []);
        $this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, an instance of Consolidation\OutputFormatters\StructuredData\PropertyList was provided.', $tableFormatter, new PropertyList([]));
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage Undescribable data error: NULL
     */
    public function testUndescribableData()
    {
        $tableFormatter = $this->formatterManager->getFormatter('table');
        $data = $tableFormatter->validate(null);
        $this->assertEquals('Will throw before comparing.', $data);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, a string was provided.
     */
    public function testInvalidTableData()
    {
        $tableFormatter = $this->formatterManager->getFormatter('table');
        $data = $tableFormatter->validate('bad data type');
        $this->assertEquals('Will throw before comparing.', $data);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\SectionsFormatter must be an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields. Instead, a string was provided.
     */
    public function testInvalidSectionsData()
    {
        $tableFormatter = $this->formatterManager->getFormatter('sections');
        $data = $tableFormatter->validate('bad data type');
        $this->assertEquals('Will throw before comparing.', $data);
    }
}