130 lines
3.9 KiB
PHP
130 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\BusProNet;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\ApiClientException;
|
|
use App\BusProNet\ResponseParser;
|
|
use App\Entity\Embeddable\Address;
|
|
use App\Entity\Embeddable\Communication;
|
|
use App\Entity\Teamer;
|
|
use App\Entity\User;
|
|
use League\Flysystem\FilesystemOperator;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
/**
|
|
* Pins the request payloads. The serializer is the last step before the socket, so the payload is
|
|
* captured there and the request aborted - no connection is ever opened.
|
|
*/
|
|
class ApiClientTest extends TestCase
|
|
{
|
|
/**
|
|
* Without the password: "login" switches BusPro's password check off and the two ids identify
|
|
* the person. The element order is the one verified on the BusPro test system.
|
|
*/
|
|
public function testUpdateProfileIdentifiesThePersonByIdInsteadOfPassword(): void
|
|
{
|
|
$payload = $this->capturePayload(fn (ApiClient $client) => $client->updateProfile($this->user(addressId: 141747, personId: 224526)));
|
|
|
|
$this->assertSame('Adressdaten_Ändern', $payload['art']);
|
|
$this->assertSame('J', $payload['login']);
|
|
$this->assertSame(141747, $payload['idadresse']);
|
|
$this->assertSame(224526, $payload['idadresseperson']);
|
|
$this->assertArrayNotHasKey('passwort', $payload);
|
|
$this->assertSame(
|
|
['user', 'key', 'satz', 'art', 'email', 'login', 'idadresse', 'idadresseperson', 'adressdaten'],
|
|
array_keys($payload),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The ids are the whole authorisation of the write, so a user missing one is refused before
|
|
* anything is sent.
|
|
*/
|
|
public function testUpdateProfileRefusesAUserWithoutBothIds(): void
|
|
{
|
|
$serializer = $this->createMock(SerializerInterface::class);
|
|
$serializer->expects($this->never())->method('serialize');
|
|
|
|
$this->expectException(ApiClientException::class);
|
|
|
|
$this->client($serializer)->updateProfile($this->user(addressId: 141747, personId: null));
|
|
}
|
|
|
|
/**
|
|
* @param callable(ApiClient): mixed $request
|
|
*/
|
|
private function capturePayload(callable $request): array
|
|
{
|
|
$captured = null;
|
|
|
|
$serializer = $this->createMock(SerializerInterface::class);
|
|
$serializer
|
|
->expects($this->once())
|
|
->method('serialize')
|
|
->willReturnCallback(function (mixed $data) use (&$captured): never {
|
|
$captured = $data;
|
|
|
|
throw new \LogicException('Request captured');
|
|
})
|
|
;
|
|
|
|
try {
|
|
$request($this->client($serializer));
|
|
} catch (\LogicException) {
|
|
}
|
|
|
|
$this->assertIsArray($captured);
|
|
|
|
return $captured['anfrage'];
|
|
}
|
|
|
|
private function client(SerializerInterface $serializer): ApiClient
|
|
{
|
|
return new ApiClient(
|
|
$serializer,
|
|
$this->createStub(ResponseParser::class),
|
|
new NullLogger(),
|
|
$this->createStub(FilesystemOperator::class),
|
|
[
|
|
'bpn_username' => 'api',
|
|
'bpn_password' => 'secret',
|
|
'bpn_api_ip' => '127.0.0.1',
|
|
'bpn_api_port' => 1,
|
|
],
|
|
);
|
|
}
|
|
|
|
private function user(?int $addressId, ?int $personId): User
|
|
{
|
|
$teamer = new Teamer();
|
|
$teamer
|
|
->setFirstName('Anna')
|
|
->setLastName('Muster')
|
|
->setGender('F')
|
|
->setAddress(new Address())
|
|
->setCommunication(new Communication())
|
|
;
|
|
|
|
$user = new User();
|
|
$user
|
|
->setEmail('[email protected]')
|
|
->setTeamer($teamer)
|
|
;
|
|
|
|
if (null !== $addressId) {
|
|
$user->setBusProAddressId($addressId);
|
|
}
|
|
|
|
if (null !== $personId) {
|
|
$user->setBusProPersonId($personId);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
}
|