I had an enormous difficulty getting Mysql to work with PHP Unit. I kept getting errors with it not finding Mysql. The issue had to do with phpunit not being able to locate the mysql socket. I finally was able to solve the issue two different ways.
The first solution I was able to get working was by creating a sym link for the socket in the location that phpunit was looking. In short phpunit was looking for my mysql sock file in '/var/mysql/mysql.sock' and my mysql sock file is in the applications folder under mamp.
The solution is to create a symbolic link to the sock file in the mamp director
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
The other solution - which I believe is the better solution is to define a persistent connection that my code uses. My PHP db connection is mysqli_connect(). Normally I just have the minimum parameters , mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME), but to fix this issue you'll need to include two other parameters like so.
mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME, NULL, '/Applications/MAMP/tmp/mysql/mysql.sock');
As you can see the parameters now include the socket location for mysql sock in the MAMP directory. Now my phpunit tests work in conjunction with my local setup.