Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lumen e2e test

lumen e2e test

How to do API E2E Test on Lumen in our team

shmurakami

March 07, 2018
Tweet

More Decks by shmurakami

Other Decks in Programming

Transcript

  1. APIςετͷॻ͖ํ use Concerns\InteractsWithContainer, Concerns\MakesHttpRequests, Concerns\InteractsWithAuthentication, Concerns\InteractsWithConsole, Concerns\InteractsWithDatabase, Concerns\InteractsWithSession, Concerns\MocksApplicationServices; ▸

    ༨ஊ) ConcernsΛίϐʔ͢Ε͹4ܥͰ΋ಉ͜͡ͱ͕Ͱ͖Δ public function testTest() { $this->json('GET', '/me', []); $this->seeJsonEquals([ 'id' => 1, 'name' => 'Shaka', ]); } TESTCODE SAMPLE
  2. ֎෦ґଘͷऔΓѻ͍ ▸ DatabaseMigrations Trait ▸ DatabaseTransactions Trait ▸ DB૚Λmockͤͣʹςετ͢Δ͜ͱ͕Ͱ͖Δ ▸

    ςετέʔε͝ͱʹMigrationͯ͠DB SchemaΛߋ৽ͨ͠ Γ ▸ ςετέʔεશମʹTransactionΛ͔͚ͯςετؒͰͷ σʔλͷӨڹΛແͨ͘͠Γ ֎෦ґଘ DATABASE
  3. ֎෦ґଘͷऔΓѻ͍ ▸ 1݅ͣͭmake͢Δඞཁ͕͋ͬͯΊΜͲ͏͍͘͞ ▸ ςετσʔλ͕ଟ͘ͳΔͱͭΒ͍ $factory->define(ModelName::class, function (Faker\Generator $faker) {

    return ['id' => 1, 'status' => 1,]; }); $factory->state(ModelName::class, ‘state_name’, function (Faker\Generator $faker) { return ['status' => 2,]; }); FACTORYͰςετσʔλੜ੒
  4. ֎෦ґଘͷऔΓѻ͍ ▸ ҰԠ͜͏͍͏΍Γํ΋… $factory->define(ModelName::class, function (Faker\Generator $faker) { ModelName::query()->insert([ ['id'

    => 1, 'status' => 1,], ['id' => 2, 'status' => 2,], ]); // modelͷattributeΛฦ͞ͳ͍ͱ͍͚ͳ͍ return [ 'id' => 3, 'status' => 3, ]; }); ▸ ͜͜·Ͱ͢ΔͳΒSeed࢖ͬͨΒ͍͍ͷͰ͸…
  5. ֎෦ґଘͷऔΓѻ͍ SQLITE ▸ ͦΜͳ͋ͳͨʹSQLite ▸ ࠷৽ͷSchemaͷঢ়ଶͷDBΛ࡞͓͍ͬͯͯϝϯόʔͱڞ༗Ͱ ͖Δ ▸ ࣮ମ͕୯ҰͷόΠφϦϑΝΠϧͳͷͰgitͰ؅ཧͰ͖Δ ▸

    ʮSQLite DBΛੜ੒͢ΔεΫϦϓτʯΛ࡞͓͚ͬͯ͹ίϯϑ ϦΫτ΋ؾʹ͠ͳͯ͘ྑ͘ͳΔ ͏ͪͰ࢖ͬͯΔartisanίϚϯυஔ͍ͱ͍ͨͷͰݟ͍ͨํͲ͏ͧ IUUQCJUMZ)'09
  6. ֎෦ґଘͷऔΓѻ͍ ▸ ֎෦APIΛݺͿΤϯυϙΠϯτͷςετͳͲ ▸ Facebook Maketing APIΛίʔϧ͢ΔAPIͷςετͳΜ͔΋ ॻ͍ͯ·͕͢஗͍ͷͰಛఆͷλΠϛϯάͰ͔࣮͠ߦ͠ͳ͍Α ͏ʹ͍ͯ͠·͢ ▸

    ςετͷ࣮ߦ࣌ؒͱ͔֎෦APIͷ࢓༷ʹґଘ͢ΔͷͰͦͷล Λߟྀͯ͠Ͳ͏͢Δ͔ܾΊΔͱྑ͍͔ͱ ▸ ඞཁͳ͍৔߹͸ؤுͬͯmock͢Δ ֎෦ґଘ 3RD PARTY SERVICE
  7. API DOCUMENTS ▸ Swaggerͱ͔ͷ࿩ ▸ ͏ͪ͸ҎԼͷཧ༝ͰSwaggerΛࣺͯͯapiDocΛ࢖ͬͯ·͢ ▸ DocΛݟΔͨΊʹyamlϑΝΠϧ͕ඞཁʹͳͬͯखؒ ▸ σϑΥϧτͰੜ੒Ͱ͖Δ੩తϖʔδͷσβΠϯ͕͋Μ·Γ

    ޷͖͡Όͳ͍(ݸਓͷײ૝Ͱ͢) ▸ Swaggerͷίʔυࣗಈੜ੒ػೳΛ·ͬͨ͘࢖͍ͬͯͳ͍ API DOCUMENTATION Swaggerͷཧղ͕ո͍͠Մೳੑ͕͋ΔͷͰؒҧͬͯͨΒ͝ΊΜͳ͍͞
  8. API DOCUMENTS ▸ apiDoc ▸ σβΠϯ͕؆ૉͰݟ௨͕͠ྑ͍ ▸ ੩తϑΝΠϧΛϏϧυ͢Ε͹(ૹΓ͚ͭΕ͹)ϩʔΧϧͰ΋ ݟΕΔ ▸

    Ϗϧυָ͕ ▸ ίʔυ಺(Doc Comment)ʹυΩϡϝϯτ͕͋ΔͷͰߋ৽ ͠ͳ͍ͱ͍͚ͳ͍ؾ࣋ͪʹͳΔ
  9. API DOCUMENTS /** * @param Request $request * @param $id

    * @return \Illuminate\Http\JsonResponse * * @api {get} /user/:user_id get user info * @apiName indexDynamicAdCreativeName * @apiDescription Get user info * @apiGroup User * * @apiParam {Number} user_id User ID * @apiParam {Number} [status] Status key * @apiSuccess {Number} id ID * @apiSuccess {String} name User Name */ APIDOC SAMPLE
  10. API DOCUMENTS public function testTest() { $request = [ ‘name’

    => ‘updated_name’, ]; $expectedResponse = [ 'id' => 1, 'name' => 'updated_name', ]; $this->json(‘PUT', '/me', $request); $this->seeJsonEquals($expectedResponse); }
  11. API DOCUMENTS DOCUMENT GENERATION SAMPLE protected $generateApiDoc = true; public

    function json($method, $uri, array $request = [], array $headers = [] { if (!$this->generateApiDoc) { return parent::json($method, $uri, $request, $headers); } $self = parent::json($method, $uri, $request, $headers); $response = json_decode($this->response->getContent()); $apiDocGenerator = new ApiDocGenerator(); $apiDocGenerator->setParser(new ApidocjsParser()) ->setFormat(new ApiDocFormatter()); @$apiDocGenerator->generate($request, $response); return $self; }