2018-03-20 00:53:47 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
2018-03-20 00:42:00 +03:00
|
|
|
module Analysis.PHP.Spec (spec) where
|
|
|
|
|
|
|
|
import Data.Abstract.Value
|
|
|
|
import Data.Map
|
2018-03-23 18:57:02 +03:00
|
|
|
import Data.Map.Monoidal as Map
|
2018-03-20 00:42:00 +03:00
|
|
|
|
|
|
|
import SpecHelpers
|
|
|
|
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2018-03-23 18:57:02 +03:00
|
|
|
describe "PHP" $ do
|
|
|
|
it "evaluates include and require" $ do
|
2018-03-23 20:11:29 +03:00
|
|
|
env <- findEnv <$> evaluate "main.php"
|
2018-03-23 18:57:02 +03:00
|
|
|
env `shouldBe` [ (name "foo", addr 0)
|
|
|
|
, (name "bar", addr 1) ]
|
2018-03-21 02:46:32 +03:00
|
|
|
|
2018-03-23 18:57:02 +03:00
|
|
|
it "evaluates include_once and require_once" $ do
|
2018-03-23 20:11:29 +03:00
|
|
|
env <- findEnv <$> evaluate "main_once.php"
|
2018-03-23 18:57:02 +03:00
|
|
|
env `shouldBe` [ (name "foo", addr 0)
|
|
|
|
, (name "bar", addr 1) ]
|
|
|
|
|
|
|
|
it "evaluates namespaces" $ do
|
2018-03-23 20:11:29 +03:00
|
|
|
res <- evaluate "namespaces.php"
|
2018-03-23 21:25:02 +03:00
|
|
|
findEnv res `shouldBe` [ (name "NS1", addr 0)
|
|
|
|
, (name "Foo", addr 6) ]
|
2018-03-23 20:11:29 +03:00
|
|
|
|
|
|
|
let heap = findHeap res
|
2018-03-23 18:57:02 +03:00
|
|
|
Map.lookup (Precise 0) heap `shouldBe` ns "NS1" [ (name "Sub1", addr 1)
|
|
|
|
, (name "b", addr 4)
|
|
|
|
, (name "c", addr 5)
|
|
|
|
]
|
|
|
|
Map.lookup (Precise 1) heap `shouldBe` ns "Sub1" [ (name "Sub2", addr 2) ]
|
|
|
|
Map.lookup (Precise 2) heap `shouldBe` ns "Sub2" [ (name "f", addr 3) ]
|
2018-03-20 00:42:00 +03:00
|
|
|
|
|
|
|
where
|
2018-03-23 18:57:02 +03:00
|
|
|
ns n = Just . Latest . Just . injValue . Namespace (name n)
|
2018-03-20 00:42:00 +03:00
|
|
|
addr = Address . Precise
|
|
|
|
fixtures = "test/fixtures/php/analysis/"
|
2018-03-23 20:11:29 +03:00
|
|
|
evaluate entry = evaluateFiles phpParser
|
2018-03-20 00:42:00 +03:00
|
|
|
[ fixtures <> entry
|
|
|
|
, fixtures <> "foo.php"
|
2018-03-21 02:46:32 +03:00
|
|
|
, fixtures <> "bar.php"
|
2018-03-20 00:42:00 +03:00
|
|
|
]
|