=VLOOKUP(B2,F$2:G$11,2,FALSE) というように書く

Excelは便利過ぎるわ。。
ソフトウェアエンジニアの技術ブログ:Software engineer tech blog
随机应变 ABCD: Always Be Coding and … : хороший
=VLOOKUP(B2,F$2:G$11,2,FALSE) というように書く

Excelは便利過ぎるわ。。
– オラクルの型に、VARCHARとVARCHAR2があるが、VARCHARは古い文字列型で、VARCHAR2はそれを拡張して作られた新しい文字列型
– VARCHARデータ型は、VARCHAR2データ型と同義
なるほど、違いはないのね。。
maatwebsite/excelを使ってExcelを出力する際に、stylingをしたい。
export.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.hoge {
color: green;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th class="hoge">id</th>
<th>name</th>
<th>email</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td class="hoge">{{$user->id}}</td>
<td style="color:red;">{{$user->name}}</td>
<td>{{$user->email}}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>

これだと、classで定義したcolorがexcel上に反映されない。
致し方がない、inlineで書くか。
### inline
<th style="background-color:#FF0000;color:#fff">id</th>

なるほど。
$animals = ["cat", "dog", "tiger","rabbit"]; $animals = array_slice($animals, 1, 3); var_dump($animals);
array(3) { [0]=> string(3) “dog” [1]=> string(5) “tiger” [2]=> string(6) “rabbit” }
例えば、値が2つしかなくて、3つ取り出そうとしたら、
$animals = ["cat", "dog"]; $animals = array_slice($animals, 0, 3); var_dump($animals);
array(2) { [0]=> string(3) “cat” [1]=> string(3) “dog” }
値がないときはnullとなる。
OK!
### ライブラリインストール
$ composer require maatwebsite/excel
app/config.php
'providers' => [
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
$ php artisan vendor:publish –provider=”Maatwebsite\Excel\ExcelServiceProvider”
$ php artisan make:export Export
app/Exports/Export.php
namespace App\Exports;
// use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class Export implements FromView
{
/**
* @return \Illuminate\Support\Collection
*/
// public function collection()
// {
// //
// }
private $view;
public function __contruct(View $view){
$this->view = $view;
}
public function view(): View {
return $this->view;
}
}
routing
Route::get('/export', [App\Http\Controllers\AdminController::class, 'export']);
controller
use App\Http\Requests\UserFormRequest;
use App\Exports\Export;
public function export(){
$users = User::get();
$view = view('admin.export', compact('users'));
return \Excel::download(new Export($view), 'users.xlsx');
}
export.blade.php
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
</tr>
@endforeach
</tbody>
</table>
ほうほう、中々デキる、こやつ

sessionでflashをつけてリダイレクトしているが、きちんとリダイレクトされるかのテストコードを書く
controller側
public function testFormConfirm(Request $request){
if($request->has('name') & $request->name !== NULL){
session()->flash('flashmessage',$request->name. 'さん、登録完了');
} else {
session()->flash('flashmessage','エラー: 名前を入力してください');
}
return redirect('/admin/test/form');
}
test code
public function testExample()
{
// $this->visit('/admin/test/form')->see('名前');
$response = $this->post('/admin/test/form/confirm', [
'name' => 'hpscript'
]);
$response->assertRedirect('/admin/test/form');
}
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:01.530, Memory: 34.00 MB
OK (1 test, 2 assertions)
なるほど、仕組みはわかってきた。keep goin.
route
Route::get('/admin/test/form', [App\Http\Controllers\AdminController::class, 'testForm']);
controller
public function testForm(){
return view('admin.test');
}

testCase
public function testExample()
{
$this->visit('/admin/test/form')->see('名前');
}
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 00:01.715, Memory: 32.00 MB
There was 1 error:
1) Tests\Feature\AdminTest::testExample
Error: Call to undefined method Tests\Feature\AdminTest::visit()
/home/vagrant/dev/testApp/tests/Feature/AdminTest.php:26
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
ん? visitを使うにはduskを入れないと駄目らしい。
と言うことで、公式を見ます。
https://laravel.com/docs/8.x/dusk
$ composer require –dev laravel/dusk
$ php artisan dusk:install
$ php artisan dusk:chrome-driver
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\UnknownErrorException: unknown error: cannot find Chrome binary
/home/vagrant/dev/testApp/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:139
/home/vagrant/dev/testApp/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:371
/home/vagrant/dev/testApp/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:136
/home/vagrant/dev/testApp/tests/DuskTestCase.php:43
/home/vagrant/dev/testApp/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:218
/home/vagrant/dev/testApp/vendor/laravel/framework/src/Illuminate/Support/helpers.php:234
/home/vagrant/dev/testApp/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:219
/home/vagrant/dev/testApp/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:97
/home/vagrant/dev/testApp/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:65
/home/vagrant/dev/testApp/tests/Browser/ExampleTest.php:22
うーん、なんでやろ。。ちょっとよくわからんな。
Unitテストでは、テスト用のデータベースを作り、それでテストする。
Schemaを使う。
mysql> show databases;
mysql> show tables;
mysql> create database unittest;
$ php artisan migrate –env=testing
mysql> use unittest;
mysql> describe users;
use Illuminate\Support\Facades\Schema;
public function testExample()
{
$this->assertTrue(
Schema::hasColumns('users',[
'id', 'name', 'email'
]),
1
);
}
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.086, Memory: 26.00 MB
OK (1 test, 1 assertion)
DBにデータ挿入
public function testExample()
{
$user = new User();
$user->name ="hpscript";
$user->email = "info@hpscript.com";
$user->password = "password";
$user->role_id = 1;
$saveUser = $user->save();
$this->assertTrue($saveUser);
}
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.094, Memory: 26.00 MB
OK (1 test, 1 assertion)
mysql> select * from users;
+—-+———-+——————-+——————-+———-+——————-+—————————+—————-+—————+—————–+——————–+———————+———————+———+
| id | name | email | email_verified_at | password | two_factor_secret | two_factor_recovery_codes | remember_token | last_login_at | current_team_id | profile_photo_path | created_at | updated_at | role_id |
+—-+———-+——————-+——————-+———-+——————-+—————————+—————-+—————+—————–+——————–+———————+———————+———+
| 1 | hpscript | info@hpscript.com | NULL | password | NULL | NULL | NULL | NULL | NULL | NULL | 2021-01-03 16:44:08 | 2021-01-03 16:44:08 | 1 |
+—-+———-+——————-+——————-+———-+——————-+—————————+—————-+—————+—————–+——————–+———————+———————+———+
1 row in set (0.00 sec)
おおお、入ってる。
use RefreshDatabase;とすると、データが削除されます。
mysql> select * from users;
Empty set (0.00 sec)
なるほど、ちょっと理解してきた。
単体テストがイマイチ腹に落ちない。
やりたいことは、controllerのメソッド単位でのテスト
$ php artisan make:test AdminTest
/tests/Feature/AdminTest.php
public function testExample()
{
$response = $this->get('/');
$response->assertStatus(200);
}
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.144, Memory: 26.00 MB
OK (1 test, 1 assertion)
route/web.php
Route::get('/admin/test/form', [App\Http\Controllers\AdminController::class, 'testForm']);
AdminController
public function testForm(){
return "hello";
// return view('admin.test');
}
public function testExample()
{
$response = $this->get('/admin/test/form');
$response->assertSee("hello");
}
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.086, Memory: 26.00 MB
OK (1 test, 1 assertion)
なるほど、$response->assertSee(); で確認するのか。assertEqualsとかassertSameで確認してた。
$ vendor/bin/phpunit tests/Feature/AdminTest.php
use App\Models\User;
public function testExample()
{
// $response = $this->get('/');
$response = User::all();
dd($response);
}
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
Illuminate\Database\Eloquent\Collection^ {#1678
#items: []
}
なんでnullになるのか理解できん。
データベースのテストの方法が違うっぽいな。
コンソールログインだと、S3のバケットに保存している画像は1枚ずつしかダウンロードできません。
そんな馬鹿な。。。
1枚ずつ処理とか、プログラマーは絶対にやりません。そこを妥協するようであれば、プログラミング辞めるわ。
と言うことで、Cyberduckを使えば一括ダウンロードできるみたいなので、CyberDuckを使いたいと思います。
cyberduckで接続にsftpではなくAmazon S3を選ぶと、アクセスキーとシークレットキーを入れれば、バケットにアクセスできるようになります。

素晴らしい! 早く言ってよ。