<commit_before><?php

namespace Phalcon\Test\Session\Adapter;

use Phalcon\Session\Adapter\Aerospike as SessionHandler;
use Codeception\TestCase\Test;
use UnitTester;
use Aerospike;

/**
 * \Phalcon\Test\Session\Adapter\AerospikeTest
 * Tests for Phalcon\Session\Adapter\Aerospike component
 *
 * @copyright (c) 2011-2016 Phalcon Team
 * @link      http://www.phalconphp.com
 * @author    <NAME> <<EMAIL>>
 * @package   Phalcon\Test\Session\Adapter
 * @group     aerospike
 *
 * The contents of this file are subject to the New BSD License that is
 * bundled with this package in the file docs/LICENSE.txt
 *
 * If you did not receive a copy of the license and are unable to obtain it
 * through the world-wide-web, please send an email to <EMAIL>
 * so that we can send you a copy immediately.
 */
class AerospikeTest extends Test
{
    /**
     * UnitTester Object
     * @var UnitTester
     */
    protected $tester;

    protected $ns = 'test';
    protected $set = 'session';
    protected $keys = [];

    /**
     * executed before each test
     */
    protected function _before()
    {
        if (!extension_loaded('aerospike')) {
            $this->markTestSkipped('The Aerospike module is not available.');
        }

        $this->getModule('Aerospike')->_reconfigure([
            'set'  => $this->set,
            'addr' => TEST_AS_HOST,
            'port' => TEST_AS_PORT
        ]);
    }

    /**
     * executed after each test
     */
    protected function _after()
    {
        $this->cleanup();
    }

    public function testShouldWriteSession()
    {
        $sessionId = 'abcdef123458';
        $session = new SessionHandler($this->getConfig());

        $data = serialize(
            [
                321   => microtime(true),
                'def' => '678',
                'xyz' => 'zyx'
            ]
        );

        $this->assertTrue($session->write($sessionId, $data));
        $this->tester->seeInAerospike($sessionId, serialize($data));
    }

    public function testShouldReadSession()
    {
        $sessionId = 'some_session_key';
        $session = new SessionHandler($this->getConfig());

        $data = serialize(
            [
                321   => microtime(true),
                'def' => '678',
                'xyz' => 'zyx'
            ]
        );

        $this->tester->haveInAerospike($sessionId, serialize($data));
        $this->keys[] = $sessionId;

        $this->assertEquals($data, $session->read($sessionId));
    }

    public function testShouldDestroySession()
    {
        $sessionId = 'abcdef123457';
        $session = new SessionHandler($this->getConfig());

        $data = serialize(
            [
                'abc' => 345,
                'def' => ['foo' => 'bar'],
                'zyx' => 'xyz'
            ]
        );

        $this->tester->haveInAerospike($sessionId, serialize($data));
        $session->destroy($sessionId);
        $this->tester->dontSeeInAerospike($sessionId);
    }

    private function cleanup()
    {
        $aerospike = new Aerospike(
            [
                'hosts' => [
                    ['addr' => TEST_AS_HOST, 'port' => TEST_AS_PORT]
                ]
            ],
            false
        );

        foreach ($this->keys as $i => $key) {
            $aerospike->remove($this->buildKey($aerospike, $key));
            unset($this->keys[$i]);
        }
    }

    private function buildKey(Aerospike $aerospike, $key)
    {
        return $aerospike->initKey(
            $this->ns,
            $this->set,
            $key
        );
    }

    private function getConfig()
    {
        return [
            'hosts' => [
                ['addr' => TEST_AS_HOST, 'port' => TEST_AS_PORT]
            ],
            'persistent' => false,
            'namespace'  => $this->ns,
            'set'        => $this->set,
            'prefix'     => '',
            'lifetime'   => 10,
            'uniqueId'   => 'some-unique-id',
            'options' => [
                \Aerospike::OPT_CONNECT_TIMEOUT => 1250,
                \Aerospike::OPT_WRITE_TIMEOUT => 1500
            ]
        ];
    }
}
<commit_msg>Data is being serialized twice<commit_after><?php

namespace Phalcon\Test\Session\Adapter;

use Phalcon\Session\Adapter\Aerospike as SessionHandler;
use Codeception\TestCase\Test;
use UnitTester;
use Aerospike;

/**
 * \Phalcon\Test\Session\Adapter\AerospikeTest
 * Tests for Phalcon\Session\Adapter\Aerospike component
 *
 * @copyright (c) 2011-2016 Phalcon Team
 * @link      http://www.phalconphp.com
 * @author    <NAME> <<EMAIL>>
 * @package   Phalcon\Test\Session\Adapter
 * @group     aerospike
 *
 * The contents of this file are subject to the New BSD License that is
 * bundled with this package in the file docs/LICENSE.txt
 *
 * If you did not receive a copy of the license and are unable to obtain it
 * through the world-wide-web, please send an email to <EMAIL>
 * so that we can send you a copy immediately.
 */
class AerospikeTest extends Test
{
    /**
     * UnitTester Object
     * @var UnitTester
     */
    protected $tester;

    protected $ns = 'test';
    protected $set = 'session';
    protected $keys = [];

    /**
     * executed before each test
     */
    protected function _before()
    {
        if (!extension_loaded('aerospike')) {
            $this->markTestSkipped('The Aerospike module is not available.');
        }

        $this->getModule('Aerospike')->_reconfigure([
            'set'  => $this->set,
            'addr' => TEST_AS_HOST,
            'port' => TEST_AS_PORT
        ]);
    }

    /**
     * executed after each test
     */
    protected function _after()
    {
        $this->cleanup();
    }

    public function testShouldWriteSession()
    {
        $sessionId = 'abcdef123458';
        $session = new SessionHandler($this->getConfig());

        $data = [
                321   => microtime(true),
                'def' => '678',
                'xyz' => 'zyx'
            ];

        $this->assertTrue($session->write($sessionId, $data));
        $this->tester->seeInAerospike($sessionId, serialize($data));
    }

    public function testShouldReadSession()
    {
        $sessionId = 'some_session_key';
        $session = new SessionHandler($this->getConfig());

        $data = [
                321   => microtime(true),
                'def' => '678',
                'xyz' => 'zyx'
            ];

        $this->tester->haveInAerospike($sessionId, serialize($data));
        $this->keys[] = $sessionId;

        $this->assertEquals($data, $session->read($sessionId));
    }

    public function testShouldDestroySession()
    {
        $sessionId = 'abcdef123457';
        $session = new SessionHandler($this->getConfig());

        $data = [
                'abc' => 345,
                'def' => ['foo' => 'bar'],
                'zyx' => 'xyz'
            ];

        $this->tester->haveInAerospike($sessionId, serialize($data));
        $session->destroy($sessionId);
        $this->tester->dontSeeInAerospike($sessionId);
    }

    private function cleanup()
    {
        $aerospike = new Aerospike(
            [
                'hosts' => [
                    ['addr' => TEST_AS_HOST, 'port' => TEST_AS_PORT]
                ]
            ],
            false
        );

        foreach ($this->keys as $i => $key) {
            $aerospike->remove($this->buildKey($aerospike, $key));
            unset($this->keys[$i]);
        }
    }

    private function buildKey(Aerospike $aerospike, $key)
    {
        return $aerospike->initKey(
            $this->ns,
            $this->set,
            $key
        );
    }

    private function getConfig()
    {
        return [
            'hosts' => [
                ['addr' => TEST_AS_HOST, 'port' => TEST_AS_PORT]
            ],
            'persistent' => false,
            'namespace'  => $this->ns,
            'set'        => $this->set,
            'prefix'     => '',
            'lifetime'   => 10,
            'uniqueId'   => 'some-unique-id',
            'options' => [
                \Aerospike::OPT_CONNECT_TIMEOUT => 1250,
                \Aerospike::OPT_WRITE_TIMEOUT => 1500
            ]
        ];
    }
}

{'fraction_non_alphanumeric': 0.13961988304093567, 'fraction_numerical': 0.010964912280701754, 'mean_word_length': 1.759327731092437, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3944871', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2388, 'n_words': 509}