Skip to content

Commit 6d95046

Browse files
authored
Ported laravel cache provider without implementing deferable (#84)
Required for wintercms/winter#501. See also wintercms/winter#493 Also removed upsert tests ref: 04230d5
1 parent c9a83dc commit 6d95046

2 files changed

Lines changed: 52 additions & 86 deletions

File tree

src/Cache/CacheServiceProvider.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Winter\Storm\Cache;
4+
5+
use Illuminate\Cache\CacheManager;
6+
use Illuminate\Support\ServiceProvider;
7+
use Symfony\Component\Cache\Adapter\Psr16Adapter;
8+
9+
class CacheServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register the service provider.
13+
*
14+
* @return void
15+
*/
16+
public function register()
17+
{
18+
$this->app->singleton('cache', function ($app) {
19+
return new CacheManager($app);
20+
});
21+
22+
$this->app->singleton('cache.store', function ($app) {
23+
return $app['cache']->driver();
24+
});
25+
26+
$this->app->singleton('cache.psr6', function ($app) {
27+
return new Psr16Adapter($app['cache.store']);
28+
});
29+
30+
$this->app->singleton('memcached.connector', function () {
31+
return new MemcachedConnector;
32+
});
33+
34+
$this->app->singleton(RateLimiter::class, function ($app) {
35+
return new RateLimiter($app->make('cache')->driver(
36+
$app['config']->get('cache.limiter')
37+
));
38+
});
39+
}
40+
41+
/**
42+
* Get the services provided by the provider.
43+
*
44+
* @return array
45+
*/
46+
public function provides()
47+
{
48+
return [
49+
'cache', 'cache.store', 'cache.psr6', 'memcached.connector', RateLimiter::class,
50+
];
51+
}
52+
}

tests/Database/QueryBuilderTest.php

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -94,92 +94,6 @@ public function testSelectConcat()
9494
);
9595
}
9696

97-
public function testUpsert()
98-
{
99-
// MySQL
100-
$builder = $this->getMySqlBuilder();
101-
$builder->getConnection()
102-
->expects($this->once())
103-
->method('affectingStatement')
104-
->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) on duplicate key update `email` = values(`email`), `name` = values(`name`)', ['foo', 'bar', 'foo2', 'bar2'])
105-
->willReturn(2);
106-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
107-
$this->assertEquals(2, $result);
108-
109-
// PostgreSQL
110-
$builder = $this->getPostgresBuilder();
111-
$builder->getConnection()
112-
->expects($this->once())
113-
->method('affectingStatement')
114-
->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "email" = "excluded"."email", "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])
115-
->willReturn(2);
116-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
117-
$this->assertEquals(2, $result);
118-
119-
// SQLite
120-
$builder = $this->getSQLiteBuilder();
121-
$builder->getConnection()
122-
->expects($this->once())
123-
->method('affectingStatement')
124-
->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "email" = "excluded"."email", "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])
125-
->willReturn(2);
126-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
127-
$this->assertEquals(2, $result);
128-
129-
// SQL Server
130-
$builder = $this->getSqlServerBuilder();
131-
$builder->getConnection()
132-
->expects($this->once())
133-
->method('affectingStatement')
134-
->with('merge [users] using (values (?, ?), (?, ?)) [laravel_source] ([email], [name]) on [laravel_source].[email] = [users].[email] when matched then update set [email] = [laravel_source].[email], [name] = [laravel_source].[name] when not matched then insert ([email], [name]) values ([email], [name]);', ['foo', 'bar', 'foo2', 'bar2'])
135-
->willReturn(2);
136-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
137-
$this->assertEquals(2, $result);
138-
}
139-
140-
public function testUpsertWithUpdateColumns()
141-
{
142-
// MySQL
143-
$builder = $this->getMySqlBuilder();
144-
$builder->getConnection()
145-
->expects($this->once())
146-
->method('affectingStatement')
147-
->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) on duplicate key update `name` = values(`name`)', ['foo', 'bar', 'foo2', 'bar2'])
148-
->willReturn(2);
149-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
150-
$this->assertEquals(2, $result);
151-
152-
// PostgreSQL
153-
$builder = $this->getPostgresBuilder();
154-
$builder->getConnection()
155-
->expects($this->once())
156-
->method('affectingStatement')
157-
->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])
158-
->willReturn(2);
159-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
160-
$this->assertEquals(2, $result);
161-
162-
// SQLite
163-
$builder = $this->getSQLiteBuilder();
164-
$builder->getConnection()
165-
->expects($this->once())
166-
->method('affectingStatement')
167-
->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])
168-
->willReturn(2);
169-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
170-
$this->assertEquals(2, $result);
171-
172-
// SQL Server
173-
$builder = $this->getSqlServerBuilder();
174-
$builder->getConnection()
175-
->expects($this->once())
176-
->method('affectingStatement')
177-
->with('merge [users] using (values (?, ?), (?, ?)) [laravel_source] ([email], [name]) on [laravel_source].[email] = [users].[email] when matched then update set [name] = [laravel_source].[name] when not matched then insert ([email], [name]) values ([email], [name]);', ['foo', 'bar', 'foo2', 'bar2'])
178-
->willReturn(2);
179-
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
180-
$this->assertEquals(2, $result);
181-
}
182-
18397
protected function getConnection($connection = null)
18498
{
18599
if ($connection) {

0 commit comments

Comments
 (0)