Skip to content

Commit

Permalink
add users api
Browse files Browse the repository at this point in the history
  • Loading branch information
Seretos committed Oct 17, 2018
1 parent 259e8de commit f173dba
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use seretos\testrail\api\Suites;
use seretos\testrail\api\Templates;
use seretos\testrail\api\Types;
use seretos\testrail\api\Users;
use seretos\testrail\connector\ApiConnectorInterface;
use seretos\testrail\connector\TestRailAPIClient;

Expand Down Expand Up @@ -96,6 +97,10 @@ public function statuses(){
return new Statuses($this->connector);
}

public function users(){
return new Users($this->connector);
}

public function api($name){
switch ($name){
case 'projects':
Expand Down Expand Up @@ -124,6 +129,8 @@ public function api($name){
return $this->results();
case 'statuses':
return $this->statuses();
case 'users':
return $this->users();
}
return null;
}
Expand Down
32 changes: 32 additions & 0 deletions src/api/Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: aappen
* Date: 17.10.18
* Time: 15:54
*/

namespace seretos\testrail\api;


class Users extends AbstractApi
{
private $cache = null;

public function all(){
if($this->cache === null) {
$this->cache = $this->connector->send_get('get_users');
}
return $this->cache;
}

public function find(string $name){
$users = $this->all();
foreach ($users as $user) {
if ($user['name'] === $name || $user['email'] === $name) {
return $user;
}
}
return [];
}
}
15 changes: 15 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use seretos\testrail\api\Suites;
use seretos\testrail\api\Templates;
use seretos\testrail\api\Types;
use seretos\testrail\api\Users;
use seretos\testrail\Client;
use seretos\testrail\connector\ApiConnectorInterface;

Expand Down Expand Up @@ -223,4 +224,18 @@ public function statuses(){
public function apiStatuses(){
$this->assertInstanceOf(Statuses::class,$this->client->api('statuses'));
}

/**
* @test
*/
public function users(){
$this->assertInstanceOf(Users::class,$this->client->users());
}

/**
* @test
*/
public function apiUsers(){
$this->assertInstanceOf(Users::class,$this->client->api('users'));
}
}
58 changes: 58 additions & 0 deletions tests/api/UsersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use PHPUnit\Framework\TestCase;
use seretos\testrail\api\Users;
use seretos\testrail\connector\ApiConnectorInterface;

/**
* Created by PhpStorm.
* User: aappen
* Date: 17.10.18
* Time: 15:57
*/

class UsersTest extends TestCase
{
/**
* @var Users
*/
private $users;

/**
* @var ApiConnectorInterface|PHPUnit_Framework_MockObject_MockObject
*/
private $mockApiConnector;

protected function setUp()
{
parent::setUp();
$this->mockApiConnector = $this->getMockBuilder(ApiConnectorInterface::class)->disableOriginalConstructor()->getMock();

$this->users = new Users($this->mockApiConnector);
}

/**
* @test
*/
public function all(){
$this->mockApiConnector->expects($this->once())
->method('send_get')
->with('get_users')
->will($this->returnValue([['id' => 1,'name' => 'user1'],['id' => 2,'name' => 'user2']]));

$this->assertSame([['id' => 1,'name' => 'user1'],['id' => 2,'name' => 'user2']],$this->users->all());
}

/**
* @test
*/
public function find(){
$this->mockApiConnector->expects($this->once())
->method('send_get')
->with('get_users')
->will($this->returnValue([['id' => 1,'name' => 'user1','email' => 'user1@web.de'],['id' => 2,'name' => 'user2','email' => 'user2@web.de']]));

$this->assertSame(['id' => 1,'name' => 'user1','email' => 'user1@web.de'],$this->users->find('user1'));
$this->assertSame(['id' => 2,'name' => 'user2','email' => 'user2@web.de'],$this->users->find('user2@web.de'));
}
}

0 comments on commit f173dba

Please sign in to comment.