File src/nasa/tests/NeoBundle/Service/ImportTest.php added (mode: 100644) (index 0000000..9ca8845) |
|
1 |
|
<?php |
|
2 |
|
|
|
3 |
|
namespace NeoBundle\Tests\Service; |
|
4 |
|
|
|
5 |
|
use PHPUnit\Framework\TestCase; |
|
6 |
|
|
|
7 |
|
use Doctrine\ORM\EntityManager; |
|
8 |
|
|
|
9 |
|
use Buzz\Browser; |
|
10 |
|
|
|
11 |
|
use NeoBundle\Repository\NeoRepository; |
|
12 |
|
use NeoBundle\Entity\Neo; |
|
13 |
|
use NeoBundle\Service\Import; |
|
14 |
|
use NeoBundle\Exception\NeoImportStructureInvalidException; |
|
15 |
|
|
|
16 |
|
class ImportTest extends TestCase |
|
17 |
|
{ |
|
18 |
|
public function testImportUrlGeneration() |
|
19 |
|
{ |
|
20 |
|
$entityManagerMock = $this->createMock(EntityManager::class); |
|
21 |
|
$browserMock = $this->createMock(Browser::class); |
|
22 |
|
$getMock = $this->createMock(MockClass::class); |
|
23 |
|
$neoRepositoryMock = $this->createMock(NeoRepository::class); |
|
24 |
|
$key = 'key'; |
|
25 |
|
$url = 'key=##key##&start=##start##&end=##end##'; |
|
26 |
|
$days = 3; |
|
27 |
|
|
|
28 |
|
$browserMock |
|
29 |
|
->expects($this->once()) |
|
30 |
|
->method('get') |
|
31 |
|
->with($this->equalTo('key=key&start=2017-08-26&end=2017-08-28')) |
|
32 |
|
->willReturn($getMock); |
|
33 |
|
$getMock->method('getContent')->willReturn('{ "near_earth_objects": [] }'); |
|
34 |
|
|
|
35 |
|
$import = new Import( |
|
36 |
|
$entityManagerMock, |
|
37 |
|
$browserMock, |
|
38 |
|
$neoRepositoryMock, |
|
39 |
|
$key, $url, $days |
|
40 |
|
); |
|
41 |
|
|
|
42 |
|
$import->import(); |
|
43 |
|
|
|
44 |
|
$this->assertTrue(true); |
|
45 |
|
} |
|
46 |
|
|
|
47 |
|
/** |
|
48 |
|
* @expectedException NeoBundle\Exception\NeoImportStructureInvalidException |
|
49 |
|
*/ |
|
50 |
|
public function testImportResponseException() |
|
51 |
|
{ |
|
52 |
|
$entityManagerMock = $this->createMock(EntityManager::class); |
|
53 |
|
$browserMock = $this->createMock(Browser::class); |
|
54 |
|
$getMock = $this->createMock(MockClass::class); |
|
55 |
|
$neoRepositoryMock = $this->createMock(NeoRepository::class); |
|
56 |
|
$key = 'key'; |
|
57 |
|
$url = 'key=##key##&start=##start##&end=##end##'; |
|
58 |
|
$days = 3; |
|
59 |
|
|
|
60 |
|
$browserMock |
|
61 |
|
->method('get') |
|
62 |
|
->willReturn($getMock); |
|
63 |
|
$getMock->method('getContent')->willReturn('{}'); |
|
64 |
|
|
|
65 |
|
$import = new Import( |
|
66 |
|
$entityManagerMock, |
|
67 |
|
$browserMock, |
|
68 |
|
$neoRepositoryMock, |
|
69 |
|
$key, $url, $days |
|
70 |
|
); |
|
71 |
|
|
|
72 |
|
$import->import(); |
|
73 |
|
} |
|
74 |
|
|
|
75 |
|
public function testImportPersistance() |
|
76 |
|
{ |
|
77 |
|
$entityManagerMock = $this->createMock(EntityManager::class); |
|
78 |
|
$browserMock = $this->createMock(Browser::class); |
|
79 |
|
$getMock = $this->createMock(MockClass::class); |
|
80 |
|
$neoRepositoryMock = $this->createMock(NeoRepository::class); |
|
81 |
|
$key = 'key'; |
|
82 |
|
$url = 'key=##key##&start=##start##&end=##end##'; |
|
83 |
|
$days = 3; |
|
84 |
|
|
|
85 |
|
$neo = new Neo(); |
|
86 |
|
$neo |
|
87 |
|
->setName('name') |
|
88 |
|
->setReference(123456) |
|
89 |
|
->setSpeed(123.456) |
|
90 |
|
->setIsHazardous(false) |
|
91 |
|
->setApproachAtAsString('2017-08-27'); |
|
92 |
|
|
|
93 |
|
$browserMock |
|
94 |
|
->method('get') |
|
95 |
|
->willReturn($getMock); |
|
96 |
|
$getMock |
|
97 |
|
->method('getContent') |
|
98 |
|
->willReturn('{ "near_earth_objects": { "2017-08-27": [ { "name": "name", "neo_reference_id": 123456, "is_potentially_hazardous_asteroid": false, "close_approach_data": [ { "close_approach_date": "2017-08-27", "relative_velocity": { "kilometers_per_hour": 123.456 } } ] } ] } }'); |
|
99 |
|
$neoRepositoryMock |
|
100 |
|
->expects($this->exactly(1)) |
|
101 |
|
->method('referenceExists') |
|
102 |
|
->with($this->equalTo(123456)) |
|
103 |
|
->willReturn(true); |
|
104 |
|
$entityManagerMock |
|
105 |
|
->expects($this->exactly(1)) |
|
106 |
|
->method('persist') |
|
107 |
|
->with($this->equalTo($neo)); |
|
108 |
|
$entityManagerMock |
|
109 |
|
->expects($this->exactly(1)) |
|
110 |
|
->method('flush'); |
|
111 |
|
|
|
112 |
|
$import = new Import( |
|
113 |
|
$entityManagerMock, |
|
114 |
|
$browserMock, |
|
115 |
|
$neoRepositoryMock, |
|
116 |
|
$key, $url, $days |
|
117 |
|
); |
|
118 |
|
|
|
119 |
|
$import->import(); |
|
120 |
|
|
|
121 |
|
$this->assertTrue(true); |
|
122 |
|
} |
|
123 |
|
} |
|
124 |
|
|
|
125 |
|
class MockClass { |
|
126 |
|
public function getContent() {} |
|
127 |
|
} |