Beispiel #1
0
        return $this->__bar;
    }
    protected function getFooBarService()
    {
        return $this->__foo_bar;
    }
    protected function getFoo_BazService()
    {
        return $this->__foo_baz;
    }
}
$sc = new ProjectServiceContainer();
$t->is(spl_object_hash($sc->getService('bar')), spl_object_hash($sc->__bar), '->getService() looks for a getXXXService() method');
$t->ok($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method');
$sc->setService('bar', $bar = new stdClass());
$t->isnt(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()');
try {
    $sc->getService('baba');
    $t->fail('->getService() thrown an \\InvalidArgumentException if the service does not exist');
} catch (\InvalidArgumentException $e) {
    $t->pass('->getService() thrown an \\InvalidArgumentException if the service does not exist');
}
try {
    $sc->baba;
    $t->fail('->__get() thrown an \\InvalidArgumentException if the service does not exist');
} catch (\InvalidArgumentException $e) {
    $t->pass('->__get() thrown an \\InvalidArgumentException if the service does not exist');
}
try {
    unset($sc->baba);
    $t->fail('->__unset() thrown an LogicException if you try to remove a service');
    }
    public function get()
    {
        return $this->foo;
    }
}
// @Before
$paramHolder = new Sonata_ParameterHolder();
$paramHolder->add($params = array('foo' => 42, 'bar' => 4711, 'baz' => new Foo()));
// @After
unset($paramHolder);
unset($params);
// @Test: ->add()
$t->is($paramHolder->getAll(), $params, 'All parameters were added correctly');
$paramHolder->add(null);
$t->isnt($paramHolder->getAll(), null, 'Nothing is added if NULL was given');
$paramHolder->add(array());
$t->isntSame($paramHolder->getAll(), array(), 'Nothing is added if an empty array was given');
$paramHolder->add('this fails');
$t->isnt($paramHolder->getAll(), 'this fails', 'Nothing is added if parameter isn\'t an array');
// @Test: ->get()
$foo = $paramHolder->get('foo');
$t->is($foo, 42, 'The value is retrieved correctly');
$rex = $paramHolder->get('rex');
$t->is($rex, null, 'NULL is returned if the parameter could not be found');
$rex = $paramHolder->get('rex', 'fido');
$t->is($rex, 'fido', 'If a default value is given for a non-existing parameter, the default value is returned');
// @Test: ->getNames()
$t->same($paramHolder->getNames(), array('foo', 'bar', 'baz'), 'All parameter names are retrieved correctly');
// @Test: ->has()
$t->is($paramHolder->has('foo'), true, 'TRUE is returned if the parameter exists');
// @Test: ->getStatusCode()
$t->is($res->getStatusCode(), 200, 'Returns HTTP status code 200 per default');
// @Test: ->getStatusText()
$t->is($res->getStatusText(), 'OK', 'Returns HTTP status text \'OK\' per default');
// @Test: ->setStatusCode()
$res->setStatusCode(404);
$t->is($res->getStatusCode(), 404, 'Sets HTTP status code correctly');
$t->is($res->getStatusText(), 'Not Found', 'If no status text is given the default text is returned');
$res->setStatusCode(666, 'Some random shit');
$t->is($res->getStatusText(), 'Some random shit', 'If status text is given it will be returned correctly');
// @Test: ->setFormat()
$res->setFormat('json');
$t->is($res->getFormat(), 'json', 'The format was set correctly');
$t->is($res->getMimeType(), 'application/json', 'The mime type was set correctly, too');
$res->setFormat('foo');
$t->isnt($res->getFormat(), 'foo', 'The format is not set if no matching format/mime type can be found');
// @Test: ::registerMimeType()
Sonata_Response::registerMimeType('foo', 'application/foo');
$res->setFormat('foo');
$t->is($res->getFormat(), 'foo', 'After registering a new format/mime type, the format can be set to it');
$t->is($res->getMimeType(), 'application/foo', 'The mime type was set correctly, too');
Sonata_Response::registerMimeType('html', 'application/bar');
$res->setFormat('html');
$t->isnt($res->getMimeType(), 'application/bar', 'Existing mime type cannot be altered');
// @Test: ->addHeader()
$res->addHeader('Content-type', 'text/xml');
$t->is($res->getHeaders(), array('Content-type' => 'text/xml'), 'The header was added correctly to the headers array');
$res->addHeader('Content-encoding', 'utf-8');
$t->is($res->getHeaders(), array('Content-type' => 'text/xml', 'Content-encoding' => 'utf-8'), 'A second one, too');
// @Test: ->appendToBody()
$res->appendToBody('Hello World.');