Skip to content

Commit

Permalink
feat: company test and project filter by company.
Browse files Browse the repository at this point in the history
  • Loading branch information
loduis committed Dec 3, 2023
1 parent 6c7b656 commit b9151b7
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public static function auth(string $company, string $api): void
*/
public static function clientOptions(array $options): void
{
static::$clientOptions = $options;
self::$clientOptions = $options;
}
}
15 changes: 14 additions & 1 deletion src/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@

namespace TeamWorkPm;

final class Project extends Resource {}
final class Project extends Resource
{
/**
* All of the companies within the specified project are returned
*
* @return Company[]
*/
public function companies()
{
return static::instanceGetRequest($this->id . '/companies', [
'instance_of' => Company::class,
]);
}
}
6 changes: 5 additions & 1 deletion src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public function __get($key)
$key = $key2;
} else {
$key2 = Str::snake($key);
$key2 = static::camelLastUpper($key2);
if ($this->hasAttribute($key2)) {
$key = $key2;
} else {
$key2 = static::camelLastUpper($key2);
if ($this->hasAttribute($key2)) {
$key = $key2;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Getting.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use GuzzleHttp\Psr7\Utils;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;

trait Getting
{
Expand Down
31 changes: 31 additions & 0 deletions tests/CompanyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace TeamWorkPm\Tests;

use TeamWorkPm\Company;

class CompanyTest extends TestCase
{
public function testResolvePath(): void
{
$this->assertEquals('companies', Company::resolvePath());
}

public function testAll(): void
{
$companies = Company::all();
$this->assertCount(1, $companies);
}

public function testGet(): void
{
$company = Company::get(1370007);
$this->assertEquals(1370007, $company->id);
$this->assertEquals("Php's Company", $company->name);
$this->assertEquals('1370007-php-s-company', $company->companyNameUrl);
$this->assertEquals('1370007-php-s-company', $company['company_name_url']);
$this->assertTrue($company->canSeePrivate);
$this->assertTrue($company['can_see_private']);
$this->assertEquals('2023-11-29T11:54:33Z', $company->createdOn);
}
}
28 changes: 28 additions & 0 deletions tests/Handlers/GetCompaniesByProjectHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace TeamWorkPm\Tests\Handlers;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Api\Testing\Handlers\ResponseHandler;

class GetCompaniesByProjectHandler
{
public static function requestHandle(RequestInterface $request, array &$options)
{
return 200;
}

public static function responseHandle(ResponseInterface $response)
{
$handler = new ResponseHandler($response);

$companies = file_get_contents(__DIR__ . '/../schemas/get.companies.json');

$companies = json_decode($companies);

$handler->setBody($companies);

return $handler->getResponse();
}
}
12 changes: 12 additions & 0 deletions tests/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TeamWorkPm\Tests;

use TeamWorkPm\Company;
use TeamWorkPm\Project;

class ProjectTest extends TestCase
Expand All @@ -23,5 +24,16 @@ public function testOne(): void
$this->assertEquals(true, $project['is_billable']);
$this->assertEquals('Hi', $project->announcementHtml);
$this->assertEquals('Hi', $project['announcement_html']);

$companies = $project->companies();

$this->assertCount(1, $companies);

// TODO Need an Collection of Company
$company = $companies->first();

$this->assertInstanceOf(Company::class, $company);

$this->assertEquals(1370007, $company->id);
}
}
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ protected function createHandler(string $mode): void

if ($mode !== 'live') {
if (!$handler) {
$handler = ApiHandler::create(__DIR__ . '/schemas');
$handler = ApiHandler::create(__DIR__ . '/schemas')
->request('GET /projects/967518/companies.json', Handlers\GetCompaniesByProjectHandler::class);
;
}
$stack = HandlerStack::create($handler);
Api::clientOptions([
Expand Down
33 changes: 33 additions & 0 deletions tests/schemas/get.companies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"zip": "",
"contacts": "0",
"last-changed-on": "",
"phone": "",
"state": "",
"created-on": "2023-11-29T11:54:33Z",
"industryId": "",
"email_one": "",
"company_name_url": "1370007-php-s-company",
"address_two": "",
"logo-URL": "",
"country": "",
"address_one": "",
"id": "1370007",
"can_see_private": true,
"cid": "",
"isowner": "1",
"industry": "",
"accounts": "1",
"email_two": "",
"name": "Php's Company",
"email_three": "",
"city": "",
"countrycode": "",
"fax": "",
"website": "",
"tags": [],
"clients": "0",
"collaborators": "0"
}
]

0 comments on commit b9151b7

Please sign in to comment.