Skip to content

Commit a8ba0b5

Browse files
committed
Merge pull request #19 from JindrichPilar/master
Možnost vlastní třídy pro WS (via AbstractFactory)
2 parents 4d0c64d + d104c82 commit a8ba0b5

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace SkautIS\Factory;
4+
5+
use SkautIS\Factory\WSFactory;
6+
use SkautIS\WS;
7+
8+
/**
9+
* @inheritdoc
10+
*/
11+
class BasicWSFactory extends WSFactory {
12+
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function createWS($wsdl, array $init, $compression, $profiler) {
17+
return new WS($wsdl, $init, $compression, $profiler);
18+
}
19+
20+
}

src/SkautIS/Factory/WSFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace SkautIS\Factory;
4+
5+
use SkautIS\WS;
6+
7+
/**
8+
* Trida umoznujici pouziti vlastni tridy WS se tridou SkautIS
9+
*/
10+
abstract class WSFactory {
11+
12+
/**
13+
* Vytvor novy WS objekt
14+
*
15+
* @param string $wsdl Odkaz na WSDL soubor
16+
* @param array $init Zakladni informace pro vsechny pozadavky
17+
* @param bool $compression Ma pouzivat kompresi na prenasena data?
18+
* @param bool $profiler Ma ukladat data pro profilovani?
19+
*
20+
* @return WS;
21+
*/
22+
abstract public function createWS($wsdl, array $init, $compression, $profiler);
23+
}

src/SkautIS/Nette/Tracy/Panel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function prepareTrace(array $trace) {
8787
$s = '<a href="#"class="tracy-toggle tracy-collapsed">Trace</a><div class="tracy-collapsed">';
8888
$cnt = 0;
8989
foreach ($trace as $f) {
90-
$s .= "" . ++$cnt . ". " . $f['function'] . " (" . $f['class'] . ($f['line'] !== NULL ? ": " . $f['line'] : "") . ")" . '<br />';
90+
$s .= "" . ++$cnt . ". " . $f['function'] . " (" . $f['class'] . (array_key_exists("line", $f) ? ": " . $f['line'] : "") . ")" . '<br />';
9191
}
9292
return $s . "</div>";
9393
}

src/SkautIS/SkautIS.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace SkautIS;
44

5+
use SkautIS\Factory\WSFactory;
6+
use SkautIS\Factory\BasicWSFactory;
57
use SkautIS\Exception\AbortException;
68
use SkautIS\Exception\InvalidArgumentException;
79
use SkautIS\Exception\WsdlException;
@@ -103,6 +105,11 @@ class SkautIS {
103105
*/
104106
public $profiler;
105107

108+
/**
109+
* @var WSFactory
110+
*/
111+
protected $wsFactory = NULL;
112+
106113
// </editor-fold>
107114
// <editor-fold defaultstate="collapsed" desc="getters & setters">
108115

@@ -201,6 +208,10 @@ public function setStorage(&$storage, $leaveValues = false) {
201208
private function __construct() {
202209
$this->perStorage = &$_SESSION["__" . __CLASS__]; //defaultni persistentní uloziste
203210

211+
if ($this->wsFactory === NULL) {
212+
$this->wsFactory = new BasicWSFactory();
213+
}
214+
204215
if (defined("SkautIS_ID_Application")) {
205216
$this->setAppId(SkautIS_ID_Application);
206217
}
@@ -215,7 +226,7 @@ private function __construct() {
215226
* @return SkautIS
216227
* @throws InvalidArgumentException
217228
*/
218-
public static function getInstance($appId = NULL, $testMode = FALSE, $profiler = FALSE) {
229+
public static function getInstance($appId = NULL, $testMode = FALSE, $profiler = FALSE, $wsFactory = NULL) {
219230
if (!is_bool($testMode)) {
220231
throw new InvalidArgumentException('Argument $testMode ma spatnou hodnotu: ' . print_r($testMode, TRUE));
221232
}
@@ -235,6 +246,11 @@ public static function getInstance($appId = NULL, $testMode = FALSE, $profiler =
235246
self::$instance->setTestMode($testMode);
236247
self::$instance->profiler = $profiler;
237248

249+
if ($wsFactory !== NULL) {
250+
self::$instance->wsFactory = $wsFactory;
251+
}
252+
253+
238254
return self::$instance;
239255
}
240256

@@ -256,7 +272,7 @@ public function __get($name) {
256272
}
257273

258274
if (!isset($this->active[$wsdlKey])) {
259-
$this->active[$wsdlKey] = new WS($this->getWsdlUri($wsdlName), $this->perStorage->init, $this->compression, $this->profiler);
275+
$this->active[$wsdlKey] = $this->wsFactory->createWS($this->getWsdlUri($wsdlName), $this->perStorage->init, $this->compression, $this->profiler);
260276
if ($this->profiler) {
261277
$this->active[$wsdlKey]->onEvent = $this->onEvent;
262278
}
@@ -372,4 +388,13 @@ public function isMaintenance() {
372388
return FALSE;
373389
}
374390

391+
/**
392+
* Nastavi WSFactory
393+
*
394+
* @param $wsFactory WSFactory
395+
*/
396+
public function setWSFactory(WSFactory $wsFactory)
397+
{
398+
$this->wsFactory = $wsFactory;
399+
}
375400
}

tests/SkautIS/SkautISTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,30 @@ public function testGetWsdlList() {
4343
}
4444
}
4545

46-
public function test__get() {
46+
public function testMagicMethodGet() {
47+
$wsA = new \StdClass;
48+
$wsB = new \StdClass;
49+
50+
$factory = \Mockery::mock("\SkautIS\Factory\WSFactory");
51+
$factory->shouldReceive("createWS")->with()->twice()->andReturn($wsA, $wsB);
4752

4853
$skautIS = \SkautIS\SkautIS::getInstance("123");
54+
$skautIS->setWSFactory($factory);
4955

5056

5157
$skautIS->setTestMode(true);
52-
$userA = $skautIS->user;
53-
$this->assertSame($userA, $skautIS->user);
58+
$eventA = $skautIS->event;
59+
$this->assertSame($eventA, $skautIS->event);
5460

5561
$skautIS->setTestMode(false);
56-
$this->assertNotSame($userA, $skautIS->user);
62+
$this->assertNotSame($eventA, $skautIS->event);
63+
$eventB = $skautIS->event;
5764

5865
$skautIS->setTestMode(true);
59-
$this->assertSame($userA, $skautIS->user);
66+
$this->assertSame($eventA, $skautIS->event);
67+
68+
$this->assertSame($wsA, $eventA);
69+
$this->assertSame($wsB, $eventB);
6070
}
6171

6272
public function testSetLoginData() {
@@ -82,8 +92,16 @@ public function testSetLoginData() {
8292
}
8393

8494
public function testIsLoggedIn() {
95+
$ws = \Mockery::mock("\SkautIS\WS");
96+
$ws->shouldReceive("LoginUpdateRefresh")->once()->andReturn();
97+
98+
99+
$factory = \Mockery::mock("\SkautIS\Factory\WSFactory");
100+
$factory->shouldReceive("createWS")->with()->once()->andReturn($ws);
101+
85102
$skautIS = SkautIS::getInstance();
86-
$this->assertFalse($skautIS->isLoggedIn());
87-
}
103+
$skautIS->setWSFactory($factory);
88104

105+
$this->assertTrue($skautIS->isLoggedIn());
106+
}
89107
}

0 commit comments

Comments
 (0)