1+ <?php
2+
3+ namespace Tests ;
4+
5+ use BoxedCode \Laravel \Scout \ElasticsearchEngine ;
6+ use BoxedCode \Laravel \Scout \ElasticsearchServiceProvider ;
7+ use Elasticsearch \ClientBuilder as ElasticBuilder ;
8+ use Illuminate \Contracts \Config \Repository as Config ;
9+ use Illuminate \Contracts \Container \Container ;
10+ use Laravel \Scout \EngineManager ;
11+ use Mockery ;
12+ use Psr \Log \LoggerInterface ;
13+ use \Elasticsearch \Client as ElasticClient ;
14+
15+ class ElasticsearchServiceProviderTestCase extends AbstractTestCase
16+ {
17+ public function test_binds_elastic_builder ()
18+ {
19+ $ app = Mockery::mock (Container::class);
20+
21+ $ provider = new ElasticsearchServiceProvider ($ app );
22+
23+ $ app ->shouldReceive ('bound ' )
24+ ->once ()
25+ ->with (ElasticBuilder::class)
26+ ->andReturnFalse ();
27+
28+ $ app ->shouldReceive ('bind ' )
29+ ->once ()
30+ ->with (ElasticBuilder::class, Mockery::any ())
31+ ->andReturnNull ();
32+
33+ $ this ->assertNull ($ provider ->register ());
34+ }
35+
36+ public function test_honors_currently_bound_elastic_builder ()
37+ {
38+ $ app = Mockery::mock (Container::class);
39+
40+ $ provider = new ElasticsearchServiceProvider ($ app );
41+
42+ $ app ->shouldReceive ('bound ' )
43+ ->once ()
44+ ->with (ElasticBuilder::class)
45+ ->andReturnTrue ();
46+
47+ $ this ->assertNull ($ provider ->register ());
48+ }
49+
50+ protected function setupEngineRegistration ()
51+ {
52+ $ app = Mockery::mock (Container::class);
53+
54+ $ provider = new ElasticsearchServiceProvider ($ app );
55+
56+ $ config = Mockery::mock (Config::class);
57+
58+ $ app ->shouldReceive ('make ' )
59+ ->twice ()
60+ ->with ('config ' )
61+ ->andReturn ($ config );
62+
63+ $ app ->shouldReceive ('make ' )
64+ ->once ()
65+ ->with (EngineManager::class)
66+ ->andReturn (
67+ $ manager = new EngineManager ($ app )
68+ );
69+
70+ $ app ->shouldReceive ('make ' )
71+ ->once ()
72+ ->with (ElasticBuilder::class)
73+ ->andReturn (
74+ $ builder = Mockery::mock (ElasticBuilder::class)
75+ );
76+
77+ $ config ->shouldReceive ('get ' )
78+ ->once ()
79+ ->with ('scout.elasticsearch.hosts ' )
80+ ->andReturn (['127.0.0.1 ' ]);
81+
82+ $ builder ->shouldReceive ('setHosts ' )
83+ ->once ()
84+ ->with (['127.0.0.1 ' ])
85+ ->andReturnSelf ();
86+
87+ $ builder ->shouldReceive ('build ' )
88+ ->once ()
89+ ->andReturn (
90+ $ elastic = Mockery::mock (ElasticClient::class)
91+ );
92+
93+ return [$ app , $ config , $ provider , $ manager , $ builder ];
94+ }
95+
96+ public function test_engine_registration ()
97+ {
98+ [$ app , $ config , $ provider , $ manager , $ builder ] = $ this ->setupEngineRegistration ();
99+
100+ $ config ->shouldReceive ('get ' )
101+ ->once ()
102+ ->with ('scout.elasticsearch.debug ' , false )
103+ ->andReturnFalse ();
104+
105+ $ provider ->boot ();
106+
107+ $ this ->assertInstanceOf (ElasticsearchEngine::class, $ manager ->engine ('elasticsearch ' ));
108+ }
109+
110+ public function test_engine_registration_with_debug_logger ()
111+ {
112+ [$ app , $ config , $ provider , $ manager , $ builder ] = $ this ->setupEngineRegistration ();
113+
114+ $ config ->shouldReceive ('get ' )
115+ ->once ()
116+ ->with ('scout.elasticsearch.debug ' , false )
117+ ->andReturnTrue ();
118+
119+ $ app ->shouldReceive ('make ' )
120+ ->once ()
121+ ->with (LoggerInterface::class)
122+ ->andReturn (
123+ $ logger = Mockery::mock (LoggerInterface::class)
124+ );
125+
126+ $ builder ->shouldReceive ('setLogger ' )
127+ ->once ()
128+ ->with ($ logger )
129+ ->andReturnSelf ();
130+
131+ $ provider ->boot ();
132+
133+ $ this ->assertInstanceOf (ElasticsearchEngine::class, $ manager ->engine ('elasticsearch ' ));
134+ }
135+ }
0 commit comments