Skip to content

Commit

Permalink
Add tests for categories webhook controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
vedanshujain committed Feb 4, 2025
1 parent 29d8282 commit 7322c38
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Models;

class Category {
public string $id;
public int $id;
public string $name;
public string $slug;
public string $description;
Expand Down
12 changes: 6 additions & 6 deletions app/src/Webhooks/CategoryWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public function handle_request(): Controller {
switch ( $params['action'] ?? '' ) {
case 'updated':
case 'added':
$this->update_category( strval( $params['category_id'] ), $params['category_data'] );
$this->update_category( intval( $params['category_id'] ), $params['category_data'] );
break;
case 'deleted':
$this->delete_category( strval( $params['category_id'] ) );
$this->delete_category( intval( $params['category_id'] ) );
break;
}
return $this;
}

private function get_or_create_category( string $category_id ): Category {
private function get_or_create_category( int $category_id ): Category {
$categories = $this->kv_store->fetch( $this->site_info->id, 'categories', array( $category_id ), array( Category::class ) );
if ( ! isset( $categories[ $category_id ] ) ) {
$category = new Category();
Expand All @@ -65,20 +65,20 @@ private function get_or_create_category( string $category_id ): Category {
return $categories[ $category_id ];
}

private function update_category( string $category_id, array $category_data ) {
private function update_category( int $category_id, array $category_data ) {
$category = $this->get_or_create_category( $category_id );
$category->name = $category_data['name'] ?? $category->name ?? '';
$category->slug = $category_data['slug'] ?? $category->slug ?? '';
$category->description = $category_data['description'] ?? $category->description ?? '';
$category->parent = $category_data['parent'] ?? $category->parent ?? '';
$category->parent = $category_data['parent'] ?? $category->parent ?? 0;
$category->count = $category_data['count'] ?? $category->count ?? 0;
$category->image = $category_data['image'] ?? $category->image ?? array();
$category->review_count = $category_data['review_count'] ?? $category->review_count ?? 0;
$category->id = $category_id;
$this->kv_store->update( $this->site_info->id, 'categories', array( $category_id => $category ) );
}

private function delete_category( string $category_id ) {
private function delete_category( int $category_id ) {
$this->kv_store->delete( $this->site_info->id, 'categories', array( $category_id ) );
}
}
40 changes: 40 additions & 0 deletions app/tests/unit/Webhooks/CategoryWebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,44 @@ public function test_create_category(): void {
$this->assertEquals( 0, $category->parent );
$this->assertEquals( 1, $category->count );
}

public function test_update_category(): void {
$this->auth->expects( $this->once() )->method( 'authenticate' )->willReturnSelf();

$category = new Category();
$category->id = 1;
$category->name = 'Test Category';
$category->slug = 'test-category';
$category->description = 'Test Category';

$this->kv_store->add( $this->site_info->id, 'categories', array( 1 => $category ) );

$request_body = array(
'category_id' => 1,
'category_data' => array(
'name' => 'Updated Category',
),
'action' => 'updated',
);
$this->request->method( 'getPayload' )->willReturn( new InputBag( $request_body ) );

$this->sut->serve();

$categories = $this->kv_store->fetch( $this->site_info->id, 'categories', array( 1 ), array( Category::class ) );
$category = $categories[1];
$this->assertEquals( 'Updated Category', $category->name );
}

public function test_delete_category(): void {
$this->auth->expects( $this->once() )->method( 'authenticate' )->willReturnSelf();
$this->kv_store->add( $this->site_info->id, 'categories', array( 1 => new Category() ) );
$request_body = array(
'category_id' => 1,
'action' => 'deleted',
);
$this->request->method( 'getPayload' )->willReturn( new InputBag( $request_body ) );
$this->sut->serve();
$categories = $this->kv_store->fetch( $this->site_info->id, 'categories', array( 1 ), array( Category::class ) );
$this->assertEmpty( $categories );
}
}

0 comments on commit 7322c38

Please sign in to comment.