66use App \Models \Site ;
77use App \RemoteSite \Connection ;
88use App \RemoteSite \Responses \HealthCheck ;
9+ use Carbon \Carbon ;
910use Illuminate \Foundation \Testing \RefreshDatabase ;
1011use Illuminate \Support \Facades \Queue ;
1112use Tests \TestCase ;
@@ -74,6 +75,82 @@ public function testRegisteringASiteFailsWhenHealthCheckFails(): void
7475 $ response ->assertStatus (500 );
7576 }
7677
78+ public function testCheckingASiteReturnsSuccessIfCheckIsSuccessful (): void
79+ {
80+ $ site = $ this ->createMockSiteInDb ();
81+
82+ $ mock = $ this ->getMockBuilder (Connection::class)
83+ ->disableOriginalConstructor ()
84+ ->getMock ();
85+
86+ $ mock ->method ('__call ' )->willReturn (HealthCheck::from ($ site ->toArray ()));
87+
88+ $ this ->app ->bind (Connection::class, fn () => $ mock );
89+
90+ $ response = $ this ->postJson (
91+ '/api/v1/check ' ,
92+ ["url " => "https://www.joomla.org " , "key " => "foobar123foobar123foobar123foobar123 " ]
93+ );
94+
95+ $ response ->assertStatus (200 );
96+ }
97+
98+ public function testCheckingASiteReturnErrorIfCheckIsUnsuccessful (): void
99+ {
100+ $ this ->createMockSiteInDb ();
101+
102+ $ mock = $ this ->getMockBuilder (Connection::class)
103+ ->disableOriginalConstructor ()
104+ ->getMock ();
105+
106+ $ mock ->method ('__call ' )->willThrowException (new \Exception ());
107+
108+ $ this ->app ->bind (Connection::class, fn () => $ mock );
109+
110+ $ response = $ this ->postJson (
111+ '/api/v1/check ' ,
112+ ["url " => "https://www.joomla.org " , "key " => "foobar123foobar123foobar123foobar123 " ]
113+ );
114+
115+ $ response ->assertStatus (500 );
116+ }
117+
118+ public function testCheckingASiteReturns404ForInvalidSite (): void
119+ {
120+ $ response = $ this ->postJson (
121+ '/api/v1/check ' ,
122+ ["url " => "https://www.joomlaf.org " , "key " => "foobar123foobar123foobar123foobar123 " ]
123+ );
124+
125+ $ response ->assertStatus (404 );
126+ }
127+
128+ public function testDeleteASiteReturns404ForInvalidSite (): void
129+ {
130+ $ response = $ this ->postJson (
131+ '/api/v1/delete ' ,
132+ ["url " => "https://www.joomlaf.org " , "key " => "foobar123foobar123foobar123foobar123 " ]
133+ );
134+
135+ $ response ->assertStatus (404 );
136+ }
137+
138+ public function testDeleteASiteRemovesRow (): void
139+ {
140+ $ this ->createMockSiteInDb ();
141+
142+ $ this ->assertEquals (1 , Site::get ()->count ());
143+
144+ $ response = $ this ->postJson (
145+ '/api/v1/delete ' ,
146+ ["url " => "https://www.joomla.org " , "key " => "foobar123foobar123foobar123foobar123 " ]
147+ );
148+
149+ $ response ->assertStatus (200 );
150+
151+ $ this ->assertEquals (0 , Site::get ()->count ());
152+ }
153+
77154 protected function getConnectionMock (HealthCheck $ response )
78155 {
79156 $ mock = $ this ->getMockBuilder (Connection::class)
@@ -84,4 +161,24 @@ protected function getConnectionMock(HealthCheck $response)
84161
85162 return $ mock ;
86163 }
164+
165+ protected function createMockSiteInDb (): Site
166+ {
167+ $ site = new Site ([
168+ "php_version " => "1.0.0 " ,
169+ "db_type " => "mysqli " ,
170+ "db_version " => "1.0.0 " ,
171+ "cms_version " => "1.0.0 " ,
172+ "server_os " => "Joomla OS 1.0.0 " ,
173+ "last_seen " => Carbon::now ()
174+ ]);
175+
176+ $ site ->key = 'foobar123foobar123foobar123foobar123 ' ;
177+ $ site ->url = 'https://www.joomla.org ' ;
178+ $ site ->last_seen = Carbon::now ();
179+
180+ $ site ->save ();
181+
182+ return $ site ;
183+ }
87184}
0 commit comments