Beispiel #1
0
 public function getTestConfig()
 {
     $driverOptions = array();
     if (defined('TESTS_ZEND_QUEUE_DB')) {
         $driverOptions = \Zend\JSON\JSON::decode(TESTS_ZEND_QUEUE_DB);
     }
     return array('options' => array(Select\Select::FOR_UPDATE => true), 'driverOptions' => $driverOptions);
 }
Beispiel #2
0
 /**
  * Deserialize JSON to PHP value
  * 
  * @param  string $json 
  * @param  array $opts 
  * @return mixed
  */
 public function unserialize($json, array $opts = array())
 {
     $opts = $opts + $this->_options;
     try {
         $ret = ZendJSON::decode($json, $opts['objectDecodeType']);
     } catch (\Exception $e) {
         throw new SerializationException('Unserialization failed by previous error', 0, $e);
     }
     // json_decode returns null for invalid JSON
     if ($ret === null && $json !== 'null') {
         throw new SerializationException('Invalid json data');
     }
     return $ret;
 }
Beispiel #3
0
 public function testDbAdapterShouldReturnNoMessagesWhenZeroCountRequested()
 {
     if (!constant('TESTS_ZEND_QUEUE_DB_ENABLED')) {
         $this->markTestSkipped('Zend_Queue DB adapter tests are not enabled');
     }
     $driverOptions = array();
     if (defined('TESTS_ZEND_QUEUE_DB')) {
         $driverOptions = \Zend\JSON\JSON::decode(TESTS_ZEND_QUEUE_DB);
     }
     $options = array('name' => '/temp-queue/ZF7650', 'options' => array(\Zend\DB\Select\Select::FOR_UPDATE => true), 'driverOptions' => $driverOptions);
     $queue = new Queue\Queue('Db', $options);
     $queue2 = $queue->createQueue('queue');
     $queue->send('My Test Message 1');
     $queue->send('My Test Message 2');
     $messages = $queue->receive(0);
     $this->assertEquals(0, count($messages));
 }
Beispiel #4
0
 /**
  * Set request state based on JSON
  *
  * @param  string $json
  * @return void
  */
 public function loadJSON($json)
 {
     $options = JSON\JSON::decode($json);
     $this->setOptions($options);
 }
Beispiel #5
0
 public function testJsonContextShouldEncodeViewVariablesByDefaultAndNotRequireRenderingView()
 {
     $this->request->setParam('format', 'json')->setActionName('bar')->setDispatched(true);
     $this->controller->dispatch('barAction');
     $headers = $this->response->getHeaders();
     $found = false;
     foreach ($headers as $header) {
         if ($header['name'] == 'Content-Type') {
             if ($header['value'] == 'application/json') {
                 $found = true;
             }
             break;
         }
     }
     $this->assertTrue($found, 'JSON content type header not found');
     $body = $this->response->getBody();
     $result = \Zend\JSON\JSON::decode($body);
     $this->assertTrue(is_array($result), var_export($body, 1));
     $this->assertTrue(isset($result['foo']));
     $this->assertTrue(isset($result['bar']));
     $this->assertEquals('bar', $result['foo']);
     $this->assertEquals('baz', $result['bar']);
 }
Beispiel #6
0
 public function testToStringImplementationShouldProxyToJSON()
 {
     $options = $this->getOptions();
     $this->smd->setOptions($options);
     $json = $this->smd->__toString();
     $smd = JSON\JSON::decode($json);
     $this->validateServiceArray($smd, $options);
 }
Beispiel #7
0
 public function testJsonHelperReturnsJsonEncodedString()
 {
     $data = $this->helper->direct('foobar');
     $this->assertTrue(is_string($data));
     $this->assertEquals('foobar', \Zend\JSON\JSON::decode($data));
 }
Beispiel #8
0
 public function validateJSON($json, array $options)
 {
     $test = JSON\JSON::decode($json);
     $this->assertTrue(is_array($test), var_export($json, 1));
     $this->assertTrue(array_key_exists('id', $test));
     $this->assertTrue(array_key_exists('method', $test));
     $this->assertTrue(array_key_exists('params', $test));
     $this->assertTrue(is_string($test['id']));
     $this->assertTrue(is_string($test['method']));
     $this->assertTrue(is_array($test['params']));
     $this->assertEquals($options['id'], $test['id']);
     $this->assertEquals($options['method'], $test['method']);
     $this->assertSame($options['params'], $test['params']);
 }
Beispiel #9
0
    /**
     * xml2json Test 6
     * It tests the conversion of demo application xml into JSON format.
     *
     * XML characteristic to be tested: XML containing a large CDATA.
     *
     */
    public function testUsingXML6()
    {
        // Set the XML contents that will be tested here.
        $xmlStringContents = <<<EOT
<?xml version="1.0"?>
<demo>
    <application>
        <name>Killer Demo</name>
    </application>

    <author>
        <name>John Doe</name>
    </author>

    <platform>
        <name>LAMP</name>
    </platform>

    <framework>
        <name>Zend</name>
    </framework>

    <language>
        <name>PHP</name>
    </language>

    <listing>
        <code>
            <![CDATA[
/*
It may not be a syntactically valid PHP code.
It is used here just to illustrate the CDATA feature of Zend_Xml2JSON
*/
<?php
include 'example.php';
new SimpleXMLElement();
echo(getMovies()->movie[0]->characters->addChild('character'));
getMovies()->movie[0]->characters->character->addChild('name', "Mr. Parser");
getMovies()->movie[0]->characters->character->addChild('actor', "John Doe");
// Add it as a child element.
getMovies()->movie[0]->addChild('rating', 'PG');
getMovies()->movie[0]->rating->addAttribute("type", 'mpaa');
echo getMovies()->asXML();
?>
            ]]>
        </code>
    </listing>
</demo>

EOT;
        // There are not going to be any XML attributes in this test XML.
        // Hence, set the flag to ignore XML attributes.
        $ignoreXmlAttributes = true;
        $jsonContents = "";
        $ex = null;
        // Convert XNL to JSON now.
        // fromXml function simply takes a String containing XML contents as input.
        try {
            $jsonContents = JSON\JSON::fromXml($xmlStringContents, $ignoreXmlAttributes);
        } catch (\Exception $ex) {
        }
        $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception.");
        // Convert the JSON string into a PHP array.
        $phpArray = JSON\JSON::decode($jsonContents);
        // Test if it is not a NULL object.
        $this->assertNotNull($phpArray, "JSON result for XML input 6 is NULL");
        // Test for one of the expected fields in the JSON result.
        $this->assertContains("Zend", $phpArray['demo']['framework']['name'], "The framework name field converted from XML input 6 is not correct");
        // Test for one of the expected CDATA fields in the JSON result.
        $this->assertContains('echo getMovies()->asXML();', $phpArray['demo']['listing']['code'], "The CDATA code converted from XML input 6 is not correct");
    }
Beispiel #10
0
 public function testTojsonShouldEmitJSON()
 {
     $this->setupSmdValidationObject();
     $json = $this->service->toJSON();
     $smd = \Zend\JSON\JSON::decode($json);
     $this->assertTrue(array_key_exists('foo', $smd));
     $this->assertTrue(is_array($smd['foo']));
     $this->validateSmdArray($smd['foo']);
 }
Beispiel #11
0
 public function testCanPreventDirectSendingResponse()
 {
     $data = $this->helper->direct(array('foobar'), false);
     $this->assertSame(array('foobar'), JSON\JSON::decode($data));
     $this->verifyJsonHeader();
     $response = $this->response->getBody();
     $this->assertTrue(empty($response));
 }
Beispiel #12
0
 public function testHandleShouldEmitResponseByDefault()
 {
     $this->markTestSkipped('Problems with Zend_Server conversion');
     $this->server->setClass('ZendTest\\JSON\\Foo');
     $request = $this->server->getRequest();
     $request->setMethod('bar')->setParams(array(true, 'foo', 'bar'))->setId('foo');
     ob_start();
     $this->server->handle();
     $buffer = ob_get_clean();
     $decoded = JSON\JSON::decode($buffer);
     $this->assertTrue(is_array($decoded));
     $this->assertTrue(array_key_exists('result', $decoded));
     $this->assertTrue(array_key_exists('id', $decoded));
     $response = $this->server->getResponse();
     $this->assertEquals($response->getResult(), $decoded['result']);
     $this->assertEquals($response->getId(), $decoded['id']);
 }
Beispiel #13
0
 public function testCastToStringShouldCastToJSON()
 {
     $this->response->setResult(true)->setId('foo');
     $json = $this->response->__toString();
     $test = JSON\JSON::decode($json);
     $this->assertTrue(is_array($test));
     $this->assertTrue(array_key_exists('result', $test));
     $this->assertTrue(array_key_exists('error', $test));
     $this->assertTrue(array_key_exists('id', $test));
     $this->assertFalse(array_key_exists('jsonrpc', $test));
     $this->assertTrue($test['result']);
     $this->assertEquals($this->response->getId(), $test['id']);
 }
Beispiel #14
0
 /**
  * @group ZF-8918
  * @expectedException Zend\JSON\Exception
  */
 public function testDecodingInvalidJSONShouldRaiseAnException()
 {
     JSON\JSON::decode(' some string ');
 }
Beispiel #15
0
 public function testCastingToStringShouldCastToJSON()
 {
     $this->setupError();
     $json = $this->error->__toString();
     $this->validateArray(JSON\JSON::decode($json));
 }
Beispiel #16
0
 public function testDojoHelperSendsResponseByDefault()
 {
     $dojo = new Helper\AutoCompleteDojo();
     $dojo->suppressExit = true;
     $data = array('foo', 'bar', 'baz');
     $encoded = $dojo->direct($data);
     $decoded = JSON\JSON::decode($encoded);
     $test = array();
     foreach ($decoded['items'] as $item) {
         $test[] = $item['name'];
     }
     $this->assertSame($data, $test);
     $body = $this->response->getBody();
     $this->assertSame($encoded, $body);
 }