Adott egy kész projekt a következő végponttal:
A projekt létrehozása során a teszteléshez pest-t választottuk.
A Guzzle webhelye:
composer require guzzlehttp/guzzle
<?php use GuzzleHttp\Client; use PHPUnit\Framework\TestCase; class MyTest extends TestCase { protected $client; protected $url; public static function setUpBeforeClass(): void { $db = new Leaf\Db(); $db->connect([ 'dbtype' => 'sqlite', 'dbname' => 'testdb.sqlite', ]); $db->query('drop table if exists employees')->execute(); $db->query('create table if not exists ' . 'employees(id integer not null primary key'. ' autoincrement, name text, city text, salary real,'. ' created_at datetime, updated_at datetime)')->execute(); } public function setUp(): void { parent::setUp(); $this->url = 'http://localhost:5500/employees'; $this->client = new Client(); } public function tearDown():void { parent::tearDown(); } public static function tearDownAfterClass(): void { } public function testGetEmployees() { $res = $this->client->get($this->url); $http_status = $res->getStatusCode(); expect($http_status)->toBe(200); } public function testPostEmployee() { $post_data = [ 'name' => 'Valaki', 'city' => 'Valahol', 'salary' => 500 ]; $res = $this->client->request('POST', $this->url, ['json' => $post_data]); $http_status = $res->getStatusCode(); expect($http_status)->toBe(200); } public function testPutEmployee() { $puturl = $this->url . '/1'; $put_body = [ 'json' => [ 'name' => 'Masvalaki', 'city' => 'Mashol', 'salary' => 352 ] ]; $res = $this->client->put($puturl, $put_body); $http_status = $res->getStatusCode(); expect($http_status)->toBe(200); } function testDelteEmployee() { $deleteurl = 'http://localhost:5500/employees/1'; $res = $this->client->delete($deleteurl); $http_status = $res->getStatusCode(); expect($http_status)->toBe(200); } }
Automatikusan létrehozz a testdb.sqlite fájlt és a tesztelés kezdetekor törli az employees táblát.