我正在尝试为一个模型编写测试,该模型包含一些普通验证器和一个使用实体管理器和请求的自定义验证器.如果由于某种原因这很重要,我正在使用phpunit进行测试.
我通过对实体管理器和请求进行存根,然后验证某些对象来测试另一个测试中的自定义验证器.由于这证明自定义验证有效,我只需要测试正常验证,如果可以,只需将自定义验证器保留.
这是我的模型:
/**
* @MyAssert\Client()
*/
abstract class BaseRequestModel {
/**
* @Assert\NotBlank(message="2101")
*/
protected $clientId;
/**
* @Assert\NotBlank(message="2101")
*/
protected $apiKey;
// ...
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我得到验证器,创建一个对象,然后验证它.
$validator = ValidatorFactory::buildDefault()->getValidator();
$requestModel = new RequestModel();
$errors = $validator->validate($requestModel);
Run Code Online (Sandbox Code Playgroud)
当然这会失败,因为它找不到为MyAssert\Client定义的Validator,它是一个服务,需要通过某个依赖注入容器来解析.
任何人都知道如何存根自定义验证器或将其从验证中排除?
我想使用symfony2为我的应用程序创建一个状态页面,我想打印特定请求的执行时间(以及其他数据).无论如何我都找不到这样做.
我知道我可以跟踪代码部分的执行时间:
$starttime = microtime();
// do something
$duration = microtime() - $starttime;
Run Code Online (Sandbox Code Playgroud)
但由于显而易见的原因,我不能将它放在控制器中,因为整个引导程序都不会被跟踪.也不包括渲染模板.
有没有办法尽可能接近脚本的总执行时间?
我想在config_test.yml中覆盖config_dev.yml中的一些配置.所以,想象一下config_dev.yml中的以下部分:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
firephp:
type: firephp
level: info
Run Code Online (Sandbox Code Playgroud)
在我的测试环境中,我根本不需要记录器.所以我试过了
monolog: ~
Run Code Online (Sandbox Code Playgroud)
没有效果.我也尝试过:
monolog:
handlers:
main: ~
firephp: ~
Run Code Online (Sandbox Code Playgroud)
再没有任何影响.然后我测试了
monolog:
handlers:
main:
type: ~
path: ~
level: ~
firephp:
type: ~
level: ~
Run Code Online (Sandbox Code Playgroud)
我得到一个ErrorException Couldn't find constant Monolog\Logger::.如果有人能指出一种方法来覆盖monolog设置,我将非常感激.谢谢!
我正在使用symfony2一段时间,我没有真正得到与供应商合作的正确方法.
所以这就是我正在做的事情:
我忽略整个供应商文件夹时,我的git中有deps和deps.lock文件.现在,当我将应用程序安装到新服务器时,我php bin/vendors install会将供应商拉到服务器上.我得到了必须使用的信息install --reinstall并且这样做.
根据我的理解,现在版本应该与我的开发机器完全相同,因为deps和deps.lock都是相同的.但似乎deps.lock被(部分)忽略了?
还有一个vendors update命令,我读不应该使用.我没想到它真正做到了什么.
所以我现在有点困惑,因为应该使用什么命令以及它应该做什么.也许有人可以对这个话题有所了解!我在使用厂商命令本地和服务器,使供应商在两个系统上的正确版本的正确方式特别感兴趣!
我正试图让yui压缩机运行资产,如果这是运行的话,那就是sass.现在,两者都不起作用.从config.yml和twig模板中删除所有过滤器时,它可以工作并php app/console assetic:dump复制css和js文件.
现在我想添加yui压缩器,我的config.yml看起来像这样:
assetic:
debug: %kernel.debug%
use_controller: false
filters:
yui_js:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar
Run Code Online (Sandbox Code Playgroud)
将过滤器添加到模板并再次运行assetic:dump会在以下错误中结束(由我翻译消息):
[RuntimeException]
The syntax for filename, directory name or drive name is wrong
Run Code Online (Sandbox Code Playgroud)
我找到一篇文章告诉我指定java.exe的路径,所以我将它添加到config.yml:
assetic:
..
java: C:/Program Files (x86)/Java/jre6/bin/java.exe
..
Run Code Online (Sandbox Code Playgroud)
现在资产:转储告诉我:
[RuntimeException]
The COMMAND "C:/Program" is either written wrong or
Run Code Online (Sandbox Code Playgroud)
我尝试在配置中使用两个变量(使用\或\而不是/,添加单引号或双引号,使用短别名Progra~1或Progra~2),但我没有得到任何结果.这两个错误一直在增加.也许有人可以指出我正确的方向.
我有多个共享公共实体的symfony2应用程序,但使用不同的数据库设置.每个数据库都有表user,user_role和role.
这里有一个问题:我希望该用户能够app1通过访问www.myproject.com/app1/login和更改URL后登录到仅/app2/使用现有令牌,如果数据库中存在相同的用户(相同的用户名,密码和盐).目前它只检查相同的用户名,你必须同意,非常不方便...app2
我真的不知道什么时候refreshUser()被叫...: - /
所有应用程序使用相同的User和Role实体UserRepository.
任何帮助将非常感激!
UserRepository:
class UserRepository extends EntityRepository implements \Symfony\Component\Security\Core\User\UserProviderInterface{
/** @var User */
private $user;
public function loadUserByUsername($username) {
/** @var $Q \Doctrine\ORM\Query */
$Q = $this->getEntityManager()
->createQuery('SELECT u FROM CommonsBundle:User u WHERE u.username = :username')
->setParameters(array(
'username' => $username
));
$user = $Q->getOneOrNullResult();
if ( $user == null ){
throw new …Run Code Online (Sandbox Code Playgroud) 我目前正在为wit.ai编写一个API包装器.我想为这个包装器添加测试但是我不确定我是如何做到的,因为我正在使用该http库来发送HTTP请求.
代码看起来像这样:
Future message(String q) {
Map<String, String> headers = {
'Accept': 'application/vnd.wit.${apiVersion}+json',
'Authorization': 'Bearer ${token}'
};
return http
.get('https://api.wit.ai/message?q=${q}', headers: headers)
.then((response) {
return JSON.decode(response.body);
}).catchError((e, stackTrace) {
return JSON.decode(e);
});
}
Run Code Online (Sandbox Code Playgroud)
鉴于此代码,我将如何编写一个实际上不发送HTTP请求的测试?
我是symfony2的新手,当用户点击div时,我在使用ajax加载视图时遇到了一些问题.使用firebug我可以看到返回的数据,但我无法将结果附加到页面中.
我的代码://默认控制器
public function indexAction($num, Request $request)
{
$request = $this->getRequest();
if($request->isXmlHttpRequest()){
$content = $this->forward('PaginationBundle:Default:ajax');
$res = new Response($content);
return $res;
}
return $this->render('PaginationBundle:Default:index.html.twig', array('num' => $num));
}
public function ajaxAction()
{
return $this->render('PaginationBundle:Default:page.html.twig');
}
}
Run Code Online (Sandbox Code Playgroud)
我的Js:当点击#target时,我想在我的div中加载page.html.twig
$("div#target").click(function(event){
t = t +1;
$.ajax({
type: "POST",
cache: "false",
dataType: "html",
success: function(){
$("div#box").append(data);
}
});
});
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中使用isXmlHttpRequest()来检测它是否是加载ajaxAction的ajax请求.我在firebug上看到了那个视图,但它没有附加在我的div#box中.div#box存在于index.html.twig中
提前谢谢大家
我试图渲染模板不使用Symfony2所需格式'Bundle:Controller:file_name',但想要从某个自定义位置渲染模板.
控制器中的代码抛出异常
可捕获的致命错误:类__TwigTemplate_509979806d1e38b0f3f78d743b547a88的对象无法在Symfony/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php第50行中转换为字符串
我的代码:
$loader = new \Twig_Loader_Filesystem('/path/to/templates/');
$twig = new \Twig_Environment($loader, array(
'cache' => __DIR__.'/../../../../app/cache/custom',
));
$tmpl = $twig->loadTemplate('index.twig.html');
return $this->render($tmpl);
Run Code Online (Sandbox Code Playgroud)
甚至可以在Symfony中做这样的事情,或者我们只能使用逻辑名称格式?
我findOneBy在我的实体中找到(使用)一个单行.这是代码:
$userown = $this->getDoctrine()->getRepository('GameShelfUsersBundle:Own')
->findOneBy(array(
'game' => $game->getId(),
'user' => $em->getRepository('GameShelfUsersBundle:User')->find($session->getId())
));
Run Code Online (Sandbox Code Playgroud)
现在我将它传递给模板userown.但是当我尝试在树枝上打印时,使用{{ userown.typo }}它会引发错误:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies\__CG__\GameShelf\UsersBundle\Entity\OwnState could not be converted to string in D:\!!XAMPP\htdocs\
Run Code Online (Sandbox Code Playgroud)
我的实体就在这里.
我一直在为学校做项目.基本上你必须用几个扫描仪制作一个脚本.例如:
我现在想为这两个函数制作一个Scanner类.但是1必须是一个整数,第二个必须是一个整数.如何确保函数返回double或整数.
我使用以下代码:
public static [Heres what goes wrong] vrager (String type, String tekst) {
Scanner vraag = new Scanner(System.in);
System.out.println(tekst);
type variable = vraag.next();
return variable;
Run Code Online (Sandbox Code Playgroud)
所以通过调用函数就像: seconden = (vrager("int", "How many seconds?:));
但是,如果我想让函数也能工作,那么它就会出错,因为函数不会期望返回双精度.
我该如何解决这个问题?