ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Event;
|
||||
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
|
||||
/**
|
||||
* Observer of Magento events triggered using \Magento\TestFramework\EventManager::dispatch()
|
||||
*/
|
||||
class Magento implements ObserverInterface
|
||||
{
|
||||
/**
|
||||
* Used when Magento framework instantiates the class on its own and passes nothing to the constructor
|
||||
*
|
||||
* @var \Magento\TestFramework\EventManager
|
||||
*/
|
||||
protected static $_defaultEventManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\EventManager
|
||||
*/
|
||||
protected $_eventManager;
|
||||
|
||||
/**
|
||||
* Assign default event manager instance
|
||||
*
|
||||
* @param \Magento\TestFramework\EventManager $eventManager
|
||||
*/
|
||||
public static function setDefaultEventManager(\Magento\TestFramework\EventManager $eventManager = null)
|
||||
{
|
||||
self::$_defaultEventManager = $eventManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Magento\TestFramework\EventManager $eventManager
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function __construct($eventManager = null)
|
||||
{
|
||||
$this->_eventManager = $eventManager ?: self::$_defaultEventManager;
|
||||
if (!$this->_eventManager instanceof \Magento\TestFramework\EventManager) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Instance of the "Magento\TestFramework\EventManager" is expected.')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'core_app_init_current_store_after' event, that converts it into 'initStoreAfter'
|
||||
* @param \Magento\Framework\Event\Observer $observer
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function execute(\Magento\Framework\Event\Observer $observer)
|
||||
{
|
||||
$this->_eventManager->fireEvent('initStoreAfter');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parameter holder for transaction events
|
||||
*/
|
||||
namespace Magento\TestFramework\Event\Param;
|
||||
|
||||
class Transaction
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isStartRequested;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isRollbackRequested;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_isStartRequested = false;
|
||||
$this->_isRollbackRequested = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request to start transaction
|
||||
*/
|
||||
public function requestTransactionStart()
|
||||
{
|
||||
$this->_isStartRequested = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request to rollback transaction
|
||||
*/
|
||||
public function requestTransactionRollback()
|
||||
{
|
||||
$this->_isRollbackRequested = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether transaction start has been requested or not
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isTransactionStartRequested()
|
||||
{
|
||||
return $this->_isStartRequested;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether transaction rollback has been requested or not
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isTransactionRollbackRequested()
|
||||
{
|
||||
return $this->_isRollbackRequested;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Listener of PHPUnit built-in events
|
||||
*/
|
||||
namespace Magento\TestFramework\Event;
|
||||
|
||||
class PhpUnit implements \PHPUnit\Framework\TestListener
|
||||
{
|
||||
/**
|
||||
* Used when PHPUnit framework instantiates the class on its own and passes nothing to the constructor
|
||||
*
|
||||
* @var \Magento\TestFramework\EventManager
|
||||
*/
|
||||
protected static $_defaultEventManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\EventManager
|
||||
*/
|
||||
protected $_eventManager;
|
||||
|
||||
/**
|
||||
* Assign default event manager instance
|
||||
*
|
||||
* @param \Magento\TestFramework\EventManager $eventManager
|
||||
*/
|
||||
public static function setDefaultEventManager(\Magento\TestFramework\EventManager $eventManager = null)
|
||||
{
|
||||
self::$_defaultEventManager = $eventManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\TestFramework\EventManager $eventManager
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function __construct(\Magento\TestFramework\EventManager $eventManager = null)
|
||||
{
|
||||
$this->_eventManager = $eventManager ?: self::$_defaultEventManager;
|
||||
if (!$this->_eventManager) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(__('Instance of the event manager is required.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function addError(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function addFailure(
|
||||
\PHPUnit\Framework\Test $test,
|
||||
\PHPUnit\Framework\AssertionFailedError $e,
|
||||
float $time
|
||||
): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void
|
||||
{
|
||||
/* PHPUnit runs tests with data provider in own test suite for each test, so just skip such test suites */
|
||||
if ($suite instanceof \PHPUnit\Framework\DataProviderTestSuite) {
|
||||
return;
|
||||
}
|
||||
$this->_eventManager->fireEvent('startTestSuite');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void
|
||||
{
|
||||
if ($suite instanceof \PHPUnit\Framework\DataProviderTestSuite) {
|
||||
return;
|
||||
}
|
||||
$this->_eventManager->fireEvent('endTestSuite', [$suite], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function startTest(\PHPUnit\Framework\Test $test): void
|
||||
{
|
||||
if (!$test instanceof \PHPUnit\Framework\TestCase || $test instanceof \PHPUnit\Framework\Warning) {
|
||||
return;
|
||||
}
|
||||
$this->_eventManager->fireEvent('startTest', [$test]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function endTest(\PHPUnit\Framework\Test $test, float $time): void
|
||||
{
|
||||
if (!$test instanceof \PHPUnit\Framework\TestCase || $test instanceof \PHPUnit\Framework\Warning) {
|
||||
return;
|
||||
}
|
||||
$this->_eventManager->fireEvent('endTest', [$test], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\TestFramework\Event;
|
||||
|
||||
/**
|
||||
* Database transaction events manager
|
||||
*/
|
||||
class Transaction
|
||||
{
|
||||
/**
|
||||
* @var \Magento\TestFramework\EventManager
|
||||
*/
|
||||
protected $_eventManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\Event\Param\Transaction
|
||||
*/
|
||||
protected $_eventParam;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isTransactionActive = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Magento\TestFramework\EventManager $eventManager
|
||||
*/
|
||||
public function __construct(\Magento\TestFramework\EventManager $eventManager)
|
||||
{
|
||||
$this->_eventManager = $eventManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'startTest' event
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
*/
|
||||
public function startTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
$this->_processTransactionRequests('startTest', $test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTest' event
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
*/
|
||||
public function endTest(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
$this->_processTransactionRequests('endTest', $test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for 'endTestSuite' event
|
||||
*/
|
||||
public function endTestSuite()
|
||||
{
|
||||
$this->_rollbackTransaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* Query whether there are any requests for transaction operations and performs them
|
||||
*
|
||||
* @param string $eventName
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
*/
|
||||
protected function _processTransactionRequests($eventName, \PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
$param = $this->_getEventParam();
|
||||
$this->_eventManager->fireEvent($eventName . 'TransactionRequest', [$test, $param]);
|
||||
if ($param->isTransactionRollbackRequested()) {
|
||||
$this->_rollbackTransaction();
|
||||
}
|
||||
if ($param->isTransactionStartRequested()) {
|
||||
$this->_startTransaction($test);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start transaction and fire 'startTransaction' event
|
||||
*
|
||||
* @param \PHPUnit\Framework\TestCase $test
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
|
||||
*/
|
||||
protected function _startTransaction(\PHPUnit\Framework\TestCase $test)
|
||||
{
|
||||
if (!$this->_isTransactionActive) {
|
||||
$this->_getConnection()->beginTransparentTransaction();
|
||||
$this->_isTransactionActive = true;
|
||||
try {
|
||||
/**
|
||||
* Add any warning during transaction execution as a failure.
|
||||
*/
|
||||
set_error_handler(
|
||||
function ($errNo, $errStr, $errFile, $errLine) use ($test) {
|
||||
$errMsg = sprintf("%s: %s in %s:%s.", "Warning", $errStr, $errFile, $errLine);
|
||||
$test->getTestResultObject()->addError($test, new \PHPUnit\Framework\Warning($errMsg), 0);
|
||||
|
||||
// Allow error to be handled by next error handler
|
||||
return false;
|
||||
},
|
||||
E_WARNING
|
||||
);
|
||||
$this->_eventManager->fireEvent('startTransaction', [$test]);
|
||||
restore_error_handler();
|
||||
} catch (\Exception $e) {
|
||||
$test->getTestResultObject()->addFailure(
|
||||
$test,
|
||||
new \PHPUnit\Framework\AssertionFailedError((string)$e),
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollback transaction and fire 'rollbackTransaction' event
|
||||
*/
|
||||
protected function _rollbackTransaction()
|
||||
{
|
||||
if ($this->_isTransactionActive) {
|
||||
$this->_getConnection()->rollbackTransparentTransaction();
|
||||
$this->_isTransactionActive = false;
|
||||
$this->_eventManager->fireEvent('rollbackTransaction');
|
||||
$this->_getConnection()->closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve database adapter instance
|
||||
*
|
||||
* @param string $connectionName
|
||||
* @return \Magento\Framework\DB\Adapter\AdapterInterface|\Magento\TestFramework\Db\Adapter\TransactionInterface
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
protected function _getConnection($connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION)
|
||||
{
|
||||
/** @var $resource \Magento\Framework\App\ResourceConnection */
|
||||
$resource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\ResourceConnection::class);
|
||||
return $resource->getConnection($connectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve clean instance of transaction event parameter
|
||||
*
|
||||
* @return \Magento\TestFramework\Event\Param\Transaction
|
||||
*/
|
||||
protected function _getEventParam()
|
||||
{
|
||||
/* reset object state instead of instantiating new object over and over again */
|
||||
if (!$this->_eventParam) {
|
||||
$this->_eventParam = new \Magento\TestFramework\Event\Param\Transaction();
|
||||
} else {
|
||||
$this->_eventParam->__construct();
|
||||
}
|
||||
return $this->_eventParam;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user