3
votes

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

2 Answers

4
votes

Does phpunit use a different php.ini file than the one configured in MAMP?

Yes, the command line version of PHP usually has it's own version of php.ini. Run php -i to get the phpinfo and have a look at which INI is used there.

1
votes

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;.....");