You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
6.0 KiB
160 lines
6.0 KiB
<?php |
|
|
|
namespace RapidIll\Tests; |
|
|
|
use PHPUnit\Framework\TestCase; |
|
use RapidIll\InsertRequest; |
|
use RapidIll\InsertResponse; |
|
use RapidIll\RapidIllClient; |
|
use RapidIll\RapidIllException; |
|
use RapidIll\RequestType; |
|
|
|
class RapidIllClientTest extends TestCase |
|
{ |
|
public function testInsertRequestBuildsCorrectSoapArray(): void |
|
{ |
|
$request = (new InsertRequest()) |
|
->setRequestType(RequestType::Article) |
|
->setJournalTitle('Nature') |
|
->setJournalYear('2024') |
|
->setJournalVolume('625') |
|
->setJournalIssue('7994') |
|
->setArticleTitle('A breakthrough in quantum computing') |
|
->setArticleAuthor('Smith, J.') |
|
->setArticlePages('100-105') |
|
->addIssn('0028-0836') |
|
->setPatronName('Jane Doe') |
|
->setPatronEmail('jane@example.edu'); |
|
|
|
$data = $request->toSoapArray(); |
|
|
|
$this->assertSame('Article', $data['RapidRequestType']); |
|
$this->assertFalse($data['IsHoldingsCheckOnly']); |
|
$this->assertSame('Nature', $data['PatronJournalTitle']); |
|
$this->assertSame('2024', $data['PatronJournalYear']); |
|
$this->assertSame('625', $data['JournalVol']); |
|
$this->assertSame('7994', $data['JournalIssue']); |
|
$this->assertSame('A breakthrough in quantum computing', $data['ArticleTitle']); |
|
$this->assertSame('Smith, J.', $data['ArticleAuthor']); |
|
$this->assertSame('100-105', $data['ArticlePages']); |
|
$this->assertSame(['string' => ['0028-0836']], $data['SuggestedIssns']); |
|
$this->assertSame('Jane Doe', $data['PatronName']); |
|
$this->assertSame('jane@example.edu', $data['PatronEmail']); |
|
} |
|
|
|
public function testInsertRequestOmitsNullFields(): void |
|
{ |
|
$request = (new InsertRequest()) |
|
->setJournalTitle('Science'); |
|
|
|
$data = $request->toSoapArray(); |
|
|
|
$this->assertArrayHasKey('PatronJournalTitle', $data); |
|
$this->assertArrayNotHasKey('ArticleTitle', $data); |
|
$this->assertArrayNotHasKey('PatronEmail', $data); |
|
$this->assertArrayNotHasKey('SuggestedIssns', $data); |
|
} |
|
|
|
public function testInsertRequestMultipleIdentifiers(): void |
|
{ |
|
$request = (new InsertRequest()) |
|
->addIssn('0028-0836') |
|
->addIssn('1476-4687') |
|
->addIsbn('978-0-123456-78-9') |
|
->setOclcNumber('12345678'); |
|
|
|
$data = $request->toSoapArray(); |
|
|
|
$this->assertSame(['string' => ['0028-0836', '1476-4687']], $data['SuggestedIssns']); |
|
$this->assertSame(['string' => ['978-0-123456-78-9']], $data['SuggestedIsbns']); |
|
$this->assertSame('12345678', $data['OclcNumber']); |
|
} |
|
|
|
public function testInsertResponseFromSoapResponse(): void |
|
{ |
|
$soapResult = (object) [ |
|
'InsertRequestResult' => (object) [ |
|
'IsSuccessful' => true, |
|
'FoundMatch' => true, |
|
'RapidRequestId' => 99999, |
|
'NumberOfAvailableHoldings' => 3, |
|
'IsLocalHolding' => false, |
|
'VerificationNote' => null, |
|
'MatchingStandardNumber' => '0028-0836', |
|
'MatchingStandardNumberType' => 'ISSN', |
|
], |
|
]; |
|
|
|
$response = InsertResponse::fromSoapResponse($soapResult); |
|
|
|
$this->assertTrue($response->isSuccessful); |
|
$this->assertTrue($response->foundMatch); |
|
$this->assertSame(99999, $response->rapidRequestId); |
|
$this->assertSame(3, $response->numberOfAvailableHoldings); |
|
$this->assertFalse($response->isLocalHolding); |
|
$this->assertSame('0028-0836', $response->matchingStandardNumber); |
|
$this->assertSame('ISSN', $response->matchingStandardNumberType); |
|
} |
|
|
|
public function testInsertResponseWithLocalHoldings(): void |
|
{ |
|
$soapResult = (object) [ |
|
'InsertRequestResult' => (object) [ |
|
'IsSuccessful' => true, |
|
'FoundMatch' => true, |
|
'RapidRequestId' => 12345, |
|
'NumberOfAvailableHoldings' => 1, |
|
'IsLocalHolding' => true, |
|
'LocalHoldings' => (object) [ |
|
'LocalHoldingItem' => (object) [ |
|
'BranchName' => 'Main Library', |
|
'LibLocation' => 'Stacks', |
|
'CallNumber' => 'Q1 .N2', |
|
'RapidRedirectUrl' => 'https://example.com/redirect', |
|
], |
|
], |
|
], |
|
]; |
|
|
|
$response = InsertResponse::fromSoapResponse($soapResult); |
|
|
|
$this->assertTrue($response->isLocalHolding); |
|
$this->assertCount(1, $response->localHoldings); |
|
$this->assertSame('Main Library', $response->localHoldings[0]['branchName']); |
|
$this->assertSame('Q1 .N2', $response->localHoldings[0]['callNumber']); |
|
} |
|
|
|
public function testClientWrapsSOAPFaultInException(): void |
|
{ |
|
$mockSoap = $this->createMock(\SoapClient::class); |
|
$mockSoap->method('__call') |
|
->willThrowException(new \SoapFault('Server', 'Authentication failed')); |
|
|
|
$client = new RapidIllClient('user', 'pass', 'CODE', 'Main'); |
|
$client->setSoapClient($mockSoap); |
|
|
|
$this->expectException(RapidIllException::class); |
|
$this->expectExceptionMessage('RapidILL API error: Authentication failed'); |
|
|
|
$client->insertRequest(new InsertRequest()); |
|
} |
|
|
|
public function testBookRequest(): void |
|
{ |
|
$request = (new InsertRequest()) |
|
->setRequestType(RequestType::Book) |
|
->setJournalTitle('Introduction to Algorithms') |
|
->addIsbn('978-0262033848') |
|
->setArticleAuthor('Cormen, T.H.') |
|
->setPublisher('MIT Press') |
|
->setEdition('3rd') |
|
->setPatronName('John Smith'); |
|
|
|
$data = $request->toSoapArray(); |
|
|
|
$this->assertSame('Book', $data['RapidRequestType']); |
|
$this->assertSame('Introduction to Algorithms', $data['PatronJournalTitle']); |
|
$this->assertSame('MIT Press', $data['Publisher']); |
|
$this->assertSame('3rd', $data['Edition']); |
|
} |
|
}
|
|
|