小编vis*_*hal的帖子

如何在功能测试中模拟Symfony 2服务?

我有symfony服务,它在某些方法中使用redis连接,但在所有方法中都没有.

class ServiceA
{
    private $redis;

    public function __construct($redis)
    {
        $this->redis = $redis;
    }

    public function getRequest($param1, $param2)
    {
    $result = $param1+ $param2;
        return $request;
    }

    .. other methods which use $redis connection
}
Run Code Online (Sandbox Code Playgroud)

我正在为仅使用getRequest方法的代码编写功能测试(此方法不需要redis连接),但是当构造函数将连接作为参数时,当我运行test时,它尝试连接redis服务器.

如何编写完全不使用redis连接的模拟服务并忽略原始构造函数.

我正在尝试下面提到的方法,但没有成功.我仍然尝试连接redis,尽管我已经禁用了原始构造函数.

http://blog.lyrixx.info/2013/04/12/symfony2-how-to-mock-services-during-functional-tests.html

$serviceA = $this->getMockBuilder('ServiceA')
    ->disableOriginalConstructor()
    ->getMock();

static::$kernel->getContainer()->set('my_bundle.service.a', $serviceA);
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing mocking symfony

28
推荐指数
2
解决办法
3万
查看次数

如何使用spring shell在spring boot web应用程序中构建控制台命令?

我使用spring boot web starter创建了restfull web应用程序,效果很好.我可以通过网址访问它.

但是我需要创建可以在后端计算和存储一些值的控制台命令.我希望能够手动或通过bash脚本运行控制台命令.

我找不到任何关于如何在spring boot web应用程序中集成spring-shell项目的文档.

在spring boot starter https://start.spring.io/中也没有选择spring-shell依赖的选项

1)webapp和console需要是两个独立的应用程序吗?我需要单独部署它们吗?

2)是否可以在同一个应用程序中部署Web应用程序并运行控制台命令?

3)在shell和Web应用程序之间共享公共代码(模型,服务,实体,业务逻辑)的最佳方法是什么?

有人可以帮忙吗?

java spring spring-boot spring-shell spring-web

12
推荐指数
1
解决办法
1万
查看次数

"执行缓存时发生错误:清除--no-warmup",同时安装symfony-cmf-standard

我试图使用以下方法安装symfony-cmf-standard:

composer.phar create-project symfony-cmf/standard-edition symfony-cmf-standard/ --stability=dev
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

Could not open input file: app/console
Script sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

[RuntimeException]
An error occured when executing the cache:clear --no-warmup
Run Code Online (Sandbox Code Playgroud)

我以管理员身份运行该命令,因此它不应该是权限问题.

php symfony symfony-cmf

10
推荐指数
2
解决办法
3万
查看次数

Tomcat 7没有关闭,进程一直在运行?

我开始使用tomcat 7,

cd /opt/tomcat7/bin    
$/opt/tomcat7/bin ./startup.sh
Run Code Online (Sandbox Code Playgroud)

它显示进程正在运行

root     23206  130  3.4 1323956 572880 pts/2  Sl   07:58   1:05 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dspring.profiles.active=mongo1,maxListenersAllowed -DST_SERVER=mongo1 -Djava.endorsed.dirs=/opt/tomcat7/endorsed -classpath /opt/tomcat7/bin/bootstrap.jar:/opt/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat7 -Dcatalina.home=/opt/tomcat7 -Djava.io.tmpdir=/opt/tomcat7/temp org.apache.catalina.startup.Bootstrap start
Run Code Online (Sandbox Code Playgroud)

如果我使用它关闭它

$/opt/tomcat7/bin ./shutdown.sh
Run Code Online (Sandbox Code Playgroud)

它给出了这个信息

Using CATALINA_BASE:   /opt/tomcat7
Using CATALINA_HOME:   /opt/tomcat7
Using CATALINA_TMPDIR: /opt/tomcat7/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat7/bin/bootstrap.jar:/opt/tomcat7/bin/tomcat-juli.jar
Run Code Online (Sandbox Code Playgroud)

但如果我检查上面的过程,它仍会显示它正在运行.Tomcat没有关闭.我也尝试使用root用户,但仍然没有成功.

Manully我可以杀死进程,但我想创建部署脚本,所以想要使用shutdown.sh和startup.sh来做

如果我尝试使用同样的事情

/opt/tomcat7/bin/catalina.sh start
/opt/tomcat7/bin/catalina.sh stop
Run Code Online (Sandbox Code Playgroud)

日志

Jul 23, 2014 8:26:17 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was …
Run Code Online (Sandbox Code Playgroud)

java tomcat

10
推荐指数
1
解决办法
3万
查看次数

如何调试python单击cli应用程序?

我使用python中的click库构建了一个cli应用程序.没有关于如何调试命令的文档.

没有单击,它很容易在IDE中调试python文件,但是当我们使用click时,命令需要通过setup.py中的console_scripts设置运行.

python python-3.x python-click

9
推荐指数
2
解决办法
2872
查看次数

如何在symfony 2中创建映射到外部URL的路由?

参考这个,

http://symfony.com/doc/current/book/routing.html

我们可以将url模式映射到控制器和动作

应用程序/配置/ routing.yml中

blog_show:
    path:      /blog/{slug}
    defaults:  { _controller: AcmeBlogBundle:Blog:show }
Run Code Online (Sandbox Code Playgroud)

我想将路径映射到外部URL.

应用程序/配置/ routing.yml中

blog_show:
    path:      /blog/{slug}
    defaults:  "www.example.com/blog"
Run Code Online (Sandbox Code Playgroud)

要求是,我现在的网站在kohana,我逐渐将它移植到symfony 2.对于我的symfony2 app kohana URL就像外部网址,我想在路由中配置这些网址并以标准方式使用它们,

例如在Twig,

<a href="{{ path('blog_show'}}">
  Read this blog post.
</a>
Run Code Online (Sandbox Code Playgroud)

所以稍后当我将我的页面移植到Symfony时,我将只需要更改路由文件,这样我就可以使用相同的blog_show键来引用url,而且我不必更改我使用url的所有文件.

php symfony twig

8
推荐指数
2
解决办法
6564
查看次数

mysql游标中的记录数没有迭代?

我正在尝试为下面的逻辑编写mysql程序,

select id, fullname from users where fullname like concat(lastname, ' ', firstname, ' (' , middlename, '%');
Run Code Online (Sandbox Code Playgroud)

如果上面的查询返回0记录然后

    select id, fullname from users where fullname like concat(lastname, ' ', firstname, '%');

.... few more queries depending upon result,
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写mysql程序的程序,在那,我使用的是mysql游标,

DECLARE user_cnt CURSOR FOR select id, fullname from users where fullname like concat(lastname, ' ', firstname, ' (' , middlename, '%'); 
Run Code Online (Sandbox Code Playgroud)

现在我想检查user_cursor中的记录数,以便我可以检查条件"如果查询返回0记录然后"

如何在没有迭代的情况下找到mysql游标中的记录数?

mysql count cursor

8
推荐指数
1
解决办法
1万
查看次数

覆盖spring批处理admin以使用mysql数据库

我试图在spring batch admin中使用mysql数据库而不是默认的HSQL.对于那个文件

http://docs.spring.io/spring-batch-admin/reference/reference.xhtml使用jndi数据源与spring batch admin

我复制env-context.xmlsrc/main/resources/META-INF/batch/override/manager/env-context.xml 和改变其配置价值

<value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>
Run Code Online (Sandbox Code Playgroud)

 <value>classpath:batch-mysql.properties</value>
Run Code Online (Sandbox Code Playgroud)

以下是我的完整配置.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--  Use this to set additional properties on beans at run time -->
    <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                <value>classpath:batch-default.properties</value>
                <value>classpath:batch-mysql.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="order" value="1" />
    </bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

我还尝试将data-source-context.xml复制到同一文件夹并将其配置更改为mysql

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-batch spring-batch-admin

8
推荐指数
1
解决办法
5936
查看次数

错误:您无法创建非活动范围的服务("templating.helper.assets")("请求")

我收到以下错误,

  [Twig_Error_Runtime]                                                                                                                                                                                     
  An exception has been thrown during the rendering of a template ("You cannot create a    service ("templating.helper.assets") of an inactive scope ("request").") in "AcmeMessagingBundle:Comment:email.html.twig".
Run Code Online (Sandbox Code Playgroud)

我正在从symfony 2自定义控制台命令渲染twig模板

下面是我的服务类,它是事件订阅者,我通过symfony console命令触发onCommentAddEmail事件来发送电子邮件,

class NotificationSubscriber implements EventSubscriberInterface
{
     private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }


    public static function getSubscribedEvents()
    {
         return array(
            'comment.add'     => array('onCommentAddEmail', 0),
         );
     }

     public function onCommentAddEmail(CommentAddEvent $event)
     {
              ...................


             $body = $this->container->get('templating')->render(
            'AcmeMessagingBundle:Comment:email.html.twig',
                array('template' => $template)
             );

     .......


    }

}
Run Code Online (Sandbox Code Playgroud)

$ body传递给swiftmailer发送电子邮件.

这是我的服务定义,

ACME\MessagingBundle …

symfony twig

7
推荐指数
2
解决办法
7704
查看次数

如果已经在其他应用程序的主域中登录,如何使用symfony安全性登录到子域?

我已经登录到主域。说出example.com(使用旧版kohana开发的应用程序)。我正在尝试登录subdmain,例如foo.bar.example.com

foo.example.com是symfony应用。下面是我的配置。开发人员太栏显示“匿名”用户。它不会从cookie中的会话ID登录用户。

安全性

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory:
            memory: ~

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: ~
            # activate different ways to authenticate

            # http_basic: ~
            # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: ~
            # http://symfony.com/doc/current/cookbook/security/form_login_setup.html
Run Code Online (Sandbox Code Playgroud)

配置文件

framework:
    #esi:             ~
    #translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: …
Run Code Online (Sandbox Code Playgroud)

php symfony symfony-security

6
推荐指数
1
解决办法
633
查看次数