/
DefaultPageLocator.php
147 lines (124 loc) · 3.41 KB
/
DefaultPageLocator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* This file is part of the QA-Tools library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
* @link https://github.com/qa-tools/qa-tools
*/
namespace QATools\QATools\PageObject\PageLocator;
use QATools\QATools\PageObject\Exception\PageFactoryException;
/**
* Class to return fully qualified class names by its name.
*
* @method \Mockery\Expectation shouldReceive(string $name)
*/
class DefaultPageLocator implements IPageLocator
{
/**
* Prefix for page classes.
*
* @var array
*/
protected $namespacePrefixes = array();
/**
* Creates the DefaultPageLocator.
*
* @param array $namespace_prefixes The page namespace prefixes.
*
* @throws PageFactoryException When no prefixes are given.
*/
public function __construct(array $namespace_prefixes)
{
if ( empty($namespace_prefixes) ) {
throw new PageFactoryException(
'No namespace prefixes passed',
PageFactoryException::TYPE_PAGE_MISSING_PREFIXES
);
}
foreach ( $namespace_prefixes as $prefix ) {
$this->namespacePrefixes[] = $this->normalize($prefix);
}
}
/**
* Normalizes passed namespace.
*
* @param string $namespace Namespace.
*
* @return string
*/
protected function normalize($namespace)
{
$normalized_namespace = rtrim($namespace, '\\') . '\\';
if ( strpos($normalized_namespace, '\\') !== 0 ) {
$normalized_namespace = '\\' . $normalized_namespace;
}
return $normalized_namespace;
}
/**
* Returns the fully qualified class name of a page by its name.
*
* @param string $name The name of the page.
*
* @return string
* @throws PageFactoryException When no name is given.
*/
public function resolvePage($name)
{
if ( empty($name) ) {
throw new PageFactoryException('No page name given', PageFactoryException::TYPE_PAGE_NAME_MISSING);
}
$possible_pages = $this->buildPossiblePages($name);
return $this->getExistingPageClass($possible_pages);
}
/**
* Builds all possible page classes from passed name and current prefixes.
*
* @param string $name Page name.
*
* @return array
*/
protected function buildPossiblePages($name)
{
$possible_classes = array();
$class_name = $this->buildClassNameFromName($name);
foreach ( $this->namespacePrefixes as $prefix ) {
$possible_classes[] = $prefix . $class_name;
}
return $possible_classes;
}
/**
* Builds the class name from a given name by uppercasing the first letter of each word and removing the spaces.
*
* @param string $name The class name.
*
* @return string
*/
protected function buildClassNameFromName($name)
{
$class_name_parts = explode(' ', $name);
return count($class_name_parts) == 1 ? $name : implode('', array_map('ucfirst', $class_name_parts));
}
/**
* Returns first existing class passed in array.
*
* @param array $possible_pages Possible page classes.
*
* @return string
* @throws PageFactoryException When page class is not found.
*/
protected function getExistingPageClass(array $possible_pages)
{
foreach ( $possible_pages as $page_class ) {
if ( class_exists($page_class) ) {
return $page_class;
}
}
$message = sprintf(
'None of the possible classes were found: %s',
implode(', ', $possible_pages)
);
throw new PageFactoryException($message, PageFactoryException::TYPE_PAGE_CLASS_NOT_FOUND);
}
}