尝试在实现Sessions的控制器方法上运行基于控制器的单元测试时,我遇到了一个问题.
在这种情况下,这是控制器方法:
/**
* @Route("/api/logout")
*/
public function logoutAction()
{
$session = new Session();
$session->clear();
return $this->render('PassportApiBundle:Login:logout.html.twig');
}
Run Code Online (Sandbox Code Playgroud)
功能测试:
public function testLogout()
{
$client = static::createClient();
$crawler = $client->request('GET', '/api/logout');
$this->assertTrue($client->getResponse()->isSuccessful());
}
Run Code Online (Sandbox Code Playgroud)
产生的错误:
无法启动会话,因为已经发送了标头.(500内部服务器错误)
我已经尝试$this->app['session.test'] = true;进入测试,但仍然没有去.有没有人尝试解决这样的问题来对使用会话的控制器进行单元测试?
在我的Symfony2包中,我需要检查是否定义了一个函数(扩展名).更具体地说,如果安装了KnpMenuBundle,我在我的包中使用那个,否则我将自己渲染插件.
我尝试了这个,但这当然不起作用:
{% if knp_menu_render() is defined %}
{# do something #}
{% else %}
{# do something else #}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
是否有测试/功能/过滤器来检查功能是否已定义?如果没有,是否有另一种方法来检查应用程序中是否安装了KnpMenuBundle?
我dbal在Symfony2应用程序中使用Doctrine的服务.
我查询一个不存在的表,它会抛出一个错误:
SQLSTATE [42S02]:找不到基表或视图:1146表'log.requests_20130311'不存在.
Symfony2在我能够做到这一点之前就抓住了这个,即使是在一个try-catch街区.我不希望这会杀死我的应用程序.我该怎么处理?
这是一个非常主观的问题,所以我会更具体.有没有时间do-while循环比正常的while循环更好的编码风格?
例如
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);`
Run Code Online (Sandbox Code Playgroud)
在评估do语句(也就是强制do语句至少运行一次)之后检查while条件似乎没有意义.
对于像我上面的例子那样简单的东西,我会想象:
int count = 0;
while(count < 10) {
System.out.println("Welcome to Java"); count++;
}
Run Code Online (Sandbox Code Playgroud)
通常被认为是以更好的写作风格写成的.
任何人都可以为我提供一个工作示例,说明什么时候将do-while循环视为唯一/最佳选择?你的代码中有do-while循环吗?它起什么作用?你为什么选择do-while循环?
(我有一种深刻的感觉,do-while循环可能在编码游戏中有用.如果我错了,请纠正我,游戏开发者!)
目前我正在我的一项服务中实例化第三方课程:
$message = new ChatMessage($apiKey);
$message->setFrom($fromUser);
$message->setBody($messageText);
$message->etc ...
Run Code Online (Sandbox Code Playgroud)
我想把它变成一项服务.apiKey在运行时根据消息的发送位置确定,因此我需要能够在运行时设置它.我已经为服务创建了这个配置:
class ChatMessageFactory
{
function createChatMessage($apiKey) {
return new ChatMessage($apiKey);
}
}
services.yml:-
services:
chatmessage_manager:
class: ChatMessage
factory: [ChatMessageFactory, createChatMessage]
arguments: ["%flow.apiKey%"]
Run Code Online (Sandbox Code Playgroud)
但是如何将我的参数传递给createChatMessage?不知道什么时候创建容器?
我有实体"新闻"与字段:
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="text")
*/
protected $description;
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中生成表单:
public function createNewsAction(Request $request)
{
$news = new News();
$news->setName('New!');
$news->setDescription('test');
$form = $this->createFormBuilder($news)
->add('name', TextType::class)
->add('description', TextType::class)
->add('save', SubmitType::class, array('label' => 'Create Task'))
->getForm();
return $this->render('frontend/createNews.html.twig', array(
'form' => $form->createView(),
));
}
Run Code Online (Sandbox Code Playgroud)
当我尝试调用此方法("createNewsAction")时,我收到错误:
Could not load type "Doctrine\DBAL\Types\TextType"
500 Internal Server Error - InvalidArgumentException
Stack Trace
in vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php at line 87 -
if (class_exists($name) …Run Code Online (Sandbox Code Playgroud) 我希望有一个超时功能,所以当提交表单时,提交后两秒显示警报.我使用的代码不起作用:
$(document).ready(function() {
$("#newsletter").submit(function() {
setTimeout(function() {
alert("submitted");
}, 2000);
});
});
Run Code Online (Sandbox Code Playgroud)
但是当我将2000ms更改为900时似乎工作正常:
$(document).ready(function() {
$("#newsletter").submit(function() {
setTimeout(function() {
alert("submitted");
}, 900);
});
});
Run Code Online (Sandbox Code Playgroud)
如何让2000ms工作?
我尝试了Silex中的示例,并将我的控制器放在一个单独的目录和类中.
默认情况下,控制器方法不会传递Request和Application传递对象.这适用于我的开发机器,它有5.3.14但不是默认的Ubuntu 5.3.2.它给了我:
PHP Catchable致命错误:传递给Sv\Controller\Index :: index()的参数1必须是Symfony\Component\HttpFoundation\Request的实例,没有给出,在第23行的/site/include/app/bootstrap.php中调用并在第46行的/site/include/app/Sv/Controller/Index.php中定义
这是我的引导程序PHP:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Sv\Repository\PostRepository;
use Sv\Controller;
$app = new Silex\Application();
// define global service for db access
$app['posts.repository'] = $app->share( function () {
return new Sv\Repository\PostRepository;
});
// register controller as a service
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app['default.controller'] = $app->share(function () use ($app) {
return new Controller\Index();
});
$app['service.controller'] = $app->share(function () use ($app) {
return new Controller\Service($app['posts.repository']);
});
// define routes
$app->get('/', 'default.controller:index');
$app->get('/next', 'default.controller:next');
$service …Run Code Online (Sandbox Code Playgroud) 我有一个while循环请求用户输入.在while while循环中我有一个switch语句.如何才能使其达到默认值,重复循环再次询问用户性别?
do
{
cout << "What is your weight?" << endl;
cin >> weight;
cout << "What is your height?" << endl;
cin >> height;
cout << "What is your age?" << endl;
cin >> age;
cout << "What is your gender?" << endl;
cin >> gender;
switch (gender)
{
case 'M':
case 'm':
cout << endl << Male(weight, height, age);
break;
case 'F':
case 'f':
cout << endl << Female(weight, height, age);
break;
default:
cout << "What is …Run Code Online (Sandbox Code Playgroud) 我想在一种形式中使用来自少数实体的字段,我可以这样做吗?例如,我想surname从ProfileType类型和name字段中添加一个表单字段CountryType.这个字段必须是一个简单的字符串(text).
我该怎么做?谢谢!
注意:我不能使用实体类型,因为Symfony只提供复选框,单选按钮并选择它,但是我需要使用一个简单的文本字段.
symfony ×6
php ×4
do-while ×2
c++ ×1
doctrine-orm ×1
java ×1
javascript ×1
jquery ×1
loops ×1
phpunit ×1
settimeout ×1
silex ×1
twig ×1
while-loop ×1