[[oktatas:web:back-end_framework:leaf|< Leaf]] ====== Leaf Teszt - GuzzleHttp Client ====== * **Szerző:** Sallai András * Copyright (c) 2024, Sallai András * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] * Web: https://szit.hu ===== Bevezetés ===== Adott egy kész projekt a következő végponttal: * employees A projekt létrehozása során a teszteléshez pest-t választottuk. A Guzzle webhelye: * https://docs.guzzlephp.org/ ===== Telepítés ===== composer require guzzlehttp/guzzle ===== CRUD teszt ===== 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. ===== Futtatás ===== * leaf test