BaseConverterTest.php 1.74 KB
<?php
namespace tests\unit;


use Yii;
use yii\multiparser\Converter;

class BaseConverterTest extends \Codeception\TestCase\Test
{
    /**
     * @var \UnitTester
     */

    private $converter;
    private $configuration;
    private $wrong_configuration;
    private $data_in;
    private $data_out;

    public function _before()
    {
        $this->converter = new Converter();

        $this->configuration = ['hasKey' => true,
            'configuration' =>
            ['encode'   => 'encode',
            'string'   => ['string1', 'string2' ],
            'float'   => 'float',
            'integer'   => ['integer1', 'integer2' ],
         ]];

        $this->wrong_configuration = ['config' =>
            ['encode'   => 'encode',
            'string'   => 'string',
            'float'   => 'float',
            'integer'   => 'integer',
         ]];

        $this->data_in = [
            "encode"   => iconv( 'UTF-8', 'windows-1251', 'test encode string' ),
            "string1"   => 43,
            "string2"   => 45.45,
            "float"   => '100.67',
            "integer1"   => '43.5',
            "integer2"   =>  45.45,
        ];


    }

    public function testConvertByConfig(){

        $this->data_out = $this->converter->convertByConfiguration( $this->data_in, $this->configuration );
        $this->assertEquals( $this->data_out['encode'], iconv( 'windows-1251', 'UTF-8', 'test encode string' ), 'Encoding failed' );
        $this->assertInternalType( 'float', $this->data_out['float'], 'Convert to float is failed' );

    }

    public function testConvertToException(){

        $this->setExpectedException('\Exception');
        $this->data_out = $this->converter->convertByConfiguration( $this->data_in, $this->wrong_configuration );

    }



}