1+ <?php
2+ /**
3+ * DesignPatternsPHP
4+ *
5+ * Copyright (c) 2014 Matteo Tafani Alunno
6+ *
7+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8+ * of this software and associated documentation files (the "Software"), to deal
9+ * in the Software without restriction, including without limitation the rights
10+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+ * copies of the Software, and to permit persons to whom the Software is
12+ * furnished to do so, subject to the following conditions:
13+ *
14+ * The above copyright notice and this permission notice shall be included in
15+ * all copies or substantial portions of the Software.
16+ *
17+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+ * THE SOFTWARE.
24+ */
25+
26+ namespace DesignPatterns \Tests \ServiceLocator ;
27+
28+ use DesignPatterns \ServiceLocator \DatabaseService ;
29+ use DesignPatterns \ServiceLocator \LogService ;
30+ use DesignPatterns \ServiceLocator \ServiceLocator ;
31+ use \PHPUnit_Framework_TestCase as TestCase ;
32+
33+ class ServiceLocatorTest extends TestCase
34+ {
35+ /**
36+ * @var LogService
37+ */
38+ private $ logService ;
39+
40+ /**
41+ * @var DatabaseService
42+ */
43+ private $ databaseService ;
44+
45+ /**
46+ * @var ServiceLocator
47+ */
48+ private $ serviceLocator ;
49+
50+ public function setUp ()
51+ {
52+ $ this ->serviceLocator = new ServiceLocator ();
53+
54+ $ this ->logService = new LogService ();
55+ $ this ->databaseService = new DatabaseService ();
56+ }
57+
58+ public function testHasServices ()
59+ {
60+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\LogServiceInterface ' , $ this ->logService );
61+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' , $ this ->databaseService );
62+
63+ $ this ->assertTrue ($ this ->serviceLocator ->has ('DesignPatterns\ServiceLocator\LogServiceInterface ' ));
64+ $ this ->assertTrue ($ this ->serviceLocator ->has ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' ));
65+
66+ $ this ->assertFalse ($ this ->serviceLocator ->has ('DesignPatterns\ServiceLocator\FakeServiceInterface ' ));
67+ }
68+
69+ public function testServicesWithObject ()
70+ {
71+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\LogServiceInterface ' , $ this ->logService );
72+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' , $ this ->databaseService );
73+
74+ $ this ->assertSame ($ this ->logService , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\LogServiceInterface ' ));
75+ $ this ->assertSame ($ this ->databaseService , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' ));
76+ }
77+
78+ public function testServicesWithClass ()
79+ {
80+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\LogServiceInterface ' , get_class ($ this ->logService ));
81+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' , get_class ($ this ->databaseService ));
82+
83+ $ this ->assertNotSame ($ this ->logService , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\LogServiceInterface ' ));
84+ $ this ->assertInstanceOf ('DesignPatterns\ServiceLocator\LogServiceInterface ' , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\LogServiceInterface ' ));
85+
86+ $ this ->assertNotSame ($ this ->databaseService , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' ));
87+ $ this ->assertInstanceOf ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' ));
88+ }
89+
90+ public function testServicesNotShared ()
91+ {
92+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\LogServiceInterface ' , $ this ->logService , false );
93+ $ this ->serviceLocator ->add ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' , $ this ->databaseService , false );
94+
95+ $ this ->assertNotSame ($ this ->logService , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\LogServiceInterface ' ));
96+ $ this ->assertInstanceOf ('DesignPatterns\ServiceLocator\LogServiceInterface ' , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\LogServiceInterface ' ));
97+
98+ $ this ->assertNotSame ($ this ->databaseService , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' ));
99+ $ this ->assertInstanceOf ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' , $ this ->serviceLocator ->get ('DesignPatterns\ServiceLocator\DatabaseServiceInterface ' ));
100+ }
101+ }
102+
0 commit comments