|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -use Illuminate\Foundation\Testing\WithoutMiddleware; |
4 |
| -use Illuminate\Foundation\Testing\DatabaseMigrations; |
5 |
| -use Illuminate\Foundation\Testing\DatabaseTransactions; |
| 3 | +namespace Protechstudio\PrestashopWebService\Tests; |
| 4 | + |
6 | 5 | use Protechstudio\PrestashopWebService\PrestashopWebService;
|
| 6 | +use Protechstudio\PrestashopWebService\PrestashopWebServiceException; |
| 7 | +use Protechstudio\PrestashopWebService\PrestashopWebServiceLibrary; |
| 8 | +use Prestashop; |
7 | 9 |
|
8 | 10 | class PrestashopWebServiceTest extends TestCase
|
9 | 11 | {
|
10 |
| - |
11 |
| - /** |
12 |
| - * @test |
13 |
| - */ |
| 12 | + /** @test */ |
14 | 13 | public function it_is_correctly_installed()
|
15 | 14 | {
|
16 | 15 | $this->assertInstanceOf(PrestashopWebService::class, Prestashop::getFacadeRoot());
|
17 | 16 | }
|
| 17 | + |
| 18 | + /** @test */ |
| 19 | + public function it_can_perform_a_get_request() |
| 20 | + { |
| 21 | + $requestResponseStub = require('requests/category-schema.php'); |
| 22 | + $ps = $this->getMockedLibrary('executeCurl', $requestResponseStub); |
| 23 | + |
| 24 | + $xml = $ps->get(['resource' => 'categories']); |
| 25 | + |
| 26 | + $this->assertEquals('prestashop', $xml->getName()); |
| 27 | + $this->assertEquals('category', $xml->children()[0]->getName()); |
| 28 | + } |
| 29 | + |
| 30 | + /** @test */ |
| 31 | + public function it_throws_exception_on_404() |
| 32 | + { |
| 33 | + $ps = $this->getMockedLibrary('executeRequest', [ |
| 34 | + 'status_code' => 404, |
| 35 | + 'response' => '', |
| 36 | + 'header' => '' |
| 37 | + ]); |
| 38 | + |
| 39 | + $this->expectException(PrestashopWebServiceException::class); |
| 40 | + $xml = $ps->get(['resource' => 'categories']); |
| 41 | + } |
| 42 | + |
| 43 | + /** @test */ |
| 44 | + public function it_throws_exception_on_empty_response() |
| 45 | + { |
| 46 | + $ps = $this->getMockedLibrary('executeRequest', [ |
| 47 | + 'status_code' => 200, |
| 48 | + 'response' => '', |
| 49 | + 'header' => '' |
| 50 | + ]); |
| 51 | + |
| 52 | + $this->expectExceptionMessage('HTTP response is empty', PrestashopWebServiceException::class); |
| 53 | + $xml = $ps->get(['resource' => 'categories']); |
| 54 | + } |
| 55 | + |
| 56 | + /** @test */ |
| 57 | + public function it_throws_exception_on_malformed_xml() |
| 58 | + { |
| 59 | + $ps = $this->getMockedLibrary('executeRequest', [ |
| 60 | + 'status_code' => 200, |
| 61 | + 'response' => '<malformed>', |
| 62 | + 'header' => '' |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->expectExceptionMessage('HTTP XML response is not parsable', PrestashopWebServiceException::class); |
| 66 | + $xml = $ps->get(['resource' => 'categories']); |
| 67 | + } |
| 68 | + |
| 69 | + /** @test */ |
| 70 | + public function it_throws_exception_on_unsupported_version() |
| 71 | + { |
| 72 | + $this->expectExceptionMessage('This library is not compatible with this version of PrestaShop', PrestashopWebServiceException::class); |
| 73 | + Prestashop::isPrestashopVersionSupported('0.0.0.0'); |
| 74 | + Prestashop::isPrestashopVersionSupported('99.99.99.9999'); |
| 75 | + } |
| 76 | + |
| 77 | + /** @test */ |
| 78 | + public function it_throws_exception_on_unsupported_version_from_request() |
| 79 | + { |
| 80 | + $requestResponseStub = require('requests/category-schema.php'); |
| 81 | + $requestResponseStub[0] = preg_replace('/^PSWS-Version:(.+?)$/im', 'PSWS-Version: 99.99.99.9999', $requestResponseStub[0]); |
| 82 | + $ps = $this->getMockedLibrary('executeCurl', $requestResponseStub); |
| 83 | + |
| 84 | + $this->expectExceptionMessage('This library is not compatible with this version of PrestaShop', PrestashopWebServiceException::class); |
| 85 | + $xml = $ps->get(['resource' => 'categories']); |
| 86 | + } |
| 87 | + |
| 88 | + protected function getMockedLibrary($method = null, $returns = null) |
| 89 | + { |
| 90 | + $ps = $this->getMockBuilder(PrestashopWebServiceLibrary::class) |
| 91 | + ->setConstructorArgs([ |
| 92 | + env('prestashop-webservice.url'), |
| 93 | + env('prestashop-webservice.key'), |
| 94 | + env('prestashop-webservice.debug'), |
| 95 | + ]); |
| 96 | + |
| 97 | + if (!$method) { |
| 98 | + return $ps->getMock(); |
| 99 | + } else { |
| 100 | + $ps = $ps->setMethods([$method])->getMock(); |
| 101 | + |
| 102 | + $ps->expects($this->once()) |
| 103 | + ->method($method) |
| 104 | + ->willReturn($returns); |
| 105 | + return $ps; |
| 106 | + } |
| 107 | + } |
18 | 108 | }
|
| 109 | + |
0 commit comments