<?php
namespace app\tests;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
class UserTest extends TestCase
{
private $client;
public function setUp()
{
$this->client = new \GuzzleHttp\Client( [
'base_uri' => 'http://z.slim.com',
'http_errors' => false,
]);
}
public function testUser()
{
$response = $this->client->get('/user/vilay');
$this->assertEquals(200, $response->getStatusCode());
$body = $response->getBody();
$data = json_decode($body, true);
$this->assertArrayHasKey('code', $data);
$this->assertArrayHasKey('msg', $data);
$this->assertArrayHasKey('data', $data);
$this->assertEquals(0, $data['code']);
}
}
After writing the test code, execute
php vendor/bin/phpunit app/tests/UserTest.php
The test was not executed and the phpunit script was output directly
dir=$(d=${0%[/\]*}; cd "$d"; cd "../phpunit/phpunit" && pwd)
# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
# Cygwin paths start with /cygdrive/ which will break windows PHP,
# so we need to translate the dir path to windows format. However
# we could be using cygwin PHP which does not require this, so we
# test if the path to PHP starts with /cygdrive/ rather than /usr/bin
if [[ $(which php) == /cygdrive/* ]]; then
dir=$(cygpath -m "$dir");
fi
fi
dir=$(echo $dir | sed 's/ /\ /g')
"${dir}/phpunit" "$@"
In addition, I configured the unit test into the system environment variable and executed the test
phpunit app/tests/UserTest.php
Need to introduce autoload.php
at the head of the code
require_once 'vendor/autoload.php';
Can’t it be loaded automatically?
Solved, I found the solution myself after being prompted by the answers given by the two previous friends
Many of the tutorials found online are executed using commands
It may be due to version reasons. The earlier version was a php script file. My version is
"phpunit/phpunit": "^6.2"
,vendor/bin/phpunit
is a shell script file, (I have not used 5. I don’t know the specifics of x either).The correct way to use it is to give the script file executable permission
Execute tests
The automatic loading method is implemented. Using the phpunit component package loaded by composer, there is a phpunit.xml in the root directory of the project, where you can set the automatic loading path
You can write a unit test file outside the framework and use phpunit xxx.php to test it. This way you will know whether phpunit is installed normally and whether the way to introduce autoload.php is wrong. Check them one by one.