Zend certified PHP/Magento developer

Test class with collection and save

i’m trying to test if a class method is doing her job.

Basically here it is.

public function __construct(SaveCommand $saveCommand, SubscriptionData $entityDataFactory,
                            ManagerInterface $manager) {
    $this->saveCommand = $saveCommand;
    $this->entityDataFactory = $entityDataFactory;
    $this->_eventManager = $manager;
}


/**
 * @param $subscriptionData
 * @return void
 */
public function updateSubscription($subscriptionData){

    try{
        $entityModel = $this->entityDataFactory;
        $entityModel->addData($subscriptionData);
        $this->saveCommand->execute($entityModel);
        
    }catch (Exception $e){
        throw($e);
    }

}

With the save command being like this

public function execute(SubscriptionInterface $subscription): int
{
    try {
        /** @var SubscriptionModel $model */
        $model = $this->modelFactory->create();
        $model->addData($subscription->getData());
        $model->setHasDataChanges(true);

        if (!$model->getData(SubscriptionInterface::SUBSCRIPTION_ID)) {
            $model->isObjectNew(true);
        }
        $this->resource->save($model);

I’m then writting my test class

protected function setUp(): void
{
    $objectManager = new ObjectManager($this);

    // Mock SaveCommand
    $this->saveCommandMock = $this->getMockBuilder(SaveCommand::class)
        ->disableOriginalConstructor()
        ->getMock();

    // Mock SubscriptionInterfaceFactory
    $this->subscriptionInterfaceFactoryMock = $this->getMockBuilder(SubscriptionData::class)
        ->disableOriginalConstructor()
        ->getMock();

    // Create SubscriptionAction instance with mocked dependencies
    $this->subscriptionAction = $objectManager->getObject(
        SubscriptionAction::class,
        [
            'saveCommand' => $this->saveCommandMock,
            'entityDataFactory' => $this->subscriptionInterfaceFactoryMock,
            'manager' => $this->createMock(MagentoFrameworkEventManagerInterface::class)
        ]
    );
}

public function testUpdateSubscription()
{
    $subscriptionData = [
        // Provide sample subscription data for testing
        'subscription_id' => 1,
        'is_active' => true,
        // Add more fields as needed for testing various scenarios
    ];

    // Check if the subscription is created
    $subscriptionCollectionMock = $this->getMockBuilder(SubscriptionCollection::class)
        ->disableOriginalConstructor()
        ->getMock();

    dump($subscriptionCollectionMock->getSize());
    // Call the method under test
    $this->subscriptionAction->updateSubscription($subscriptionData);
    dump($subscriptionCollectionMock->getSize());

    $subscriptionCollectionMock->addFieldToFilter('subscription_id', $subscriptionData['subscription_id']);
    $this->assertEquals(1, $subscriptionCollectionMock->getSize(), 'Subscription was not created.');

}

But the dump of the size is clearly showing me that the collection size is null before and after.

So i suspect i’m not doing things right due to the collection use…

Any help please ?