Skip to content

Commit 07f5212

Browse files
33378 GraphQL: move logger test to GenerateCustomerTokenTest.php; clean code
1 parent 7e00080 commit 07f5212

File tree

3 files changed

+68
-102
lines changed

3 files changed

+68
-102
lines changed

app/code/Magento/CustomerGraphQl/etc/graphql/events.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
910
<event name="customer_login">
1011
<observer name="customer_log_login" instance="Magento\Customer\Observer\LogLastLoginAtObserver" />
1112
</event>

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/GenerateCustomerTokenTest.php

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,41 @@
77

88
namespace Magento\GraphQl\Customer;
99

10+
use Magento\Customer\Model\Log;
11+
use Magento\Customer\Model\Logger;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
1015
use Magento\TestFramework\TestCase\GraphQlAbstract;
1116

1217
/**
1318
* API-functional tests cases for generateCustomerToken mutation
1419
*/
1520
class GenerateCustomerTokenTest extends GraphQlAbstract
1621
{
22+
23+
/**
24+
* @var Logger
25+
*/
26+
private $logger;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
$this->logger = Bootstrap::getObjectManager()->get(Logger::class);
35+
}
36+
1737
/**
1838
* Verify customer token with valid credentials
1939
*
2040
* @magentoApiDataFixture Magento/Customer/_files/customer.php
2141
*/
22-
public function testGenerateCustomerValidToken()
42+
public function testGenerateCustomerValidToken(): void
2343
{
24-
$email = 'customer@example.com';
25-
$password = 'password';
26-
27-
$mutation = $this->getQuery($email, $password);
44+
$mutation = $this->getQuery();
2845

2946
$response = $this->graphQlMutation($mutation);
3047
$this->assertArrayHasKey('generateCustomerToken', $response);
@@ -41,7 +58,7 @@ public function testGenerateCustomerValidToken()
4158
* @param string $password
4259
* @param string $message
4360
*/
44-
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message)
61+
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message): void
4562
{
4663
$this->expectException(\Exception::class);
4764

@@ -55,12 +72,9 @@ public function testGenerateCustomerTokenInvalidData(string $email, string $pass
5572
*
5673
* @magentoApiDataFixture Magento/Customer/_files/customer.php
5774
*/
58-
public function testRegenerateCustomerToken()
75+
public function testRegenerateCustomerToken(): void
5976
{
60-
$email = 'customer@example.com';
61-
$password = 'password';
62-
63-
$mutation = $this->getQuery($email, $password);
77+
$mutation = $this->getQuery();
6478

6579
$response1 = $this->graphQlMutation($mutation);
6680
$token1 = $response1['generateCustomerToken']['token'];
@@ -110,7 +124,7 @@ public function dataProviderInvalidCustomerInfo(): array
110124
* @param string $password
111125
* @return string
112126
*/
113-
private function getQuery(string $email, string $password) : string
127+
private function getQuery(string $email = 'customer@example.com', string $password = 'password'): string
114128
{
115129
return <<<MUTATION
116130
mutation {
@@ -127,22 +141,12 @@ private function getQuery(string $email, string $password) : string
127141
/**
128142
* Verify customer with empty email
129143
*/
130-
public function testGenerateCustomerTokenWithEmptyEmail()
144+
public function testGenerateCustomerTokenWithEmptyEmail(): void
131145
{
132146
$email = '';
133147
$password = 'bad-password';
134148

135-
$mutation
136-
= <<<MUTATION
137-
mutation {
138-
generateCustomerToken(
139-
email: "{$email}"
140-
password: "{$password}"
141-
) {
142-
token
143-
}
144-
}
145-
MUTATION;
149+
$mutation = $this->getQuery($email, $password);
146150

147151
$this->expectException(\Exception::class);
148152
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "email" value.');
@@ -152,25 +156,51 @@ public function testGenerateCustomerTokenWithEmptyEmail()
152156
/**
153157
* Verify customer with empty password
154158
*/
155-
public function testGenerateCustomerTokenWithEmptyPassword()
159+
public function testGenerateCustomerTokenWithEmptyPassword(): void
156160
{
157161
$email = 'customer@example.com';
158162
$password = '';
159163

160-
$mutation
161-
= <<<MUTATION
162-
mutation {
163-
generateCustomerToken(
164-
email: "{$email}"
165-
password: "{$password}"
166-
) {
167-
token
168-
}
169-
}
170-
MUTATION;
164+
$mutation = $this->getQuery($email, $password);
171165

172166
$this->expectException(\Exception::class);
173167
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "password" value.');
174168
$this->graphQlMutation($mutation);
175169
}
170+
171+
/**
172+
* Verify customer log after generate customer token
173+
*
174+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
175+
*/
176+
public function testCustomerLogAfterGenerateCustomerToken(): void
177+
{
178+
$response = $this->graphQlMutation($this->getQuery());
179+
$this->assertArrayHasKey('generateCustomerToken', $response);
180+
$this->assertIsArray($response['generateCustomerToken']);
181+
182+
/** @var Log $log */
183+
$log = $this->logger->get(1);
184+
$this->assertNotEmpty($log->getLastLoginAt());
185+
}
186+
187+
/**
188+
* Ensure that customer log record is deleted.
189+
*
190+
* @return void
191+
*/
192+
protected function tearDown(): void
193+
{
194+
if ($this->logger->get(1)->getLastLoginAt()) {
195+
/** @var AdapterInterface $connection */
196+
/** @var ResourceConnection $resource */
197+
$resource = Bootstrap::getObjectManager()->get(ResourceConnection::class);
198+
$connection = $resource->getConnection(ResourceConnection::DEFAULT_CONNECTION);
199+
$connection->delete(
200+
$resource->getTableName('customer_log'),
201+
['customer_id' => 1]
202+
);
203+
}
204+
parent::tearDown();
205+
}
176206
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/LoggerTest.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)