<commit_before><?php

namespace atk4\data\tests;

use atk4\data\Model;
use atk4\data\Persistence_SQL;

class SerializeTest extends \atk4\schema\PHPUnit_SchemaTestCase
{
    public function testBasicSerialize()
    {
        $db = new Persistence_SQL($this->db->connection);
        $m = new Model($db, 'job');

        $f = $m->addField('data', ['serialize' => 'serialize']);

        $this->assertEquals(
            ['data' => 'a:1:{s:3:"foo";s:3:"bar";}'], $db->typecastSaveRow($m,
            ['data' => ['foo' => 'bar']]
        ));
        $this->assertEquals(
            ['data' => ['foo' => 'bar']], $db->typecastLoadRow($m,
            ['data' => 'a:1:{s:3:"foo";s:3:"bar";}']
        ));

        $f->serialize = 'json';
        $f->type = 'array';
        $this->assertEquals(
            ['data' => '{"foo":"bar"}'], $db->typecastSaveRow($m,
            ['data' => ['foo' => 'bar']]
        ));
        $this->assertEquals(
            ['data' => ['foo' => 'bar']], $db->typecastLoadRow($m,
            ['data' => '{"foo":"bar"}']
        ));
    }

    /**
     * @expectedException Exception
     */
    public function testSerializeErrorJSON()
    {
        $db = new Persistence_SQL($this->db->connection);
        $m = new Model($db, 'job');

        $f = $m->addField('data', ['serialize' => 'serialize']);
        $f->serialize = 'json';
        $f->type = 'array';
        $this->assertEquals(
            ['data' => ['foo' => 'bar']], $db->typecastLoadRow($m,
            ['data' => '{"foo":"bar" OPS']
        ));
    }

    /*
     * @expectedException Exception
     *
     * THIS IS NOT POSSIBLE BECAUSE unserialize() produces error
     * and not exception
     */

    /*
    public function testSerializeErrorSerialize()
    {
        $db = new Persistence_SQL($this->db->connection);
        $m = new Model($db, 'job');

        $f = $m->addField('data', ['serialize' => 'serialize']);
        $this->assertEquals(
            ['data' => ['foo' => 'bar']]
            , $db->typecastLoadRow($m,
            ['data' => 'a:1:{s:3:"foo";s:3:"bar"; OPS']
        ));<commit_msg>add test for exception in case of json encoding recursive variable<commit_after><?php

namespace atk4\data\tests;

use atk4\data\Model;
use atk4\data\Persistence_SQL;

class SerializeTest extends \atk4\schema\PHPUnit_SchemaTestCase
{
    public function testBasicSerialize()
    {
        $db = new Persistence_SQL($this->db->connection);
        $m = new Model($db, 'job');

        $f = $m->addField('data', ['serialize' => 'serialize']);

        $this->assertEquals(
            ['data' => 'a:1:{s:3:"foo";s:3:"bar";}'], $db->typecastSaveRow($m,
            ['data' => ['foo' => 'bar']]
        ));
        $this->assertEquals(
            ['data' => ['foo' => 'bar']], $db->typecastLoadRow($m,
            ['data' => 'a:1:{s:3:"foo";s:3:"bar";}']
        ));

        $f->serialize = 'json';
        $f->type = 'array';
        $this->assertEquals(
            ['data' => '{"foo":"bar"}'], $db->typecastSaveRow($m,
            ['data' => ['foo' => 'bar']]
        ));
        $this->assertEquals(
            ['data' => ['foo' => 'bar']], $db->typecastLoadRow($m,
            ['data' => '{"foo":"bar"}']
        ));
    }

    /**
     * @expectedException Exception
     */
    public function testSerializeErrorJSON()
    {
        $db = new Persistence_SQL($this->db->connection);
        $m = new Model($db, 'job');

        $f = $m->addField('data', ['type' => 'array', 'serialize' => 'json']);
        
        $db->typecastLoadRow($m, ['data' => '{"foo":"bar" OPS']);
    }

    /**
     * @expectedException Exception
     */
    public function testSerializeErrorJSON2()
    {
        $db = new Persistence_SQL($this->db->connection);
        $m = new Model($db, 'job');

        $f = $m->addField('data', ['type' => 'array', 'serialize' => 'json']);
        
        // recursive array - json can't encode that
        $a =[];
        $a[] = &$a;
        
        $db->typecastSaveRow($m, ['data' => ['foo' => 'bar', 'recursive' => $a]]);
    }

    /*
     * @expectedException Exception
     *
     * THIS IS NOT POSSIBLE BECAUSE unserialize() produces error
     * and not exception
     */

    /*
    public function testSerializeErrorSerialize()
    {
        $db = new Persistence_SQL($this->db->connection);
        $m = new Model($db, 'job');

        $f = $m->addField('data', ['serialize' => 'serialize']);
        $this->assertEquals(
            ['data' => ['foo' => 'bar']]
            , $db->typecastLoadRow($m,
            ['data' => 'a:1:{s:3:"foo";s:3:"bar"; OPS']
        ));
{'fraction_non_alphanumeric': 0.2187296769997832, 'fraction_numerical': 0.0058530240624322565, 'mean_word_length': 2.1997226074895977, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6166001', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1483, 'n_words': 277}