You are here

public function DeveloperTest::developerGetCompanyListTest in Apigee Edge 8

Tests getCompanies() and hasCompany() methods on the developer entity.

See also

\Drupal\apigee_edge\Entity\Developer::getCompanies()

\Drupal\apigee_edge\Entity\Developer::hasCompany()

1 call to DeveloperTest::developerGetCompanyListTest()
DeveloperTest::testDeveloperRegisterAndCreate in tests/src/Functional/DeveloperTest.php
Tests developer registration and create by admin.

File

tests/src/Functional/DeveloperTest.php, line 428

Class

DeveloperTest
Create, delete, update developer entity tests.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

public function developerGetCompanyListTest() {

  // Add matched organization response so it returns the org whenever called.
  $this
    ->addOrganizationMatchedResponse();

  // Create a new developer.
  $name = strtolower($this
    ->randomMachineName());
  $this->developer = $this->developerStorage
    ->create([
    'email' => $name . '@example.com',
    'userName' => $name,
    'firstName' => $this
      ->getRandomGenerator()
      ->word(8),
    'lastName' => $this
      ->getRandomGenerator()
      ->word(8),
  ]);

  // Stack a created developer response, and an empty response
  // mocking the status change to active.
  $account = $this
    ->queueDeveloperResponseFromDeveloper($this->developer, 201);
  $this->stack
    ->queueMockResponse('no_content');
  $this->developer
    ->save();

  // Result of getCompanies() function should be an empty array.
  // - Queue a developer response, as method getCompanies() loads the dev if
  // the companies are null initially.
  $this
    ->queueDeveloperResponse($account);
  $this->developer
    ->getCompanies();
  $this
    ->assertNotNull($this->developer
    ->getCompanies());
  $this
    ->assertEmpty($this->developer
    ->getCompanies());

  // Create a new company and add developer as a member to it.
  $this->company = new Company([
    'name' => $this
      ->getRandomGenerator()
      ->name(),
    'displayName' => $this
      ->getRandomGenerator()
      ->name(),
  ]);
  $company_controller = new CompanyController($this->sdkConnector
    ->getOrganization(), $this->sdkConnector
    ->getClient());

  // Queue a created company response.
  $this
    ->queueCompanyResponse($this->company, 201);
  $company_controller
    ->create($this->company);

  // Queue a developers in a company response.
  $this
    ->queueDevsInCompanyResponse([
    [
      'email' => $this->developer
        ->getEmail(),
      'role' => '',
    ],
  ]);
  $company_membership_controller = new CompanyMembersController($this->company
    ->getName(), $this->sdkConnector
    ->getOrganization(), $this->sdkConnector
    ->getClient());
  $company_membership = new CompanyMembership([
    $this->developer
      ->getEmail() => NULL,
  ]);
  $company_membership_controller
    ->setMembers($company_membership);

  // Ensure that the developer is reloaded from Apigee Edge so remove the
  // developer entity from the cache.
  $this->developerStorage
    ->resetCache([
    $this->developer
      ->id(),
  ]);

  // resetCache() does not clear controller's cache by default.
  // @see \Drupal\apigee_edge\Entity\Storage\EdgeEntityStorageBase::resetCache()
  $developer_cache = $this->container
    ->get('apigee_edge.controller.cache.developer');
  $developer_cache
    ->removeEntities([
    $this->developer
      ->id(),
  ]);

  // Check the companies array if the developer is reloaded.

  /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
  $this->stack
    ->queueMockResponse([
    'developers' => [
      'developers' => [
        $account,
      ],
      'expand' => TRUE,
    ],
  ]);
  $this
    ->queueDeveloperResponse($account, NULL, [
    'companies' => [
      $this->company
        ->getName(),
    ],
  ]);
  $developer = $this->developerStorage
    ->loadMultiple()[$this->developer
    ->id()];
  $this
    ->assertContains($this->company
    ->getName(), $developer
    ->getCompanies());
  self::assertTrue($developer
    ->hasCompany($this->company
    ->getName()));

  // Check the companies array if the developer is removed from the member
  // list.
  // - Add an empty response simulating when a member is deleted.
  $this->stack
    ->queueMockResponse('no_content');
  $company_membership_controller
    ->removeMember($this->developer
    ->getEmail());
  $developer_cache
    ->removeEntities([
    $this->developer
      ->id(),
  ]);
  $this
    ->queueDeveloperResponse($account);
  $developer = $this->developerStorage
    ->loadUnchanged($this->developer
    ->id());
  $this
    ->queueDeveloperResponse($account);
  $this
    ->assertNotContains($this->company
    ->getName(), $developer
    ->getCompanies());
  self::assertFalse($developer
    ->hasCompany($this->company
    ->getName()));
}