Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Data Fields | Static Protected Member Functions | Static Protected Attributes
ObserverImplementationTest Class Reference
Inheritance diagram for ObserverImplementationTest:

Public Member Functions

 testObserverInterfaceImplementation ()
 
 testObserverHasNoExtraPublicMethods ()
 

Static Public Member Functions

static setUpBeforeClass ()
 

Data Fields

const OBSERVER_INTERFACE = \Magento\Framework\Event\ObserverInterface::class
 

Static Protected Member Functions

static getObserverClasses ($fileNamePattern, $xpath)
 

Static Protected Attributes

static $observerClasses = []
 

Detailed Description

PAY ATTENTION: Current implementation does not support of virtual types

Definition at line 13 of file ObserverImplementationTest.php.

Member Function Documentation

◆ getObserverClasses()

static getObserverClasses (   $fileNamePattern,
  $xpath 
)
staticprotected
Parameters
string$fileNamePattern
string$xpath
Returns
array

Definition at line 84 of file ObserverImplementationTest.php.

85  {
86  $observerClasses = [];
87  foreach (Files::init()->getConfigFiles($fileNamePattern, [], false) as $configFile) {
88  foreach (simplexml_load_file($configFile)->xpath($xpath) as $observer) {
89  $className = (string)$observer->attributes()->instance;
90  // $className may be empty in cases like this <observer name="observer_name" disabled="true" />
91  if ($className) {
92  $observerClasses[] = trim((string)$observer->attributes()->instance, '\\');
93  }
94  }
95  }
96 
97  $blacklistFiles = str_replace('\\', '/', realpath(__DIR__)) . '/_files/blacklist/observers*.txt';
98  $blacklistExceptions = [];
99  foreach (glob($blacklistFiles) as $fileName) {
100  $blacklistExceptions = array_merge(
101  $blacklistExceptions,
102  file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
103  );
104  }
105  return array_diff(
106  array_unique($observerClasses),
107  $blacklistExceptions
108  );
109  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$fileName
Definition: translate.phtml:15
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ setUpBeforeClass()

static setUpBeforeClass ( )
static

Definition at line 25 of file ObserverImplementationTest.php.

26  {
27  self::$observerClasses = array_merge(
28  self::getObserverClasses('{*/events.xml,events.xml}', '//observer')
29  );
30  }

◆ testObserverHasNoExtraPublicMethods()

testObserverHasNoExtraPublicMethods ( )

Definition at line 54 of file ObserverImplementationTest.php.

55  {
56  $errors = [];
57  foreach (self::$observerClasses as $observerClass) {
58  $reflection = (new \ReflectionClass($observerClass));
59  $maxCountMethod = $reflection->getConstructor() ? 2 : 1;
60 
61  if (count($reflection->getMethods(\ReflectionMethod::IS_PUBLIC)) > $maxCountMethod) {
62  $errors[] = $observerClass;
63  }
64  }
65 
66  if ($errors) {
67  $errors = array_unique($errors);
68  sort($errors);
69  $this->fail(
70  sprintf(
71  '%d of observers have extra public methods: %s',
72  count($errors),
73  implode("\n", $errors)
74  )
75  );
76  }
77  }
$errors
Definition: overview.phtml:9

◆ testObserverInterfaceImplementation()

testObserverInterfaceImplementation ( )

Definition at line 32 of file ObserverImplementationTest.php.

33  {
34  $errors = [];
35  foreach (self::$observerClasses as $observerClass) {
36  if (!is_subclass_of($observerClass, self::OBSERVER_INTERFACE)) {
37  $errors[] = $observerClass;
38  }
39  }
40 
41  if ($errors) {
42  $errors = array_unique($errors);
43  sort($errors);
44  $this->fail(
45  sprintf(
46  '%d of observers which not implement \Magento\Framework\Event\ObserverInterface: %s',
47  count($errors),
48  "\n" . implode("\n", $errors)
49  )
50  );
51  }
52  }
is_subclass_of($obj, $className)
$errors
Definition: overview.phtml:9

Field Documentation

◆ $observerClasses

$observerClasses = []
staticprotected

Definition at line 23 of file ObserverImplementationTest.php.

◆ OBSERVER_INTERFACE

const OBSERVER_INTERFACE = \Magento\Framework\Event\ObserverInterface::class

Observer interface

Definition at line 18 of file ObserverImplementationTest.php.


The documentation for this class was generated from the following file: