From e03795e36a82ecb1b5871d59663270437bd834e2 Mon Sep 17 00:00:00 2001 From: Iain Date: Thu, 29 Feb 2024 00:27:43 +0100 Subject: [PATCH] Fix behat tests --- tests/Behat/GeneralContext.php | 19 ++- tests/Behat/PlanContext.php | 28 ++-- tests/Behat/SendRequestTrait.php | 25 +++- tests/Behat/TeamContext.php | 31 +++- tests/Behat/TeamTrait.php | 14 +- tests/Behat/UserContext.php | 246 +++++++++++++++++-------------- tests/Behat/UserTrait.php | 33 +++++ 7 files changed, 258 insertions(+), 138 deletions(-) create mode 100644 tests/Behat/UserTrait.php diff --git a/tests/Behat/GeneralContext.php b/tests/Behat/GeneralContext.php index b417813..5154b7d 100644 --- a/tests/Behat/GeneralContext.php +++ b/tests/Behat/GeneralContext.php @@ -1,8 +1,20 @@ entityManager; $metaData = $em->getMetadataFactory()->getAllMetadata(); @@ -36,4 +49,4 @@ public function startUp(BeforeScenarioScope $event) } $this->session->start(); } -} \ No newline at end of file +} diff --git a/tests/Behat/PlanContext.php b/tests/Behat/PlanContext.php index 56ec484..166fe75 100644 --- a/tests/Behat/PlanContext.php +++ b/tests/Behat/PlanContext.php @@ -1,11 +1,22 @@ sendJsonRequest('GET', '/api/plans'); + $this->sendJsonRequest('GET', '/api/billing/plans'); } /** @@ -32,16 +43,15 @@ public function iShouldSeeThePlansThatAreConfigured() $plans = $this->planManager->getPlans(); - foreach ($plans as $plan) - { + foreach ($plans as $plan) { $name = $plan->getName(); if (!isset($content['plans'][$name])) { - throw new \Exception("Can't see plan ". $name); + throw new \Exception("Can't see plan ".$name); } if ($content['plans'][$name]['limits'] != $plan->getLimits()) { - throw new \Exception("Plan for ". $plan->getLimits()); + throw new \Exception('Plan for '.$plan->getLimits()); } } } -} \ No newline at end of file +} diff --git a/tests/Behat/SendRequestTrait.php b/tests/Behat/SendRequestTrait.php index 3e29a4f..5a6a330 100644 --- a/tests/Behat/SendRequestTrait.php +++ b/tests/Behat/SendRequestTrait.php @@ -1,9 +1,18 @@ session->getPage()->getContent(); $json = json_decode($content, true); + if (!$json) { - throw new \Exception("No valid JSON found"); + throw new \Exception('No valid JSON found'); } return $json; } -} \ No newline at end of file +} diff --git a/tests/Behat/TeamContext.php b/tests/Behat/TeamContext.php index bf650c1..0660088 100644 --- a/tests/Behat/TeamContext.php +++ b/tests/Behat/TeamContext.php @@ -1,8 +1,19 @@ session->getPage()->getContent(), true); - foreach ($jsonData['sent_invites'] as $invite) { if ($invite['email'] == $email) { return; @@ -111,4 +120,16 @@ public function theInviteForShouldntBeUsable($email) throw new \Exception('Invite is usable'); } } -} \ No newline at end of file + + /** + * @Then /^I should be told that the email is already a member$/ + */ + public function iShouldBeToldThatTheEmailIsAlreadyAMember() + { + $jsonData = json_decode($this->session->getPage()->getContent(), true); + + if (!$jsonData['already_a_member']) { + throw new \Exception('Not declared as already a member'); + } + } +} diff --git a/tests/Behat/TeamTrait.php b/tests/Behat/TeamTrait.php index feacb0c..e287d6e 100644 --- a/tests/Behat/TeamTrait.php +++ b/tests/Behat/TeamTrait.php @@ -1,6 +1,18 @@ createUser($username, $password, $confirmationCode, false); } - protected function createUser($username, $password, $confirmationCode, $confirmed, $name = 'A test user', $isAdmin = false, $bulk = false) - { - $user = new User(); - if (!$bulk) { - $encodedPassword = $this->hasherFactory->getPasswordHasher($user)->hash($password); - } else { - $encodedPassword = $password; - } - $user->setEmail($username); - $user->setPassword($encodedPassword); - $user->setName($name); - $user->setConfirmationCode($confirmationCode); - $user->setIsConfirmed($confirmed); - $user->setCreatedAt(new \DateTime('now')); - $user->setRoles($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER']); - - $this->repository->getEntityManager()->persist($user); - if (!$bulk) { - $this->repository->getEntityManager()->flush(); - } - - return $user; - } - /** * @Then I will be logged in */ @@ -314,19 +304,19 @@ public function iHaveLoggedAs($arg1) } /** - * @When I edit my profile with the name :arg1 + * @When I edit my settings with the name :arg1 */ - public function iEditMyProfileWithTheName($arg1) + public function iEditMySettingsWithTheName($arg1) { - $this->sendJsonRequest('GET', '/api/user/profile'); + $this->sendJsonRequest('GET', '/api/user/settings'); $content = $this->getJsonContent()['form']; $output = []; foreach ($content as $key => $options) { - $output[$key] = $options['value']; + $output[$key] = $options; } $output['name'] = $arg1; - $this->sendJsonRequest('POST', '/api/user/profile', $output); + $this->sendJsonRequest('POST', '/api/user/settings', $output); } /** @@ -349,11 +339,11 @@ public function iAmNotLoggedIn() } /** - * @When I visit the profiile page + * @When I visit the settings page */ - public function iVisitTheProfiilePage() + public function iVisitTheSettingsPage() { - $this->session->visit('/api/user/profile'); + $this->session->visit('/api/user/settings'); } /** @@ -366,22 +356,6 @@ public function iWillBeOnTheLoginPage() } } - /** - * @param $email - * - * @return User|null - * - * @throws \Doctrine\ORM\ORMException - */ - protected function fetchUser($email) - { - /** @var User $user */ - $user = $this->repository->findOneBy(['email' => $email]); - $this->repository->getEntityManager()->refresh($user); - - return $user; - } - /** * @When I change my password to :arg1 giving my current password as :arg2 */ @@ -433,7 +407,9 @@ public function thatTheFollowingUsersExist(TableNode $table) $users = $table->getColumnsHash(); foreach ($users as $user) { - $this->createUser($user['Email'], $user['Password'], 'ddd', true, $user['Name'], false, true); + $userEnt = $this->createUser($user['Email'], $user['Password'], 'ddd', true, $user['Name'], false, true); + + $this->createTeam($userEnt, $user['Name']); } $this->repository->getEntityManager()->flush(); @@ -444,7 +420,8 @@ public function thatTheFollowingUsersExist(TableNode $table) */ public function anAdminUserWithThePasswordExist($email, $password) { - $this->createUser($email, $password, 'fake', true, 'A system user', true); + $user = $this->createUser($email, $password, 'fake', true, 'A system user', true); + $this->createTeam($user, 'admin'); } /** @@ -475,6 +452,19 @@ public function iClickNext() } } + /** + * @When I click back + */ + public function iClickBack() + { + try { + $this->session->getPage()->clickLink('crud_list_back'); + } catch (\Throwable $e) { + echo $this->session->getPage()->getContent(); + throw $e; + } + } + /** * @Then I should see :arg1 items */ @@ -494,6 +484,7 @@ public function iShouldSee($arg1) { if (!$this->session->getPage()->hasContent($arg1)) { echo $this->session->getPage()->getContent(); + echo $this->session->getCurrentUrl(); throw new \Exception("Can't see ".$arg1); } } @@ -582,7 +573,7 @@ public function iEditTheUserWithTheData($arg1, TableNode $table) $this->session->visit('/athena/user/'.$user->getId().'/edit'); foreach ($table->getRowsHash() as $fieldName => $value) { - $this->session->getPage()->fillField('form['.$fieldName.']', $value); + $this->session->getPage()->fillField('athena['.$fieldName.']', $value); } $this->session->getPage()->pressButton('crud_edit_submit'); } @@ -618,6 +609,7 @@ public function thereWillBeAnInviteCodeFor($email) throw new \Exception('No invite code found'); } } + /** * @Then there will not be an invite code for :arg1 */ @@ -626,7 +618,7 @@ public function thereWillNotBeAnInviteCodeFor($email) $inviteCode = $this->inviteCodeRepository->findOneBy(['email' => $email]); if ($inviteCode) { - throw new \Exception('invite code found'); + throw new \Exception('Invite code found'); } } @@ -643,32 +635,6 @@ public function theInviteCode($code) $this->inviteCodeRepository->getEntityManager()->flush(); } - - /** - * @When I edit my settings with the name :arg1 - */ - public function iEditMySettingsWithTheName($arg1) - { - $this->sendJsonRequest('GET', '/api/user/settings'); - $content = $this->getJsonContent()['form']; - $output = []; - foreach ($content as $key => $options) { - $output[$key] = $options; - } - - $output['name'] = $arg1; - $this->sendJsonRequest('POST', '/api/user/settings', $output); - } - - - /** - * @When I visit the settings page - */ - public function iVisitTheSettingsPage() - { - $this->session->visit('/api/user/settings'); - } - /** * @When I try to sign up with the code :arg1 */ @@ -755,31 +721,6 @@ public function theFollowingTeamsExist(TableNode $table) } } - protected function createTeam(?User $user, $name, $plan = 'Trial') - { - $team = new Team(); - $team->setName($name); - if ($user) { - $team->addMember($user); - } - $team->setCreatedAt(new \DateTime('now')); - $team->setSubscription(new Subscription()); - $team->getSubscription()->setPlanName($plan); - $team->getSubscription()->setValidUntil(new \DateTime('+7 days')); - $team->getSubscription()->setActive(true); - - $this->teamRepository->getEntityManager()->persist($team); - - $this->teamRepository->getEntityManager()->flush(); - if ($user) { - $user->setTeam($team); - $this->teamRepository->getEntityManager()->persist($user); - } - - $this->teamRepository->getEntityManager()->flush(); - } - - /** * @When I sent an invite to :arg1 */ @@ -805,4 +746,83 @@ public function iShouldNotSeeAsAnInvitedUser($email) } } } + + protected function createUser($username, $password, $confirmationCode, $confirmed, $name = 'A test user', $isAdmin = false, $bulk = false) + { + $user = new User(); + if (!$bulk) { + $encodedPassword = $this->hasherFactory->getPasswordHasher($user)->hash($password); + } else { + $encodedPassword = $password; + } + $user->setEmail($username); + $user->setPassword($encodedPassword); + $user->setName($name); + $user->setConfirmationCode($confirmationCode); + $user->setIsConfirmed($confirmed); + $user->setCreatedAt(new \DateTime('now')); + $user->setRoles($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER']); + + $this->repository->getEntityManager()->persist($user); + if (!$bulk) { + $this->repository->getEntityManager()->flush(); + } + + return $user; + } + + /** + * @return User|null + * + * @throws \Doctrine\ORM\ORMException + */ + protected function fetchUser($email) + { + /** @var User $user */ + $user = $this->repository->findOneBy(['email' => $email]); + $this->repository->getEntityManager()->refresh($user); + + return $user; + } + + protected function createTeam(?User $user, $name, $plan = 'Trial') + { + $team = new Team(); + $team->setName($name); + if ($user) { + $team->addMember($user); + } + $team->setCreatedAt(new \DateTime('now')); + $team->setSubscription(new EmbeddedSubscription()); + $team->getSubscription()->setPlanName($plan); + $team->getSubscription()->setValidUntil(new \DateTime('+7 days')); + $team->getSubscription()->setActive(true); + + $externalReference = bin2hex(random_bytes(40)); + $team->setExternalCustomerReference($externalReference); + + $this->teamRepository->getEntityManager()->persist($team); + + $this->teamRepository->getEntityManager()->flush(); + if ($user) { + $user->setTeam($team); + $this->teamRepository->getEntityManager()->persist($user); + } + + $this->teamRepository->getEntityManager()->flush(); + + $subscription = new Subscription(); + $subscription->setPlanName($plan); + $subscription->setCustomer($team); + $subscription->setActive(true); + $subscription->setStatus(SubscriptionStatus::ACTIVE); + $subscription->setAmount(100); + $subscription->setCurrency('EUR'); + $subscription->setPaymentSchedule('year'); + $subscription->setCreatedAt(new \DateTime()); + $subscription->setUpdatedAt(new \DateTime()); + + $this->teamRepository->getEntityManager()->persist($subscription); + $this->teamRepository->getEntityManager()->flush(); + } } diff --git a/tests/Behat/UserTrait.php b/tests/Behat/UserTrait.php new file mode 100644 index 0000000..1700dc3 --- /dev/null +++ b/tests/Behat/UserTrait.php @@ -0,0 +1,33 @@ +userRepository->findOneBy(['email' => $email]); + + if (!$user instanceof User) { + throw new \Exception('No user found'); + } + + $this->userRepository->getEntityManager()->refresh($user); + + return $user; + } +}