diff --git a/src/Api.php b/src/Api.php index 43f95d7..a0beaef 100644 --- a/src/Api.php +++ b/src/Api.php @@ -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; } } diff --git a/src/Project.php b/src/Project.php index 7005ab7..3e36f48 100644 --- a/src/Project.php +++ b/src/Project.php @@ -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, + ]); + } +} diff --git a/src/Resource.php b/src/Resource.php index 0e7e9ce..bed27c2 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -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; + } } } } diff --git a/src/Support/Getting.php b/src/Support/Getting.php index 0d5c6fd..59b343e 100644 --- a/src/Support/Getting.php +++ b/src/Support/Getting.php @@ -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 { diff --git a/tests/CompanyTest.php b/tests/CompanyTest.php new file mode 100644 index 0000000..f21904c --- /dev/null +++ b/tests/CompanyTest.php @@ -0,0 +1,31 @@ +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); + } +} diff --git a/tests/Handlers/GetCompaniesByProjectHandler.php b/tests/Handlers/GetCompaniesByProjectHandler.php new file mode 100644 index 0000000..9931dcf --- /dev/null +++ b/tests/Handlers/GetCompaniesByProjectHandler.php @@ -0,0 +1,28 @@ +setBody($companies); + + return $handler->getResponse(); + } +} \ No newline at end of file diff --git a/tests/ProjectTest.php b/tests/ProjectTest.php index 4d977c7..60156e8 100644 --- a/tests/ProjectTest.php +++ b/tests/ProjectTest.php @@ -2,6 +2,7 @@ namespace TeamWorkPm\Tests; +use TeamWorkPm\Company; use TeamWorkPm\Project; class ProjectTest extends TestCase @@ -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); } } \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 146a6e8..3bf6fd5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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([ diff --git a/tests/schemas/get.companies.json b/tests/schemas/get.companies.json new file mode 100644 index 0000000..35dc396 --- /dev/null +++ b/tests/schemas/get.companies.json @@ -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" + } +] \ No newline at end of file