dragon071111 2013-10-24 14:20
浏览 94

phpunit是否使用与MAMP中配置的文件不同的php.ini文件?

I'm trying to run some functional tests using phpunit in Symfony2. When I run my test, I get this error:

PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

I'm assuming phpunit cannot find my mysql socket, though I have set it in my php.ini file (in MAMP.) My web application works fine in my browser and succeeds in connecting to the database.

I've tried adding this setting to my phpunit.xml.dist file but I get the same result:

<php>
    <ini name="mysql.default_socket" value="/Applications/MAMP/tmp/mysql/mysql.sock"/>
    <!--<server name="KERNEL_DIR" value="/path/to/your/app/" />-->
</php>

Is there any way to verify/ change the php.ini file that phpunit is using?

If that's not the problem, then let's go back to square one. Here's the test I ran:

class VBMTest extends WebTestCase
{
var $container;

public function setUp()
{
     $client = static::createClient();
     $client->getKernel()->boot();
     $this->container =  $client->getContainer();
}

public function testNameNotFound() 
{
    $form = array('lastname'=>'Jones','sex'=>'F','day'=>'5','month'=>'5','year'=>'2005');
    $vbm = $this->container->get('tk.vbm');
    $vbm->init($form, 'process_app');
    $step = $vbm->getStep();

    $this->assertEquals(
        'register',
        $step
    );
}
}

I added this line, in case the testing app wasn't loading all of my parameters. (I'm new to testing in Symfony2.)

 $client->getKernel()->boot();

config_test.yml imports config_dev.yml, so I don't see why my database settings would not be included in my testing configuration.

UPDATE

I tried uninstalling phpunit and reinstalling it using Composer as mentioned here~ Installing PHPUnit on MAMP 2.1.3 (Mountain Lion).

So now I run my phpunit command with bin/phpunit.

Still -no change in the outcome. I still receive the "PDO::__construct(): [2002] No such file or directory" error.

UPDATE II

As I mentioned below - responding to @Udan's suggestion - I changed the config settings in my config_test.yml file to change the host to 127.0.0.1:

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     127.0.0.1
        port:     3306
        dbname:   test_tk
        user:     test_tk
        password: ############
        charset:  UTF8

I even added a new user and a new database for testing (test_tk). The error changed, but I still can't test my application.

ERROR 1045 (28000): Access denied for user 'test_tk'@'localhost' (using password: YES)

Here's a synopsis of my user's privileges from phpmyadmin:

User:        test_tk
Host:        %
Type:        global
Privileges:  All Privileges
Grant:       Yes

There is something screwy going on with my mysql installation. I used to be able to connect to mysql through the command line. And now I cannot. Strangely - the web app still connects just fine through a web browser.

Which brings me back to the heart of the original question. Why does my application successfully connect to the database through a web browser, but not through phpunit?

Any other helpful hints out there?

  • 写回答

2条回答 默认 最新

  • douzhiji2020 2013-10-24 16:31
    关注

    For using TCP on the local machine you have to use 127.0.0.1 as hostname and not localhost in new PDO("mysql:host=127.0.0.1;.....");

    评论

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案